课外天地 李树青学习天地Java程序语言课件 → 程序代码——基于多态编程的绘图程序


  共有17920人关注过本帖树形打印复制链接

主题:程序代码——基于多态编程的绘图程序

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1945 积分:26736 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——基于多态编程的绘图程序  发帖心情 Post By:2007/6/7 5:53:19 [只看该作者]

1、IShape接口
import java.awt.*;

public interface IShape
{
public void setShape(int x1,int y1,int x2,int y2);
public void setColor(Color c);
public void draw(Graphics g);
}

2、IShape实现类——Shape
import java.awt.*;

public class Shape implements IShape
{
protected int x1,y1,x2,y2;
Color color;

public void setShape(int x1,int y1,int x2,int y2)
{
  this.x1=x1;
  this.y1=y1;
  this.x2=x2;
  this.y2=y2;
}

public void setColor(Color c)
{
  this.color=c;
}

public void draw(Graphics g)
{
}
}

3、Rectangle子类
import java.awt.*;

public class Rectangle extends Shape
{
public Rectangle()
{  
}

public Rectangle(int x1,int y1,int x2,int y2)
{
  setShape(x1,y1,x2,y2);
}

public void draw(Graphics g)
{
  g.setColor(color);
  g.drawRect(x1,y1,x2-x1,y2-y1);
}
}

4、Circle子类
import java.awt.*;

public class Circle extends Shape
{
public Circle()
{  
}

public Circle(int x1,int y1,int x2,int y2)
{
  setShape(x1,y1,x2,y2);
}

public void draw(Graphics g)
{
  g.setColor(color);
  g.drawOval(x1,y1,x2-x1,y2-y1);
}
}

5、Line子类
import java.awt.*;

public class Line extends Shape
{
public Line()
{  
}

public Line(int x1,int y1,int x2,int y2)
{
  setShape(x1,y1,x2,y2);
}

public void draw(Graphics g)
{
  g.setColor(color);
  g.drawLine(x1,y1,x2,y2);
}
}

6、应用程序类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class exec
{
public static void main(String args[])
{
  MyFrame m=new MyFrame("OK");
  m.setSize(400,400);
  m.setLocation(100,100);
  m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  m.setVisible(true);
}
}

class MyFrame extends JFrame
{
public MyFrame(String title)
{
  super(title);
  this.getContentPane().setLayout(new BorderLayout());
  DrawPanel dp=new DrawPanel();
  getContentPane().add(dp,BorderLayout.CENTER);
  getContentPane().add(new ControlPanel(dp),BorderLayout.SOUTH);
}
}

class DrawPanel extends JPanel implements MouseListener,MouseMotionListener
{
int x1;
int y1;
int x2;
int y2;
Color color;
IShape shape;

public DrawPanel()
{
  setBackground(Color.white);
  addMouseListener(this);
  addMouseMotionListener(this);
}

public void mousePressed(MouseEvent e)
{
  x1=e.getX();
  y1=e.getY();
}

public void mouseReleased(MouseEvent e)
{
  x2=e.getX();
  y2=e.getY();
  Graphics g=this.getGraphics();  
  shape.setColor(color);
  shape.setShape(x1,y1,x2,y2);
  shape.draw(g);
    
}  

public void mouseClicked(MouseEvent e)
{;}

public void mouseEntered(MouseEvent e)
{;}

public void mouseExited(MouseEvent e)
{;}

public void mouseDragged(MouseEvent e)
{;  
}

public void mouseMoved(MouseEvent e)
{;}

}

class ControlPanel extends JPanel
{
public ControlPanel(DrawPanel dp)
{
  this.setLayout(new GridLayout(2,1));
  this.add(new ColorPanel(dp));
  this.add(new ShapePanel(dp));
}
}

class ColorPanel extends JPanel implements ItemListener
{
DrawPanel dp;
JRadioButton rb1 = new JRadioButton("red");
JRadioButton rb2 = new JRadioButton("green");
JRadioButton rb3 = new JRadioButton("blue");

public ColorPanel(DrawPanel dp)
{
  this.dp=dp;
  this.setLayout(new FlowLayout());
  ButtonGroup bg = new ButtonGroup();
  bg.add(rb1);
  bg.add(rb2);
  bg.add(rb3);
  this.add(rb1);
  this.add(rb2);
  this.add(rb3);
  rb1.addItemListener(this);
  rb2.addItemListener(this);
  rb3.addItemListener(this);
}

public void itemStateChanged(ItemEvent e)
{
  JRadioButton c=(JRadioButton)e.getItem();
  if(rb1.isSelected())
  {
   dp.color=Color.red;
  }
  else if(rb2.isSelected())
  {
   dp.color=Color.green;
  }
  else
  {
   dp.color=Color.blue;
  }
}
}

class ShapePanel extends JPanel implements ItemListener
{
DrawPanel dp;
JRadioButton rb1 = new JRadioButton("Line");
JRadioButton rb2 = new JRadioButton("Rectangle");
JRadioButton rb3 = new JRadioButton("Circle");

public ShapePanel(DrawPanel dp)
{
  this.dp=dp;
  this.setLayout(new FlowLayout());
  ButtonGroup bg = new ButtonGroup();
  bg.add(rb1);
  bg.add(rb2);
  bg.add(rb3);
  this.add(rb1);
  this.add(rb2);
  this.add(rb3);
  rb1.addItemListener(this);
  rb2.addItemListener(this);
  rb3.addItemListener(this);

}

public void itemStateChanged(ItemEvent e)
{
  JRadioButton c=(JRadioButton)e.getItem();
  if(rb1.isSelected())
  {
   dp.shape=new Line();
  }
  else if(rb2.isSelected())
  {
   dp.shape=new Rectangle();
  }
  else
  {
   dp.shape=new Circle();
  }
}
}

[此贴子已经被作者于2010-12-12 08:18:44编辑过]

 回到顶部