【问题标题】:Detecting when multiple Ellipse objects are being clicked检测何时单击多个椭圆对象
【发布时间】:2014-04-25 08:41:47
【问题描述】:

我正在开发 Squares 游戏的一个版本。为此,我需要检测我的椭圆何时被点击。但问题是我的方法是使用一个 Ellipse 对象。如何检测单击了哪个椭圆?这是我的代码。

主广场类

public static boolean running = false;

public Squares() {

    this.setSize(600, 600);
    this.setTitle("Squares");
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setContentPane(new SquarePane());
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

public static void main(String[] args) {

    try {
        new Squares();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Crashed");
        System.exit(-1);
    }

    running = true;
}

}

SquaresPanel 类

public static int x = 100;
public static int y = 100;

public static Color randomColor;

public static float r;
public static float g;
public static float b;

public void paintComponent(Graphics gra) {

    Graphics2D g2d = (Graphics2D) gra;

    gra.setColor(Color.black);
    gra.fillRect(0, 0, 600, 600);

    Random rand = new Random();

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {

            Ellipse2D oval = new Ellipse2D.Double(x, y, 10, 10);

            r = rand.nextFloat();
            g = rand.nextFloat();
            b = rand.nextFloat();

            randomColor = new Color(r, g, b);

            g2d.setColor(randomColor);
            g2d.fill(oval);

            x += 50;
        }

        x = 100;
        y += 50;
    }
}

谢谢大家!

【问题讨论】:

    标签: java swing graphics jpanel ellipse


    【解决方案1】:

    不用过多查看您的代码(因为我发现它缺少很多),我将解释如何实现您的要求。

    第一个:您需要一个MouseListener 并实现mousePressed。从MouseEvent 对象中,您可以获得点击的点。如果您不确定,请参阅How to Write MouseListener

    public void mousePressed(MouseEvent e) {
        Point p = e.getPoint();
    }
    

    第二个:保留一个List 的省略号

    List<Ellipse2D> ellipses;
    

    3rd:保留一个selectedEllipse 变量来保存选择的变量。

    Ellipse2D selectedEllipse;
    

    4th:单击该点后,循环遍历列表,检查每个Ellipse2D.contains 点是否。然后对选中的 Ellipse 做一些事情

    public void mousePressed(MouseEvent e) {
        Point p = e.getPoint();
        for (Ellipse2D ellipse : ellipses) {
            if (ellipse.contains(p) {
                selectedEllipse = ellipse;
                // do something with selectedEllipse
                break;
            } else {
                selectedEllipse = null;
            }
        }
    }
    

    5th:循环遍历您的 ellipses 以在 paintComponent 方法中绘制

    protected void paintComponent(Grapchics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for (Ellipse2D ellipse : ellipses) {
            g2.fill(ellipse):
        }
    }
    

    旁注

    • 必须在您的 paintComponent 方法中调用 super.paintComponent

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

    更新

    仔细查看您的代码后,我看到了更多您想要实现的目标。看起来您想要椭圆的 8 x 8 网格。另一种选择是创建 64 个面板。并涂上每一个。

    首先有一个面板类,您可以在其中设置颜色

    public class EllipsePanel extends JPanel {
        private Color color;
    
        public EllipsePanel(Color color) {
            this.color = color;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(color);
            g.fillOval(0, 0, getWidth(), getHeight());
        }
    }
    

    然后您可以使用一个面板来保存所有这些面板并使用GridLayout,但也可以保留一个JPanel[][],以便您可以轻松地参考每个面板。您还可以为每个面板添加鼠标侦听器

    JPanel gridPanel = new JPanel(new GridLayout(8, 8));
    EllipsePanel[][] panels = new EllipsePanel[8][8];
    EllipsePanel selectedPanel = null;
    int currentRow;
    int currentCol;
    ...
    for (int i = 0; i < 8; i++) {
        for (int j = 0; i < 8; j++) {
            final EllipPanel panel = new EllipsePanel(getRendomColor);
            panel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    selectedPanel = panel;
                    // do something with selected panel;
                }
            });
            gridPanel.add(panel);
        }
    }
    

    【讨论】:

    • 非常感谢!这个答案很有用
    • 查看我的 UPDATE 以了解其他解决方案。
    • +1 表示contains();与缩放相关的示例被引用here
    【解决方案2】:

    您应该在您的 JPanel 上实现一个鼠标侦听器,然后使用从侦听器检索到的单击位置来确定单击了哪个椭圆

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2017-07-01
      • 2014-11-29
      相关资源
      最近更新 更多