Java 游戏,颜色没有出现

Java game, colors don't apear

我有一个名为 Wave 的游戏的代码,通常当我 运行 它时,它应该是一个黑色 window 里面有白色方块。但是 window 是白色的,在 window 的左边有一条很细的黑色条纹。我几乎看不到它。

有人知道为什么会发生这种情况吗?

package wave.myFirstGame;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

public class Game extends Canvas implements Runnable {
    private static final long serialVersionUID = 3580879553502102315L;
    public static final int WITDH = 640, HEIGHT = WITDH / 12 * 9;

    private Thread thread;
    private boolean running = false;

    private Random r;
    public Handler handler;

    public Game() {
        new Window(WITDH, HEIGHT, "Wave", this);

        handler = new Handler();
        r = new Random();

        for(int i = 0; i < 50; i++){
            handler.addObject(new Player(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.Player));
        }

        handler.addObject(new Player(200, 200, ID.Player));
    }

    public synchronized void start() {// initializing the thread
        thread = new Thread(this);
        thread.start();
        running = true;
    }

    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // GAME LOOP
    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames = 0;
        while (running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while (delta >= 1) {
                tick();
                delta--;
            }
            if (running)
                render();
            frames++;

            if (System.currentTimeMillis() - timer > 1000) {
                timer += 1000;
                System.out.println("FPS " + frames);
                frames = 0;
            }
        }
        stop();
    }

    private void tick(){
        handler.tick();
    }
    private void render(){
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null){
            this.createBufferStrategy(3);
            return;
        }

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.black);

        g.fillRect(0, 0, WIDTH, HEIGHT);
        handler.render(g);

        g.dispose();
        bs.show();
    }

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

查看 Canvas 的 API。在那里你会发现:

Fields inherited from interface java.awt.image.ImageObserver
[...] HEIGHT, [...], WIDTH

因此,由于您从 Canvas class 扩展了 class,您已经有了 WIDTH 和 HEIGHT 常量,并且出于某种原因,WIDTH 接缝的值是 1
因此,只需重命名您的常量,它就会按预期显示。