试图将我的屏幕颜色更改为黑色

Posted

技术标签:

【中文标题】试图将我的屏幕颜色更改为黑色【英文标题】:Trying to change the color of my screen to black 【发布时间】:2015-09-20 21:33:00 【问题描述】:

,但我有一个 IllegalStateExeption b/c,它说组件必须是我正在使用 NetBeans 的有效对等体。

这里是详细版本:

Exception in thread "Window" java.lang.IllegalStateException: Component must have a valid peer
    at java.awt.Component$FlipBufferStrategy.createBuffers(Component.java:3998)
    at java.awt.Component$FlipBufferStrategy.<init>(Component.java:3972)
    at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Component.java:4495)
    at java.awt.Component.createBufferStrategy(Component.java:3849)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:194)
    at java.awt.Component.createBufferStrategy(Component.java:3773)
    at java.awt.Canvas.createBufferStrategy(Canvas.java:169)
    at com.gamestormjr.Performance.render(Performance.java:34)
    at com.gamestormjr.main.TestRun.run(TestRun.java:72)
    at java.lang.Thread.run(Thread.java:745)

这是我的代码;我有 4 个不同的类,其中一个引用了其他三个,类名是“TestRun”:

测试运行:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gamestormjr.main;

import com.gamestormjr.Performance;
import com.gamestormjr.Performance;
import com.gamestormjr.Window;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author amani
 */
public class TestRun extends Window implements Runnable 

    public static void main(String... args) 

        System.out.println("Starting...");
        Window win = new Window();
        win.Window();

    

    /**
     * Thread that helps handle the game being produced.
     */
    private Thread thread;
    private boolean running = false;

    public synchronized void start() 
        running = true;
        thread = new Thread(this);
        /**
         * What this will do is, it will create a separate thread that handles
         * the Updates.
         * <> This is really good for making games more efficient.
         */
        thread = new Thread(this, "Window");
        /**
         * What this will do is, it will create a new thread that will handle
         * the Rendering.
         * <> This is really good for making games more efficient.
         */
//        thread = new Thread(this, "Render");
        thread.start();
    

    /**
     * To help manage Threads.
     */
    public synchronized void stop() 
        running = false;
        try 
            thread.join();

         catch (InterruptedException ex) 
            Logger.getLogger(TestRun.class
                    .getName()).log(Level.SEVERE, null, ex);
        

    

    public void run() 

        while (running) 
            Time time = new Time();
            Performance render = new Performance();
            render.render();

        
    


窗口类:

/**
 * <Javadoc Codes>
 * The Declaration for my Project; I this case it's for my game.
 *
 */
package com.gamestormjr;

import com.gamestormjr.main.TestRun;
import java.awt.*;
import javax.swing.JFrame;

/**
 * <Javadoc Codes>
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates and open the template
 * in the editor.
 *
 *
 *
 * @author amani
 */
public class Window extends Canvas 

    private static final long serialVersionUID = -1L;

    public static int width = 800;
    public static int height = 600;

    public JFrame win;

    /**
     * <Javadoc Codes>
     * <html>
     * <b> Input -- <font color="blue">
     * public
     * </font>
     * ClassName()
     * </b>
     * <p>
     * What this method does is that it tells JVM that it's ready to be called
     * upon.
     * </p>
     *
     * @param DataType <font color="blue">
     * public, private, static, void, protected, boolean
     * </font>
     * @param Method ClassName
     * </html>
     */
    public void Window() 
        System.out.println("Started...");
        Window window = new Window();
        win = new JFrame();

        win.setResizable(false);
        win.setTitle("Game");
        win.setSize(800, 500);
        win.setLocationRelativeTo(null);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);

        TestRun test = new TestRun();
        test.start();
    


时间类;我不认为这是问题所在,只有一行代码 但是还是

测试类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gamestormjr.main;

/**
 *<JavaDoc Codes>
public class Time 

    /**
     * Data type long ass variable "SECOND" : is .equal() = 1000MILISECONDS
     * (ms); which is equal() = 1SECOND.
     */
    public static long SECOND = 1000;

    public void update() 
    


最后,错误开始的目的地; 性能类:

性能等级:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gamestormjr;

import java.awt.*;
import java.awt.image.BufferStrategy;

/**
 *
 * @author amani
 */
public class Performance extends Canvas 

    public void render() 

        /**
         * <b>This creates a temporary storage </b>
         * <font color="blue"> OpenGL </font color="blue">. is a Great example
         * of this. What this does is that it creates and stores frame before
         * they're ready to be showed.
         */
        BufferStrategy bs = getBufferStrategy();
        // We only want to do this once so...
        if (bs == null) 
            /*
            What this does is it tells the BufferStategy(), to buffer "n" number of rendering.
            This is Short for Speed Improvement.
            Higher # = More frames Stored and ready to be placed.
            Lower # = Less frames Stored and  ready to be placed.
            */
            createBufferStrategy(3);
            return;
        

        Graphics g = bs.getDrawGraphics();
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
        /*
        This removes finished/un-used graphics.
        Buffer swapping AKA(Blitting)
        */
        g.dispose();
        /*
        This makes the next available buffer available.
        */
        bs.show();
    


如果有人知道可能出了什么问题,请尽快帮助我。

【问题讨论】:

Illegal State Exception when creating new Bufferstrategy的可能重复 【参考方案1】:

你的代码一团糟。

您有一个名为Window 的类,它扩展了Canvas,但实际上从未添加到任何东西或以任何方式使用。不要使用 WindowFrame 这样的名称,这些类已经存在 实际进行渲染的Performance 永远不会添加到您的JFrame (win),但即使您这样做了,您也会在TestRun 中创建它的另一个实例,因此您尝试绘制屏幕外的东西

首先在Window中创建Performance的实例变量

public class Window // 这什么都不做 extends Canvas

private static final long serialVersionUID = -1L;

public static int width = 800;
public static int height = 600;

public JFrame win;
private Performance performance;

更改您的Window 方法并使其成为构造函数并创建Performance 的实例并将其添加到win

public Window() 
    System.out.println("Started...");
    win = new JFrame();
    performance = new Performance();
    win.add(performance);

win 中为performance 创建一个getter

protected Performance getPerformance() 
    return performance;

删除...

TestRun test = new TestRun();
test.start();

来自Window的构造函数

更改您的main 方法以创建TestRun 的实例而不是Window...

public static void main(String... args) 

    System.out.println("Starting...");
    //Window win = new Window();
    //win.Window();
    TestRun test = new TestRun();
    test.start();


TestRunrun 方法更改为使用来自Windowperformance 实例,您已继承...

public void run() 
    while (running) 
        Time time = new Time();
        getPerformance().render();

    

也许,只是也许,如果程序大神好心,它会运行

【讨论】:

以上是关于试图将我的屏幕颜色更改为黑色的主要内容,如果未能解决你的问题,请参考以下文章

如何将所有文本颜色更改为默认颜色(文本为黑色,提示为灰色)而不是白色

如何在颤动中将状态栏图标和文本颜色更改为黑色?

将按钮的文本颜色从黑色更改为白色,然后恢复

无法将状态栏颜色更改为半透明黑色 [重复]

如何将 UiSearchBar 的背景颜色更改为黑色-SWIFT

状态栏在全屏对话框片段android中将其颜色更改为黑色