【问题标题】:Why is my eclipse not setting my background color?为什么我的日食没有设置我的背景颜色?
【发布时间】:2015-12-27 03:28:19
【问题描述】:

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

我需要有人用我提供的代码来解决这个问题(请不要制作全新的代码)。我知道为什么 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();
    }
}

【问题讨论】:

  • 什么时候打电话给start
  • Start 在我的 Window 类中,我一开始在构造函数中调用它..
  • 新窗口(参数)当我称之为我的 app.start();开始运行。
  • 好的,请确保您没有收到 IllegalStateException 组件未附加到本机对等方(即,在您尝试创建 BufferStrategy 之前窗口不可见),因为这是你的代码在我开始工作时所做的第一件事,但我只需稍作调整就能让它安全运行
  • 我没有收到任何错误。我的 FPS 工作正常,只是我的背景不会显示。你是怎么让它工作的?

标签: java awt


【解决方案1】:

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

此外,您应该覆盖getPreferredSize 方法和MainApp,因为这应该控制窗口的可视大小,这样,当您在JFrame 上使用pack 时,它将确保窗口大于其内容的preferredSize,允许框架装饰环绕它的外部。

但是,您遇到的主要问题是将 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();
        }

    }

}

【讨论】:

  • 谢谢你 MadProgrammer 但你能再解释一下发生了什么.. 我有点慢?我的主要错误是什么?是主要修复吗?非常感谢。我之前做过这段代码,它对我有用,但我重新输入它来练习,但它没有用。
  • 还有为什么我在启动时出现错误然后消失了?
  • 我不知道这个错误,但你的线程和窗口之间可能存在竞争条件变得可见。主要问题是在框架可见后将“应用程序”添加到框架中,这意味着组件布局不正确,这是一个非常常见的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-09
  • 2011-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多