尝试将屏幕颜色更改为黑色,但我有一个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个不同的类,其中一个引用其他三个类,类名是&#34; TestRun&#34;:
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() {
}
}
最后,错误开始的目的地; Performance类:
表演课程:
/*
* 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();
}
}
如果有人知道可能出现的问题,请尽快帮助我。
答案 0 :(得分:3)
你的代码一团糟。
Window
的类,它扩展了Canvas
,但实际上从未添加到任何内容或用于任何方面。请勿使用Window
或Frame
等名称,这些类已存在Performance
永远不会添加到您的JFrame
(win
),但即使您这样做,也会在TestRun
中创建另一个实例,所以你试着画一些不在屏幕上的东西首先在Performance
Window
的实例变量
public class Window {//这无所事事扩展了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);
在performance
win
创建一个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();
}
更改run
的{{1}}方法,以使用您已继承的TestRun
performance
个实例...
Window
也许,也许,只是也许,如果程序之神是善良的,它将会运行