我有一个快速问题给你们,好吧,快点给你,对我来说不是很快。
我有一个充满Arc2D形状的数组,我有一个MouseAdapter
在mousePressed事件中,我需要将点击的Arc2D与Arc2D的数组进行比较,甚至更好,自动识别单击哪个Arc2D然后能够从那里开始工作(更改颜色,更具体)
我试过这个
private class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
Component c = gamePanel.getComponentAt(p);
if(c == arcs[0]) {
System.out.println("test");
}
}
}
当然不起作用,但老实说我不知道该怎么做
提前多多感谢!
编辑:
在查看我在这里得到的第一个答案后,我尝试将代码更改为此
private class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if(arcs[0].contains(p)) {
System.out.println("test");
}
}
}
当然不会起作用,因为arcs数组只包含稍后在此方法中绘制的弧
@Override
protected void paintComponent(Graphics graph) {
super.paintComponent(graph);
Graphics2D g = (Graphics2D) graph;
// smooth graphics
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// moving to the middle of the panel
g.translate(this.getWidth()/2, this.getHeight()/2);
// painting colored arcs
for(int i = 0; i < 4; i++) {
g.setColor(dimColors[i]);
g.fill(arcs[i]);
}
// painting borders
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5F));
g.drawLine(-98, 0, 98, 0);
g.drawLine(0, -98, 0, 98);
g.draw(circle);
// painting central white circle
g.setColor(Color.WHITE);
g.fill(smallCircle);
g.setColor(Color.BLACK);
g.draw(smallCircle);
}
我完全被人困住了
答案 0 :(得分:0)
这可以让你开始......
private class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Point click = e.getPoint();
//this next loop may seem hacky
for (Arc2D c: arcs) {
if (c.getX() == click.getX() && c.getY() == click.getY()) {
System.out.println("The arc at (" + c.getX() + ", " + c.getY() + ") was clicked.");
return;
}
}
System.out.println("No arcs were clicked.");
}
}