简体   繁体   English

没有调用paintComponent()

[英]paintComponent() doesn't get called

My paintComponent() won't get called.我的paintComponent()不会被调用。

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.起初,我没有frame.getContentPane().add(this) ,并认为答案是插入它,但都没有奏效。 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();
    }
}

Try calling super() as the first line of your constructor so that your JPanel initializes correctly.尝试调用 super() 作为构造函数的第一行,以便您的 JPanel 正确初始化。

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 注释来清除代码。

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

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

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).请发布您尝试绘制此 Frame 对象的上下文(您在 Frame 对象中使用该绘制方法的位置、时间和方式)。

General diagnostic resources:一般诊断资源:

1. Inheritance Hierarchy 1.继承层次

2. This post has a very good position on why not to use paintComponent() and to use repaint() instead. 2. 这篇paintComponent()对于为什么不使用paintComponent()而使用repaint()有很好的说明。 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.我从来没有遇到过需要使用paintComponent()构建路径的情况,但是我已经能够多次使用repaint()

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.您在游戏循环中调用repaint ,但repaint并不能保证窗口会被重绘,因此可能不会调用paintComponent You should instead use paintImmediately .您应该改为使用paintImmediately

paintImmediately(0, 0, 1920, 1080);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM