0

I am trying to code a little brick breaker project and I am having a trouble with the graphic aspects of it. I don't understand how to update properly my Panel. When I call the repaint() method it won't update every time. My "fix" was to add a call to repaint() in the paintComponent() method of my panel but it does not seems right and my panel will redraw multiple times. This is my code:

I have my play() method which will make the game work :

public void play()
{
    long framesDrawn = 0;
    long lastFrame = System.currentTimeMillis();
    long currentTime = System.currentTimeMillis();
    while(!hasGameEnded())
    {

        double dt = (currentTime - lastFrame)/1000.;
        if(dt > 1./_fps) // Draws a new frame at the desired fps
        {
            framesDrawn++;
            System.out.println(dt);
            moveItems(dt);
            drawNewFrame();
            System.out.println("new frame drawn (frame "+framesDrawn+" )\n");
            lastFrame = currentTime;
        }

        currentTime = System.currentTimeMillis();
    }
}

It calls the drawNewFrame() method:

private void drawNewFrame()
    {
        ArrayList<GameItem> allItems = new ArrayList<>();
        allItems.addAll(_allItems);
        allItems.addAll(_platforms);
        _gameFrame.getGamePanel().updateItems(allItems);
        _gameFrame.getGamePanel().repaint();
        _gameFrame.repaint();
    }

And my custom JFrame and JPanel are:

public class GameFrame extends JFrame {
    private GamePanel _gamePanel;

    public GameFrame(GamePanel gamePanel)
    {
        _gamePanel = gamePanel;
        this.setTitle("Brickbreaker");
        this.setSize(800, 1200);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(true);
        this.setContentPane(_gamePanel);
        this.setVisible(true);
    }

    public GamePanel getGamePanel()
    {
        return _gamePanel;
    }


}


public class GamePanel extends JComponent {
    private int _gameWidth, _gameHeight;
    private ArrayList<GameItem> _gameItems;
    public GamePanel(int gameWidth, int gameHeight, ArrayList<GameItem> gameItems )
    {
        _gameWidth = gameWidth;
        _gameHeight = gameHeight;
        _gameItems = gameItems;
        this.setOpaque(true);
        this.setBackground(Color.WHITE);
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int x = getX();
        int y = getY();
        int height = getHeight();
        int width = getWidth();

        float widthRatioMultiplier = ( (float)width )/( (float)_gameWidth );
        float heightRatioMultiplier = ( (float) height)/( (float) _gameHeight);

        for( GameItem gameItem : _gameItems)
        {
            gameItem.draw(g, widthRatioMultiplier, heightRatioMultiplier );
        }
        repaint();
    }

    public void updateItems(ArrayList<GameItem> updatedItems)
    {
        _gameItems = updatedItems;
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
avincigu
  • 35
  • 6
  • 4
    Don't use a while loop on the EDT this will prevent the GUI from repainting itself. *My "fix" was to add a call to repaint() in the paintComponent() method* - which is wrong and should be removed. For animation use a `Swing Timer`. For example: https://stackoverflow.com/a/54028681/131872. If you really want to use a while loop then you should be using a SwingWorker. Read the section from the Swing tutorial on [Concurrency](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for more information about the EDT and the SwingWorker. – camickr Dec 25 '20 at 17:18
  • 1
    Also have a look my project on github [SwingGameLibrary](https://github.com/davidkroukamp/SwingGameLibrary). It contains much of the necessary foundation for a game in swing and I'm sure there are a couple of things you could find there to help you out. – David Kroukamp Dec 25 '20 at 18:20
  • There are several brick breaker games available online with source code, like [this one](https://github.com/awaismirza/Java-Game-Brick-Breaker) which also has an explanatory [video](https://www.youtube.com/watch?v=K9qMm3JbOH0). Perhaps you can learn from them? – Abra Dec 26 '20 at 08:45
  • Thank you for your answers I will read your code and look at the videos :) – avincigu Dec 26 '20 at 16:17

0 Answers0