簡體   English   中英

僅在最小化最大化屏幕時才調用paintComponent()

[英]paintComponent() gets called only when Minimizing-Maximizing Screen

我正在嘗試開發一種我需要繪制網格的游戲。 為此我使用了由repaint()方法調用的paintComponent(Graphics g) repaint()方法。

問題是重繪方法在無限的While循環中,它永遠不會調用paintComponent()方法,除非我最小化和最大化屏幕。 之后它工作正常並在while循環中完美地調用paintComponent()

簡而言之,我需要通過最小化 - 最大化屏幕來觸發它。

有人可以幫幫我嗎?

在代碼中,您可以看到3個類,即Frame.java,MenuHandler.java和Screen.java。 代碼從Frame類中的main方法開始,並在Screen擴展JPanel時將Screen類添加到自身。 但是,僅當用戶在菜單上選擇“創建地圖”時,控件才會進入Screen類。 然后MenuHandler類將控件傳遞給Screen類,其中在createMap方法中調用run方法,並在此run方法中調用repaint方法。 包裝如此;

public class Screen extends JPanel implements Runnable {
    Frame frame;

    public Screen(Frame frame) {
        this.frame = frame;
    }

    public void createMap() {

        thread.start();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.BLUE);

    }

    @Override
    public void run() {

        System.out.println("Success******");

        long lastFrame = System.currentTimeMillis();

        int frames = 0;
        running = true;
        scene = 0;

        // the map grid would be refreshed every 2 ms so that we don't get the
        // flickering effect
        while (running) {

            frames++;

            if (System.currentTimeMillis() - 1000 >= lastFrame) {
                fps = frames;
                frames = 0;
                lastFrame = System.currentTimeMillis();
            }

            // to draw stuff all the time on the screen : goes around 2 millions
            // frames per second. Which is of no use.
            repaint();

            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.exit(0);
    }
}

運行方法中的定時器代碼:

 public void run() {

    System.out.println("Success");

    long lastFrame = System.currentTimeMillis();

    int frames = 0;
    running = true;
    scene = 0;

    // the map grid would be refreshed every 2 ms so that we don't get the
    // flickering effect
    while (running) {

        frames++;

        if (System.currentTimeMillis() - 1000 >= lastFrame) {
            fps = frames;
            frames = 0;
            lastFrame = System.currentTimeMillis();
        }
        System.out.println("before repaint");
        // to draw stuff all the time on the screen : goes around 2 millions
        // frames per second. Which is of no use.
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                repaint();
            }
        };

        new Timer(200, taskPerformer).start();
        System.out.println("after repaint");

        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    System.exit(0);
 }

我想出了這個問題。 我只需要添加一行來觸發paintComponent方法,而不是通過最小化窗口來實現它。

Frame是我的頂級容器,我正在向框架添加Screen組件(將JPanel擴展並具有paintComponent的實現)。 所以在添加時,我早些時候正在做

frame.add(screen);

我改為:

frame.getContentPane().add(screen);
frame.getContentPane().validate();

添加后調用validate方法為我做了。 我不知道它是否有意義,但是這是唯一對我有用的線路。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM