試圖將我的屏幕顏色更改爲黑色,但我有一個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」:
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();
}
}
如果有誰知道什麼可能是錯誤的,請幫幫我,只要你能。
可能重複(http://stackoverflow.com/questions/10866269/illegal -state-異常時,創造新-BufferStrategy中) – yfklon