Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试将我的屏幕颜色更改为黑色_Java_Swing_Netbeans_Jframe_Components - Fatal编程技术网

Java 正在尝试将我的屏幕颜色更改为黑色

Java 正在尝试将我的屏幕颜色更改为黑色,java,swing,netbeans,jframe,components,Java,Swing,Netbeans,Jframe,Components,试图将我的屏幕颜色更改为黑色,但我有一个非法状态选项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) a

试图将我的屏幕颜色更改为黑色,但我有一个非法状态选项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();
    }

}
性能等级:

/*
 * 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();
    }

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

你的代码乱七八糟

您有一个名为Window的类,它扩展了Canvas,但实际上从未添加到任何内容或在任何情况下使用。不要使用窗口或框架之类的名称,这些类已经存在 实际上执行渲染的性能从未添加到JFrame win中,但即使添加了,您也会在TestRun中创建另一个实例,因此您会尝试在屏幕外绘制一些内容 首先在窗口中创建性能的实例变量

公共类窗口{//这不起作用{

private static final long serialVersionUID = -1L;

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

public JFrame win;
private Performance performance;
更改窗口方法,使其成为构造函数,创建性能实例,并将其添加到win中

为win中的性能创建一个getter

删除

TestRun test = new TestRun();
test.start();
从窗口的构造函数

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

更改TestRun的run方法以使用Windows中的性能实例,您继承了该实例

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

    }
}
也许,只是也许,如果程序神是善良的,它将运行

可能重复的
public static void main(String... args) {

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

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

    }
}