如何在鼠标点击时防止形状发生变化?

时间:2015-11-20 05:18:49

标签: java graphics jframe mouseclick-event

我的代码有问题,如果我点击矩形并在屏幕上点击它会显示形状并且工作正常..但是如果我在那之后点击了圆圈,那么所有的rec 已存储的缠结停留在同一位置,但更改为圆形。如何防止已存储的形状发生变化?

头等舱:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class MouseClick implements ActionListener{
    private static int x,y;
    private static DrawingObjects object = new DrawingObjects();
   JPanel panel = new JPanel();
   JFrame frame = new JFrame("MouseClick");


   MouseClick(){
       dObjects();
   }
   void dObjects() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2,2));
        frame.add(object);
        frame.add(panel);
        panel.setBackground(Color.WHITE);
        panel.setSize(10, 300);
        panel.setLocation(100, 200);
        panel.setLayout(new GridLayout(1,2));
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setSize(400, 400);
        object.addMouseListener(new AL());

        Button rect = new Button("Rectangle");
        Button oval = new Button("Circle");
        panel.add(rect);
        panel.add(oval);
        rect.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {           
                object.setType(1);
}
        });         

   oval.addActionListener(new ActionListener() {

       public void actionPerformed(ActionEvent e)
       {            
        object.setType(2);
}
   });         }

   public static void main(String[] args){

      new MouseClick();
    }
    static class AL extends MouseAdapter{
        public void mouseClicked (MouseEvent e){
            x = e.getX();
            y = e.getY();
            object.drawing(x, y);
        }
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

第二课:

import javax.swing.*;

import javafx.scene.input.MouseEvent;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
public class DrawingObjects extends JPanel{
    private ArrayList<Point> points = new ArrayList<>();
    public int shapetype ;

    public void drawing(int x, int y){
        points.add(new Point(x, y));
        repaint();
    }


    public void setType(int choice){
        if(choice==1){
            shapetype = 1;
        }

        else if (choice ==2){
            shapetype = 2;
        }

    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);


        if(shapetype == 1){

        for(Point p : points){
            g.fillRect(p.x, p.y, 60, 20);
            g.setColor(Color.GREEN);
            repaint();
        }
        }

        else if (shapetype == 2){
            for(Point p : points){
                g.fillOval(p.x, p.y, 20, 20);
                g.setColor(Color.RED);
                repaint();

            }

    }
    }
}

1 个答案:

答案 0 :(得分:0)

出现此问题是因为shapetype未与绘制形状的点一起存储。因此,在paintComponent方法中,当points数组中存储的位置中的所有形状都重新绘制时,它们将使用当前值shapetype。第一次创建形状时,不是shapetype的值。

一个简单(但不是最好)的解决方案,只需最少的代码更改即可存储shapetype,如下所示。

DrawingObjects类中,添加另一个数组来存储shapetype

private ArrayList<Integer> shapeTypes = new ArrayList<>();

按如下所示更新drawing方法,以存储当前的shapetype和点。

public void drawing(int x, int y) {
    points.add(new Point(x, y));
    shapeTypes.add(shapetype);
    repaint();
}

重新绘制形状时请参考存储的shapetype,如下所示。

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    for (int i = 0; i < points.size(); i++) {
        Point p = points.get(i);
        if (shapeTypes.get(i) == 1) {

          g.fillRect(p.x, p.y, 60, 20);
          g.setColor(Color.GREEN);
          repaint();    
        } else if (shapeTypes.get(i) == 2) {
          g.fillOval(p.x, p.y, 20, 20);
          g.setColor(Color.RED);
          repaint();    
        }
    }
}

更好的设计是创建一个单独的类,它具有存储位置的能力以及形状类型以及如何绘制它的实现。