简体   繁体   English

JFrame仅在调整窗口大小时更新

[英]JFrame updates only upon resizing window

I'm trying to make my own version of Snake for learning purposes. 我正在尝试制作自己的Snake版本以供学习。 Everything seems to work fine except that if I want my frame to be repainted, I have to resize my window manually. 除了要重新粉刷框架,必须手动调整窗口大小之外,其他所有内容似乎都可以正常工作。 Here is my code: 这是我的代码:

package snake;

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PlayGame extends JPanel implements Runnable{

    public boolean animate = false;           
    public final int FRAME_DELAY = 750;

    PickupBall b = new PickupBall();
    Snake bob = new Snake();


    public synchronized void start() {
        animate = true;
    }

    public synchronized void stop() {
        animate = false;
    }
    private synchronized boolean animationEnabled() {
        return animate;
    }

    @Override
    public void run(){            
        while(true){    
            if (animationEnabled()){                    
                repaint();                    
            }
            try {
                Thread.sleep(FRAME_DELAY);
            }   
            catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }        

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

        b.draw(g);
        bob.draw(g);            
    }        

    public static void main(String[] args) {    
        JFrame jfr = new JFrame("Snake");  
        jfr.setSize(640,640);
        jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
        jfr.setResizable(true);

        PlayGame p = new PlayGame();  

        jfr.setContentPane(p);  

        p.setBackground(Color.WHITE);            

        p.start();            

        new Thread(p).start();            
        jfr.setVisible(true);    

    }


}

Why isn't repaint() triggered without altering the frame size? 为什么不更改帧大小就不会触发repaint()? I get the correlation but it makes no sense to me why it needs such a trigger when it's in a while(true) loop anyway. 我得到了相关性,但是对我来说没有任何意义,为什么当它处于while(true)循环中时为什么需要这样的触发器。

What am I missing here? 我在这里想念什么?

Edit 1: Removed thread object Replaced t.start() with p.start() 编辑1:删除线程对象将t.start()替换为p.start()

Edit 2: Added new Thread(p).start(); 编辑2:添加了new Thread(p).start(); and now it works! 现在就可以了! Thanks. 谢谢。

Edit 3: Removed revalidate(); 编辑3:删除revalidate();

You are executing repaint() in the worker thread and not in the event dispatch tread (EDT) which is the only one actually drawing onto the screen. 您是在工作线程中执行repaint() ,而不是在事件分派踏步(EDT)中执行 ,后者是唯一实际绘制到屏幕上的事件。

You have to en queue the call to repaint() in the EDT using SwingUtilities static methods invokeLater() or invokeAndWait() . 您必须使用SwingUtilities静态方法invokeLater()invokeAndWait()在EDT中将对repaint()的调用排队。

Added new Thread(p).start(); 添加了new Thread(p).start();

Still have no idea how or why this is different to Thread t = new Thread(p); 仍然不知道这与Thread t = new Thread(p);为何或为何不同Thread t = new Thread(p); t.start();

But it worked. 但这行得通。

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

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