java - Run 方法被调用,但不会绘制

标签 java swing jframe

如果运行该程序,您可以看到当调用 run 时,它会打印“Run() 方法被调用”。但是 if 语句中的 System.out.println() 不会被调用,也不会调用 render() 方法。

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
public static int WIDTH = 300;
public static int HEIGHT = WIDTH / 16*9;
public static int SCALE = 3;
public static String TITLE = "";

private Thread thread;
private boolean running = false;
private JFrame frame;


public void start() {
    if(running) return;

    thread = new Thread(this);
    thread.start();
}

public void stop() {
    if(!running) return;

    try{
        thread.join();
    }catch(InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    System.out.println("Run() has been called");
    long lastTime = System.nanoTime();
    long timer = System.currentTimeMillis();
    double ns = 1000000000.0 / 60.0;

    double delta = 0;
    int ticks = 0;
    int fps = 0;

    while(running) {
        long now = System.nanoTime();
        delta += (now-lastTime) / ns;
        lastTime = now;
        while(delta >= 1) {
            tick();
            ticks++;
            delta--;
        }
        render();
        fps++;
        if(System.currentTimeMillis() - timer > 1000) {
            timer += 1000;
            System.out.println("Fps: " + fps + " Ticks: " + ticks);
            fps = 0;
            ticks = 0;
        }
    }
    stop();
}

public void tick() {        
}

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs==null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.fillRect(36, 25, 25, 25);
    g.dispose();
    bs.show();
}

public Game() {
    setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    frame = new JFrame();
}

public static void main(String[] args) {
    Game game = new Game();

    game.frame.setResizable(false);
    game.frame.setTitle("SPACE ADAPT PRE-ALPHA 0.001");
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);

    game.start();
}
}

最佳答案

如果您从未将 running 设置为 true,那么它就是 false。作为与此问题无关的旁注,但大多数 swing 组件方法都不是线程安全的,因此在不是 Event Dispatch Thread 的另一个线程中调用不会按您的预期工作。

了解更多 Concurrency in Swing

关于java - Run 方法被调用,但不会绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054108/

相关文章:

运行 jar 文件时抛出 java.lang.NoClassDefFoundError

java - Tomcat 重新启动后 session JDBCStore 失败 java.lang.Class.forName NullPointerException - 但只有一分钟左右,然后工作

java - 我与 JPanel、getWidth 和 getHeight 作斗争

java - 如何在 Java 中创建一个用于创建文本字段的 GUI 按钮?

java - 无法重绘我的 JFrame/JPanel

java - getContentPane().add() 和 add() 的意思一样吗

java - 如何在 Java Swing 中按下按钮时关闭窗口并打开一个新窗口

java - 在 Eclipse 中按严重性/优先级对 PMD 报告的内容进行排序

java - 如何从java中的字符串中获取Good CRC16?

java - 如何检测 JTextField 是否为空?