`
luyier
  • 浏览: 8522 次
社区版块
存档分类
最新评论

简易java画图板的实现

阅读更多

画图板的步骤:
一、窗口:
   1.定义一个继承自JFrame的窗口类
          public class 该类  extends JFrame{ }
   2.定义主函数,实例化画板窗体类对象,并且调用显示界面的方法
          public static void main(String[] args) {
  DrawFrame df = new DrawFrame();
  df.init();
    }
   3.显示界面,设置窗体属性
          setTitle("");
          setSize(  , );
          ……

二、组件对象:
   1.实例化组件对象,并设置命令动作,添加到窗体上
          JButton btnLine = new JButton("直线");
          btnLine.setActionCommand("Line");
          ……
          add(btnLine);
          add …
   2.按钮组件的监听器方法
   3.按钮的事件处理类实现
         使用“匿名类”来实现:
         ActionListener  al = new ActionListener(){
            //事件处理类的方法
            public  void  actionPerformed(ActionEvent e){
            if (e.getActionCommand().equals("Color")) {
  // JColorChooser类是一个颜色选择面板,使用showDialog方法将界面显示出来,null表示没有指定父窗体,第二参数是标题,但三个参数是默认的颜色
  color = javax.swing.JColorChooser.showDialog(null,"颜色选择器", Color.BLACK);
  System.out.println("你选择的颜色是:" + color);
     } else {
     // 得按钮上的动作命令值。
     item = e.getActionCommand();
     System.out.println("你点击的按钮是:" + item);
  }
            }
         };

    ActionListener 虽然不能来实例化对象,但是通过匿名类的方式,在{}中实现了实例化的功能,所以最后的}后面有个“;”,因为ActionListener  al = new ActionListener(){…}只是一句话;

   4.实例化3的对象,将其绑定到按钮的监听器方法中
          btnLine.addActionListener(al);
          ……
    //窗体可见
    setVisible{true};

三、画布中图形的实现:
    1.获取画布对象,在窗体对象上添加一个鼠标监听器方法
          //获取画布对象
   Graphics g = this.getGraphics();
  
   //给窗体添加鼠标监听器
   this.addMouseListener(…);
   this.addMouseMotionListener(…);
 
    2.实现鼠标监听器事件处理类
           public class DrawListener extends MouseAdapter {

   //定义保存坐标值的四个属性
   private int x1,y1,x2,y2;
 
   private Graphics g;
   private DrawFrame df;
 
   //接收画布对象的构造函数
   public DrawListener(Graphics g,DrawFrame df){
  this.g = g;
  this.df = df;
   }
        
   //鼠标点中时,执行该方法
   public void mousePressed(MouseEvent e){
  //按下鼠标的时候就获取第一个坐标点的值
  x1 = e.getX();
  y1 = e.getY();
   }
         /**
  * 画直线;矩形……
  */
   //鼠标释放时,执行该方法
   public void mouseReleased(MouseEvent e){
  //鼠标释放的时候获取第二个坐标点的值
      x2= e.getX();
      y2= e.getY();
  Draw dr ;
   //设置当前图像的颜色
   g.setColor(df.getColor());
   //判断点击的是否是直线
       if(df.getItem().equals("Line")){
          //实例化画直线类的对象
   dr = new DrawLine();
   //调用画直线的方法
   dr.draw(g, x1, y1, x2, y2);
   }
   //判断点击是否是矩形
          if(df.getItem().equals("Rect")){
   //实例化画矩形类的对象
   dr= new DrawRect();
   //调用画矩形的方法
   dr.draw(g, x1, y1, x2, y2);
   }
          类似的圆,填充矩形,填充圆。

        /**
  * 画曲线
  */
 //鼠标按下键后拖动时调动该方法
 public void mouseDragged(MouseEvent e){
     //鼠标拖动的时候就获取第二个坐标点的值
     x2 = e.getX();
     y2 = e.getY();
     Draw draw;
   //判断点击是否是曲线
   if (df.getItem().equals("Curve")){
     g.setColor(df.getColor());
     draw = new DrawLine();
     draw.draw(g, x1, y1, x2, y2);
     //互换坐标
     x1=x2;
     y1=y2;
   }
 }
    3.画图形的实现:
      直线:
        public class DrawLine extends Draw{
 //重写drwa抽象类中的draw抽象方法
 public void draw(Graphics g,int x1,int y1,int x2,int y2){
  System.out.println("画直线");
  //调用画直线的方法
  g.drawLine(x1,y1,x2,y2);
  
 }

}
      矩形(要考虑起始点坐标关系):
        public class DrawRect extends Draw{
 public void draw(Graphics g,int x1,int y1,int x2,int y2){
  System.out.println("画矩形");
  int width,height;
  if (x1 < x2){
   width = x2 - x1;
  } else{
   width = x1 - x2;
   x1 = x2;
  }
  if (y1 < y2){
   height = y2 - y1;
  }else {
   height = y1-y2;
   y1 = y2;
  }
  g.drawRect(x1, y1, width, height);
 }

}
    4.实例化3的对象,并且将其绑定到鼠标监听器方法中
        //实例化画图形事件处理类的对象
 DrawListener dl = new DrawListener(g,this);
  
 //给窗体添加鼠标监听器
 this.addMouseListener(dl);
 this.addMouseMotionListener(dl);
    5.添加上面需要的属性:
 public Color getColor(){
  return color;
 }
 public String getItem(){
  return item;
 }
 
 private Color color = Color.BLACK;
 private String item = "Line";
 

0
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics