Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未调用Java组件_Java_Swing - Fatal编程技术网

未调用Java组件

未调用Java组件,java,swing,Java,Swing,由于某些原因,未调用paintComponent。如果线程没有生成,它将运行一次。为什么线程会阻止它运行 package com.example.cellcitygame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.util.ArrayList; import javax.swing.ImageIcon; i

由于某些原因,未调用paintComponent。如果线程没有生成,它将运行一次。为什么线程会阻止它运行

package com.example.cellcitygame;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 * Manages memory, entities, map, objects, etc.
 * 
 * @author John Smith
 */

public class Scene extends JPanel {
    public ArrayList<Entity> entities = new ArrayList<>();

    Image background;
    Image good;
    Image bad;
    Image digested;
    Image protein1;
    Image protein2;
    Image protein3;
    Image protein4;

    public Scene() {
        background = new ImageIcon(this.getClass().getResource("BG.png"))
                .getImage();
        good = new ImageIcon(this.getClass().getResource("Good.png"))
                .getImage();
        bad = new ImageIcon(this.getClass().getResource("Bad.png")).getImage();
        digested = new ImageIcon(this.getClass().getResource("Digested.png"))
                .getImage();
        protein1 = new ImageIcon(this.getClass().getResource("Protein1.png"))
                .getImage();
        protein2 = new ImageIcon(this.getClass().getResource("Protein2.png"))
                .getImage();
        protein4 = new ImageIcon(this.getClass().getResource("Protein4.png"))
                .getImage();
        protein3 = new ImageIcon(this.getClass().getResource("Protein3.png"))
                .getImage();

        Entity test = new Entity();
        test.targetX = 500;
        test.targetY = 300;
        test.x = 100;
        test.y = 100;
        entities.add(test);

        new UpdateThread().start();
    }

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

        for (Entity entity : entities) {
            entity.draw(g);
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    class UpdateThread extends Thread {
        public void run() {
            while (true) {
                for (Entity entity : entities) {
                    entity.update();
                }

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                repaint();
            }
        }
    }
}
我想知道这是否与更新循环太快有关


TIA

Toolkit.getDefaultToolkit.sync或g.dispose都不是好主意。我假设您实际将面板添加到顶级容器中,并且面板的大小大于0。我还假设实体实际上是在面板的可视范围内开始绘制的……实体周围也没有同步,这意味着当paintComponent方法尝试绘制它们时,线程可能正在更新它们,只是说……这是正确的。它看起来是随机的,有时它会画画,有时它不会。我有正在调试的坐标,它们在窗格内。重新绘制后可能也需要调用重新验证???@nachokk重新验证与布局API相关,最好在重新绘制前调用;