【问题标题】:How can I prevent shapes from changing on mouseclick?如何防止鼠标点击时形状发生变化?
【发布时间】:2015-11-20 05:18:49
【问题描述】:

我的代码有问题,如果我单击矩形并在屏幕上单击鼠标,它将显示形状并且工作正常。但如果我在那之后单击圆圈,所有的记录 已存储的缠结保持在同一位置,但变为圆形。如何防止已存储的形状发生变化?

头等舱:

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();

            }

    }
    }
}

【问题讨论】:

  • state 应该只用于创建形状,而不是绘制它。您将需要生成某种“形状”数据,它知道应该如何绘制,或者使用Graphics2D中的Shape API
  • @MadProgrammer 你能解释一下吗,我不太明白你指的是什么状态。

标签: java graphics jframe mouseclick-event


【解决方案1】:

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

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

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

private ArrayList&lt;Integer&gt; shapeTypes = new ArrayList&lt;&gt;();

更新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();    
        }
    }
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多