我正在开发一个版本的Square游戏。为此,我需要检测我的省略号何时被点击。但问题是我的方法使用的是一个Ellipse对象。如何检测正在单击的是哪个椭圆?这是我的代码。
Main Square类
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;
}
}谢谢你们!
将要
发布于 2014-04-25 16:56:46
我不会过多地看你的代码(因为我看到它缺少很多),我只会解释如何实现你的需求。
第一:您需要一个MouseListener并实现mousePressed。从MouseEvent对象中,您可以获取所单击的点。如果您不确定,请参阅How to Write MouseListener。
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
}第二:保持省略的List
List<Ellipse2D> ellipses;第三:保留一个selectedEllipse变量来保存选择的变量。
Ellipse2D selectedEllipse;第4步:单击点后,遍历列表,检查是否每个Ellipse2D.contains都指向该点。然后对选定的椭圆执行某些操作
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;
}
}
}第5步:循环遍历ellipses以在paintComponent方法中绘制
protected void paintComponent(Grapchics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Ellipse2D ellipse : ellipses) {
g2.fill(ellipse):
}
}备注
paintComponent方法中调用super.paintComponentprotected void paintComponent(图形g) { super.paintComponent(g);}
更新
在仔细查看您的代码之后,我看到了您想要实现的更多内容。看起来你想要一个8乘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);
}
}发布于 2014-04-25 16:49:25
您应该在JPannel上实现一个鼠标侦听器,然后使用从侦听器检索到的单击位置来确定单击的是哪个椭圆
https://stackoverflow.com/questions/23288235
复制相似问题