[英]Graphics g - Using paint, how do I make things erase?
my paint method goes like this. 我的绘画方法是这样的。
public void paint (Graphics g)
{
while (cardChosen != 'a');
{
g.drawImage (selectionBG, 0, 0, 1960, 677, null);
g.drawImage (duelSGX, x_coordinate, y_coordinate, 483, 677, null);
g.drawImage (Ultor, x_coordinate + 777, y_coordinate, 483, 677, null);
g.drawImage (Seirin, x_coordinate + 777 * 2, y_coordinate, 483, 677, null);
g.drawImage (Rowgen, x_coordinate + 777 * 3, y_coordinate, 483, 677, null);
g.drawImage (Ronel, x_coordinate + 777 * 4, y_coordinate, 483, 677, null);
g.drawImage (Ophelia, x_coordinate + 777 * 5, y_coordinate, 483, 677, null);
g.drawImage (Narza, x_coordinate + 777 * 6, y_coordinate, 483, 677, null);
g.drawImage (Michele, x_coordinate + 777 * 7, y_coordinate, 483, 677, null);
g.drawImage (Maxwell, x_coordinate + 777 * 8, y_coordinate, 483, 677, null);
g.drawImage (MageDEAN, x_coordinate + 777 * 9, y_coordinate, 483, 677, null);
g.drawImage (Kuda, x_coordinate + 777 * 10, y_coordinate, 483, 677, null);
g.drawImage (Gravion, x_coordinate + 777 * 11, y_coordinate, 483, 677, null);
}
}
I also have this. 我也有这个
if (ev.getKeyCode () == KeyEvent.VK_A)
cardChosen = 'a';
repaint ();
Now considering this shouldn't every thing I paint in pain disappear when I press 'a' and appear when I let go? 现在考虑一下,当我按“ a”键时,我痛苦中描绘的所有事物难道不应该消失吗? It doesn't.
没有。 The picture shows, up and it lags.
图片显示,向上并且滞后。
Your paint(...) method should be: 您的paint(...)方法应为:
public void paint(Graphics g)
{
super.paint(g); // to clear the background
// add your code here
}
You've got dangerous code: you've got a while (true)
loop within a painting method, and this will grind the GUI to an ignominious halt. 您有危险的代码:在绘画方法中有一个
while (true)
循环,这将使GUI陷入停顿。 Never do that. 绝对不要那样做。 Instead
代替
paintComponent(...)
method. paintComponent(...)
方法中进行绘制。 paintComponent(...)
method. paintComponent(...)
方法。 Often this should be the first line of your paintComponent method override, and this will erase the old images. if (...)
block of some type within the paintComponent method, and then change the state of this block in an event listener, then call repaint()
. if (...)
块,然后在事件侦听器中更改此块的状态,然后调用repaint()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.