画图板的基本实现

 要实现画图板的基本功能,除了在qq登陆界面中用到的组件和方法外,还需要添加鼠标监听器,和接口实现。

首先,需要显示一个JFrame界面:

public class DrameFrame extends JFrame {
              //显示界面
     public void initUI(){
         this.setSize(600,600);
         this.setTitle("画图板");
         this.setDefaultCloseOperation(3);
         this.setLocationRelativeTo(null);
         this.setVisible(true);
         //从窗体上获取画笔,在窗体可见之后获取
         Graphics g=this.getGraphics();

}

  然后需要给界面添加监听器,获取点击时的坐标,

 DrameListener d=new DrameListener();
         //给窗体对象增添鼠标监听器
        this.addMouseListener(d);       

在新写一个类用于实现鼠标监听器接口,实现接口需要重写其中所有方法,在方法中添加自己希望实现的功能,包括获取点击时的坐标,绘制各种图形,代码如下:

public class DrameListener implements MouseListener,MouseMotionListener{
    private int x1,x2,y1,y2;                       //点击时获得的坐标,MouseMotionListener是用于实现拖动事件
    private Graphics g;                            
    private String s;                                 //s用于获得文本框中的字符串
    private ButtonListener but;
    private Color color;                             
    private int ax,ay,bx,by,cx,cy;                      //画三角形的坐标
    private int flag=1;                    
   
    public String getS(){
        return s;
    }
   
    //
    public void setBut(ButtonListener but){
        this.but = but;
    }
   
    public void setG(Graphics g){
        this.g=g;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        //通过标志找到点击过的点
        if(flag == 1 && s.equals("三角形")){
            ax = e.getX();
            ay = e.getY();
            flag++;
        }else if(flag == 2){
            bx = e.getX();
            by = e.getY();
            flag++;
        }else if(flag == 3){
            cx = e.getX();
            cy = e.getY();
            g.drawLine(ax, ay,bx, by);
            g.drawLine(bx, by,cx, cy);
            g.drawLine(ax, ay,cx, cy);
            flag = 1;
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
        s = but.getS();
        color =  but.getColor();                      
        g.setColor(color);                                           //按下时获得颜色
        System.out.println("but,getS()="+s);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        if(s.equals("直线")){
            System.out.println("x2 = "+x2 +"     y2 = "+y2);
            g.drawLine(x1, y1, x2, y2);
        }
        else if(s.equals("椭圆")){
                for(int i=1;i<=100;i++){
                g.setColor(new Color(2*i,i+100,0));
                g.fillOval(x1+i/2, y1+i/2, Math.abs(x1-x2)-i, Math.abs(y2-y1)-i);
                }
        }
        else if(s.equals("矩形")){
            g.drawLine(x1, y1, x1,y2);
            g.drawLine(x1, y1, x2, y1);
            g.drawLine(x2, y1, x2, y2);
            g.drawLine(x1, y2, x2, y2);
        }
//        else if(s.equals("三角形")){
//           g.drawLine(x1, y1, x2, y2);
//           g.drawLine(x1, y1, x1, y2);
//           g.drawLine(x1, y2, x2, y2);
//        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
       
    }

    @Override
    public void mouseExited(MouseEvent e) {
       
    }
  
    //实现拖动
    public void mouseDragged(MouseEvent e) {
        System.out.println(">>>>>>>>>>>");
        // TODO Auto-generated method stub
        if(s.equals("铅笔")){
        x2=e.getX();
        y2=e.getY();
        g.drawLine(x1, y1, x2, y2);
        x1=x2;y1=y2;
    }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // TODO Auto-generated method stub
   
    }
}

在添加一个类,用于侦听获取点击时按钮中的文字或颜色:

public class ButtonListener implements ActionListener {
        private String s  = "直线";
        private Color color;
       
        public String getS(){
            return s;
        }
        public Color getColor(){
            return color;
        }
    @Override
    public void actionPerformed(ActionEvent e) {

         if(e.getActionCommand().equals("")){
             //获取当前事件源对象
             JButton jbu=(JButton)e.getSource();
             //获取颜色按钮的背景色
            color =  jbu.getBackground();
            System.out.println("color= "+color);
         }else {
                //    获取按钮上的字符串
             s = e.getActionCommand();
         }
//        System.out.println("s = "+s);
    }
}


在实现过程中,需要注意的有画笔和参数的传递,监听器的正确添加,按钮中文字或颜色的正确获取。

猜你喜欢

转载自15700786134.iteye.com/blog/2216720
今日推荐