为什么我的屏幕不变红?

好的,所以我是编程的新手,我正在关注Youtube上的教程来构建我自己的游戏。 我的问题是我的屏幕没有变红,只是保持灰色。 我确定我做错了什么但是日食没有错误。 这是代码:

import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import javax.swing.JFrame; public class Game extends Canvas implements Runnable { private static final long serialVersionUID = 1L; public static int width = 300; public static int height = width / 16 * 9; public static int scale = 3; private Thread thread; private JFrame frame; private boolean running = false; public Game() { Dimension size = new Dimension(width * scale, height * scale); setPreferredSize(size); frame = new JFrame(); } public synchronized void start() { running = true; thread = new Thread("Display"); thread.start(); } public synchronized void stop() { running = false; try { thread.join(); } catch(InterruptedException e) { e.printStackTrace(); } } public void run() { while (running) { update(); render(); } } public void update() { } public void render() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, getWidth(), getHeight()); g.dispose(); bs.show(); } public static void main(String[] args){ Game game = new Game(); game.frame.setResizable(false); game.frame.setTitle("JJF"); game.frame.add(game); game.frame.pack(); game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.setLocationRelativeTo(null); game.frame.setVisible(true); game.start(); } } 

你永远不会真正为Thread提供运行的东西……

 public synchronized void start() { running = true; // Not the reference to this... thread = new Thread(this, "Display"); thread.start(); } 

通过传递(这是实现Runnable Game类的实例), Thread将能够调用您的run方法

注意:

可视区域的大小应由组件定义,而不是框架。 这可以通过重写getPreferredSize方法并返回您希望组件的首选可视大小来实现。 否则,可视区域将是框架的大小减去它的装饰内容,这可能不符合您的期望。

在你的“游戏循环”中你应该考虑在循环之间有一个小的延迟,以给系统实际更新屏幕的时间,这需要Thread一些压力

可运行的例子

缓冲

 import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; public class Game extends Canvas implements Runnable { private static final long serialVersionUID = 1L; public static int width = 300; public static int height = width / 16 * 9; public static int scale = 3; private Thread thread; private JFrame frame; private boolean running = false; public Game() { Dimension size = new Dimension(width * scale, height * scale); setPreferredSize(size); frame = new JFrame(); } public synchronized void start() { running = true; thread = new Thread(this, "Display"); thread.start(); } public synchronized void stop() { running = false; try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } public void run() { while (running) { update(); render(); // try { // Thread.sleep(40); // } catch (InterruptedException ex) { // } } } public void update() { } public void render() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, getWidth(), getHeight()); g.dispose(); bs.show(); } public static void main(String[] args) { Game game = new Game(); game.frame.setResizable(false); game.frame.setTitle("JJF"); game.frame.add(game); game.frame.pack(); game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.frame.setLocationRelativeTo(null); game.frame.setVisible(true); game.start(); } }