0

I am coding a Pacman game and I ran into few problems while trying to implement it.

I have a main JFrame which holds the background, and the points on it, the points are added in the paint() method.

I added to the frame a JPanel that makes the board which Pacman is on, Pacman is being painted by paintComponent(). Pacman movement is decided by a `KeyListener which is on the frame.

The problems I'm running into:

  1. If upon pressing a key I call the paint function of the frame, which later calls the paintComponent method of the panel, my screen is blinking because each time it repaints all of the points.

  2. If upon pressing a key I call directly to the panel paintComponent() method and paint the Pacman, it stays on the board like in gif number 2.

1:

2:

This is the paint method of the frame:

public void paint(Graphics g) {
    super.paint(g);
        for (int i = 0; i < 800; i++)
            for (int j = 0; j < 800; j++)
                if (boardPanel.getBoard().getTileXY(i, j).getPill() != null) 
                   g.drawImage(yellowPil, i, j, null);
    boardPanel.paintComponent(g);
}

And this is the paintComponent:

protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawImage(pacmanImage,pacman.getX(),pacman.getY(),null);
}

this is how i call each of this methods (its in the jframe class) :

    public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    switch (keyCode) {
        case KeyEvent.VK_RIGHT:
            boardPanel.movePacman(Direction.RIGHT);
            boardPanel.paintComponent(getGraphics()); // this will casue ` 
                                                         problem 2`
            break;
        case KeyEvent.VK_LEFT:
            boardPanel.movePacman(Direction.LEFT);
            repaint(); // this will cause problem 1
            break;
        case KeyEvent.VK_DOWN:
            boardPanel.movePacman(Direction.DOWN);
            repaint();
            break;
        case KeyEvent.VK_UP:
            boardPanel.movePacman(Direction.UP);
            repaint();
            break;
    }
}
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Based on what's above now, I'd say don't change the paint method of the frame, but instead do all the custom painting from the panel. Never directly call `paint` or `paintComponent` - always simply call `repaint()` on the component or any parent container. – Andrew Thompson May 29 '18 at 12:16
  • hey andrew, thanks for the help. i added more code to the thread. i have tried to call panel.repaint() but it didnt change anything at the code. is there any way i can delete all previous "pacman's" that shows up in problem number 2 ? thank again. – Aviv milnik May 29 '18 at 12:39
  • *"i added more code to the thread."* ... why? And more importantly, did you actually ***read*** the linked advice? Minus one because I think you made a random guess about what an MCVE / SSCCE is, and that is very poor research. – Andrew Thompson May 29 '18 at 13:18

0 Answers0