Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共1 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

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

1楼
admin 发表于: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编辑过]
共1 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.