0
\$\begingroup\$

I dont know why i need to call super.paintComponent() and super.repaint() to make the player's movements update, if i just put paintComponent the game seems like to always be stopped. However I think that my mistake is the render() method, if a remove it, it doesn't make any difference, so it is useless, I've been looking for the error but I still can not see it. I would appreciate some help

Here is the paint method of the drawing class

public void paint(Graphics g) {
    super.paintComponent(g);

    setFocusable(true);
    requestFocusInWindow();
    setBackground(Color.BLACK);
    player.drawPlayer(g, getPosX(), getPosY());
    enemy.drawEnemy(g);
    //I know this should not be here
    super.repaint();
}

Pricipal GameLoop and render method

public void run() {
    long lastLoop = System.nanoTime();
    final int TARGET_FPS = 60;
    final long OPTIM_TIME = 1000000000 / TARGET_FPS;
    long lastFPSTime = 0;
    int fps = 0;
    double delta = 0;

    while (running) {
        long now = System.nanoTime();
        lastLoop = now; 
        delta += (now - lastLoop) / ((double) TIEMPO_OPTIMO);

        lastFPSTime += (now - lastLoop);
        fps++;

        if (lastFPSTime >= 1000000000) {
            System.out.println("[FPS : " + fps + " ]");
            lastFPSTime = 0;
            fps = 0;
        }

        while (delta >= 1) {
            render();
            delta--;
        }

        try {
            Thread.sleep((System.nanoTime() - lastLoop + OPTIM_TIME) / 1000000);
        } catch (InterruptedException ex) {
            Logger.getLogger(mainGameLoop.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(2);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    drawing.paint(g);
    g.dispose();
    bs.show();
}

And as last and least probable, I thought that maybe there is something that is not adding correctly to my window

A normal window class

Drawing drawing = new Drawing();

public Window(String title,int width, int hight, mainGameLoop mainGame){
    JFrame frame = new JFrame(title);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(width, hight);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.add(mainGame);
    frame.add(drawing);

    mainGame.start();
    mainGame.run();
\$\endgroup\$
4
  • \$\begingroup\$ super.repaint() does what it says, it repaints the image. If you don't override it, then you don't need the super \$\endgroup\$
    – Bálint
    Jun 21, 2017 at 7:09
  • \$\begingroup\$ The use of a BufferStrategy.show and Java's repaint system are kind of mutually exclusive, if you use the former in your game loop you shouldn't have to call repaint or paintComponent. Can you produce a MCVE that demonstrates why you need to call these two methods? \$\endgroup\$
    – Thomas
    Jun 23, 2017 at 5:09
  • \$\begingroup\$ Thomas, I did it, how could I give you the code? (I'm a little new in these forums) \$\endgroup\$
    – Smuk
    Jun 23, 2017 at 21:10
  • \$\begingroup\$ @Smuk You can edit your question. There is a button at the bottom of the question, above the comments. \$\endgroup\$
    – Evorlor
    Mar 20, 2022 at 5:47

1 Answer 1

-1
\$\begingroup\$

paintComponent() method is called when you call repaint(). I used paintComponent(). It's very good solution.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .