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\$
  • \$\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 '17 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 '17 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 '17 at 21:10
0
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.