为什么我的日食没有设置我的背景颜色?

Why is my eclipse not setting my background color?

我设计了一个游戏代码。问题是背景不会更改为我从图形颜色库中选择的任何颜色。

我需要有人用我提供的代码解决这个问题(请不要制作全新的代码)。 idk 为什么 java/ eclipse 不显示它???我错过了什么吗??这里的程序应该显示一个背景颜色为蓝色的 GUI。相反,我变白了。

public class MainApp extends Canvas implements Runnable {

    private static final long serialVersionUID = 8928635543572788908L;

    private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
    private Thread thread;
    private boolean running= false;

    public MainApp()
    {
        new Window(WIDTH, HEIGHT, "App", this);
    }
    public synchronized void start()
    {
        thread= new Thread(this);
        thread.start();
        running= true;
    }
    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.print("FPS: " + frames);
                frames= 0;
            }
        }
        stop();
    }

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

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

        Graphics g= bs.getDrawGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();
        bs.show();
    }
    public static void main(String[] args) {
        new MainApp();
    }
}

你的代码有点乱,你不应该从 MainApp 中创建 Window 的新实例,Window 应该创建它(恕我直言)。

此外,您应该覆盖 MainAppgetPreferredSize 方法,因为这是控制 window 可见大小的方法,这样,当您使用pack 在 JFrame 上,它将确保 window 大于 preferredSize 的内容,允许框架装饰环绕它的外部。

但是,您遇到的主要问题是在 JFrame 已经可见后将 MainApp 添加到 JFrame

以下适合我...

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 MainApp extends Canvas implements Runnable {

    private static final long serialVersionUID = 8928635543572788908L;

    private static final int WIDTH = 648, HEIGHT = WIDTH / 12 * 9;
    private Thread thread;
    private boolean running = false;

    public MainApp() {
        new Window("App", this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(WIDTH, HEIGHT);
    }

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

    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.print("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }

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

    public void tick() {

    }

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

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.dispose();
        bs.show();
    }

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

    public static class Window {

        private Window(String title, MainApp app) {
            JFrame frame = new JFrame(title);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(app);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
            app.start();
        }

    }

}