2015-07-03 157 views
-1

試圖將我的屏幕顏色更改爲黑色,但我有一個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(); 
    } 

} 

如果有誰知道什麼可能是錯誤的,請幫幫我,只要你能。

+1

可能重複(http://stackoverflow.com/questions/10866269/illegal -state-異常時,創造新-BufferStrategy中) – yfklon

回答

3

您的代碼亂七八糟。

  • 你有一個叫Window類延伸Canvas,但從來沒有真正加入到任何東西,或以任何使用。不要使用名字,如WindowFrame,這些類已經存在
  • Performance其實際執行渲染永遠不會添加到您的JFramewin),但即使是這樣,您在TestRun創建它的另一個實例,所以你嘗試和Window

    公共類窗口油漆的東西是關閉屏幕

開始通過創建Performance實例變量{//這是無爲延伸帆布{

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

protected Performance getPerformance() { 
    return performance; 
} 

刪除創建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方法使用performance實例從Window,您繼承...

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

    } 
} 

也許,只是也許,如果程序神是善良,它將[創建新BufferStrategy使用時非法狀態異常]運行