单击鼠标切换图形点
我正在制作一个程序,可以单击窗口,然后在那里放置一个点。如果用户再次单击,该点将被删除。以编程方式,每次单击都会创建另一个名为“Element”的类的新实例,其中包含单个点的 (X, Y) 位置。
为了实现这一目标,我扩展了 JPanel 并实现了 MouseListener。为了绘制点,我重写了paint()方法。每次用户单击时,对 MouseListener 的 mouseReleased() 进行编码,要么添加到 ArrayList,要么从其中删除,然后调用 Paint(),其中屏幕被清除并重新绘制 ArrayList。
我遇到的问题是点击时这些点不会消失。我不知道这是我对paint()缺乏理解,还是与ArrayList有关。
这是我的paint():
public void paint(Graphics g)
{
// Clear screen
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
// Display what elements will be drawn (for debugging)
System.out.println("About to draw the following elements:");
for (Element e : elements)
{
System.out.println("\t" + e);
}
// Draw Elements
g.setColor(Color.BLACK);
for(int i=0; i < elements.size(); i++)
{
g.fillOval(elements.get(i).x, elements.get(i).y, 10, 10);
}
}
这是鼠标单击方法:
public void mouseReleased(MouseEvent e)
{
// Rounds to the nearest grid space (spacing is currently 20px)
int roundX = (int) ((float)(Math.round(e.getX() / GRID_SPACING)) * GRID_SPACING);
int roundY = (int) ((float)(Math.round(e.getY() / GRID_SPACING)) * GRID_SPACING);
System.out.println("Clicked (" + roundX + ", " + roundY + ")");
// Go through each element...
for (int i=0; i < elements.size(); i++)
{
// if an element exists at the coordinates clicked,
if (elements.get(i).getX() == roundX && elements.get(i).getY() == roundY)
{
// remove it from the elements list
elements.remove(i);
i--;
System.out.println("\tElement exists at (" + roundX + ", " + roundY + "). Removing it.");
}
}
elements.add(new Element(roundX, roundY));
repaint();
}
输出如下:
About to draw the following elements: (None)
Clicked (140, 100)
About to draw the following elements:
This element's coordinates are (140, 100)
Clicked (160, 100)
About to draw the following elements:
This element's coordinates are (140, 100)
This element's coordinates are (160, 100)
Clicked (140, 100)
Element exists at (140, 100). Removing it.
About to draw the following elements:
This element's coordinates are (160, 100)
This element's coordinates are (140, 100)
I am making a program where one can click on the window, and a point will be placed there. If the user clicks again, the point will be removed. Programmatically, each click will create a new instance of another class called "Element", which contains the (X, Y) positions for a single point.
To achieve this, I am extending JPanel and implementing a MouseListener. To draw the points, I'm overriding the paint() method. Every time a user clicks, code the MouseListener's mouseReleased() either adds to the ArrayList, or removes from it, then calls paint(), where the screen is cleared and the ArrayList is redrawn.
The problem I am having is that the points aren't going away when clicked on. I don't know if it's my lack of understanding of paint(), or something to do with the ArrayList.
Here is my paint():
public void paint(Graphics g)
{
// Clear screen
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
// Display what elements will be drawn (for debugging)
System.out.println("About to draw the following elements:");
for (Element e : elements)
{
System.out.println("\t" + e);
}
// Draw Elements
g.setColor(Color.BLACK);
for(int i=0; i < elements.size(); i++)
{
g.fillOval(elements.get(i).x, elements.get(i).y, 10, 10);
}
}
And here is the mouse click method:
public void mouseReleased(MouseEvent e)
{
// Rounds to the nearest grid space (spacing is currently 20px)
int roundX = (int) ((float)(Math.round(e.getX() / GRID_SPACING)) * GRID_SPACING);
int roundY = (int) ((float)(Math.round(e.getY() / GRID_SPACING)) * GRID_SPACING);
System.out.println("Clicked (" + roundX + ", " + roundY + ")");
// Go through each element...
for (int i=0; i < elements.size(); i++)
{
// if an element exists at the coordinates clicked,
if (elements.get(i).getX() == roundX && elements.get(i).getY() == roundY)
{
// remove it from the elements list
elements.remove(i);
i--;
System.out.println("\tElement exists at (" + roundX + ", " + roundY + "). Removing it.");
}
}
elements.add(new Element(roundX, roundY));
repaint();
}
The output of this is as follows:
About to draw the following elements: (None)
Clicked (140, 100)
About to draw the following elements:
This element's coordinates are (140, 100)
Clicked (160, 100)
About to draw the following elements:
This element's coordinates are (140, 100)
This element's coordinates are (160, 100)
Clicked (140, 100)
Element exists at (140, 100). Removing it.
About to draw the following elements:
This element's coordinates are (160, 100)
This element's coordinates are (140, 100)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果该元素已在上一个循环中被删除,则不应
add(new Element(roundX, roundY))
You should not
add(new Element(roundX, roundY))
if the element has been removed in the previous loop不要重写 Swing 面板中的
paint(Graphics)
。请改用paintComponent(Graphics)
。Don't override
paint(Graphics)
in a Swing panel. UsepaintComponent(Graphics)
instead.