Reputation: 17
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? It doesn't. The picture shows, up and it lags.
Upvotes: 0
Views: 155
Reputation: 285405
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. Never do that. Instead
paintComponent(...)
method.paintComponent(...)
method. 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()
.Upvotes: 1
Reputation: 324128
Your paint(...) method should be:
public void paint(Graphics g)
{
super.paint(g); // to clear the background
// add your code here
}
Upvotes: 1