Hisoka Freescs
Hisoka Freescs

Reputation: 17

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? It doesn't. The picture shows, up and it lags.

Upvotes: 0

Views: 155

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

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

  • Use Swing and draw within a JPanel or JCompnent's paintComponent(...) method.
  • Be sure to call the super's paintComponent(...) method. Often this should be the first line of your paintComponent method override, and this will erase the old images.
  • Most important, get all program logic, like that while loop, out of all painting methods. All painting methods must be as blindingly fast as possible, and so you never want code in there that could be called elsewhere and that could possibly slow it down. Also you never have full control over if or when the painting methods are called, and so program logic might falter if present within these methods.
  • Better would be to have an if (...) block of some type within the paintComponent method, and then change the state of this block in an event listener, then call repaint().
  • The while shouldn't be in your code regardless, since yours is an event driven program. Instead listen for events, and then respond.

Upvotes: 1

camickr
camickr

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

Related Questions