-1

My paintComponent() won't get called.

I have Googled a little, and haven't found an answer that I could use. At first, I didn't have the frame.getContentPane().add(this), and thought that the answer was to insert that, but neither that worked. I hope someone can help me out.

Here you have a little snippet of my code:

package engine;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Frame extends JPanel {
    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private GameEngine engine;
    private Game game;

    public Frame(GameEngine engine) {
        this.engine = engine;
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(1920, 1080);
        frame.getContentPane().add(this);
    }

    public void updateGame(Game game) {
        this.game = game;
    }

    public JFrame getFrame() {
        return frame;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.drawString("" + game.getClockDate(), 1920 - 100, 20);
        System.out.println("test");
    }
}

I'm calling it from here, in another class:

public void loop() {
    if (this.ingame) {
        game.loop();
        frame.updateGame(game);
        frame.repaint();
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
markus rytter
  • 140
  • 1
  • 3
  • 13
  • 1
    How is `loop` called? You should also reset creating a `JFrame` inside your `Frame`'s constructor, this kind of side effect is dangerous, bad design and difficult to manage, it's not your `Frame`s responsibility to decide how it's used ;) – MadProgrammer Jan 17 '17 at 21:09
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 18 '17 at 00:41

3 Answers3

0

Try calling super() as the first line of your constructor so that your JPanel initializes correctly.

public Frame(GameEngine engine) {
    super();
    this.engine = engine;
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setUndecorated(true);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);
    frame.setSize(1920, 1080);
    frame.getContentPane().add(this);
}

Also call the @Override annotation for getting your code clear.

@Override    
public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        g2d.drawString("" + game.getClockDate(), 1920 - 100, 20);
        System.out.println("test");
}
nikowis
  • 103
  • 5
0

Please post what context you are trying to paint this Frame object (where, when, and how you are using the paint method in your Frame object).

General diagnostic resources:

1. Inheritance Hierarchy

2. This post has a very good position on why not to use paintComponent() and to use repaint() instead. I have never been put into a situation where I have needed to forge a path with paintComponent(), however I have been in a position to use repaint() several times.

Community
  • 1
  • 1
bmc
  • 817
  • 1
  • 12
  • 23
0

You are calling repaint in the game loop, but repaint does not guarantee that window will be repainted and therefore paintComponent may not be called. You should instead use paintImmediately.

paintImmediately(0, 0, 1920, 1080);
Vladimír Bielený
  • 2,795
  • 2
  • 11
  • 16
  • From the docs *"In most cases it's more efficient to call repaint"* :P - Not say that this isn't a possibility, but I'd prefer to know what `paintComponent` isn't been called properly in the first place, as `repaint` should (eventually (milliseconds)) trigger and new paint cycle – MadProgrammer Jan 17 '17 at 21:13