2015-04-23 79 views
2

我知道这已经被问过,但我仍然无法让它工作。缓冲区策略IllegalStateException

public class GUI extends JFrame implements Runnable{ 

    public static JPanel contentPane; 
    public static Graphics2D graphics; 
    public static BufferStrategy bufferStrategy; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        GUI frame = new GUI(); 
        frame.setVisible(true); 
        frame.setResizable(false); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public GUI() { 
     setResizable(false); 
     setTitle("Tower Defense Game"); 
     setIgnoreRepaint(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     contentPane.setLayout(new BorderLayout(0, 0)); 
     setContentPane(contentPane); 


    } 

    @Override 
    public void run() { 

     createBufferStrategy(2); 
     bufferStrategy = getBufferStrategy(); 
     graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); 

     for(int infiniteVar = 0; infiniteVar == -1; infiniteVar++){ 

      graphics.setBackground(Color.WHITE); 
      graphics.drawLine(100, 100, (int) (Math.random() * ((200-50) + 1) + 50), (int) (Math.random() * ((200-50) + 1) + 50)); 

      try { 
       Thread.sleep(10); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      infiniteVar = 0; 
     } 
    } 
} 
public class Initialize { 

public static void main(String[] args){ 

    GUI.main(args); 

    GUI objGUI = new GUI(); 
    Thread threadGUI = new Thread(objGUI); 
    threadGUI.start(); 
} 
} 

我得到Exception in thread "Thread-2" java.lang.IllegalStateException: Component must have a valid peer on the line我试图制定缓冲策略。我认为我应该首先制定框架,但是在制定制定缓冲策略的线程之前,我确实会这样做。

回答

2

基本上,你的问题从这里开始......

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       GUI frame = new GUI(); 
       frame.setVisible(true); 
       frame.setResizable(false); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

,并在这里被激怒......

public class Initialize { 

    public static void main(String[] args) { 

     GUI.main(args); 

     GUI objGUI = new GUI(); 
     Thread threadGUI = new Thread(objGUI); 
     threadGUI.start(); 
    } 
} 

基本上,发生了什么事时,GUI.main方法是创建的一个新实例GUI,它显示在屏幕上,然后创建另一个GUI实例...

GUI objGUI = new GUI(); 
Thread threadGUI = new Thread(objGUI); 
threadGUI.start(); 

到您尝试使用创建BufferStrategy用,但这种情况下是不可见的屏幕上(可显示或连接到本地同行),因此您的问题...

相反,摆脱在GUImain方法,它没有真正做任何好处,为您和运用它的逻辑在Initialize

GUI frame = new GUI(); 
// Better idea to do this before you make the frame visible 
// as it can change the frame borders and cause some issues 
frame.setResizable(false); 
frame.setVisible(true); 

Thread thread = new Thread(frame); 
thread.start(); 

你也可以在你的run方法添加检查要等到JFrame成为显示...

@Override 
public void run() { 

    while (!isDisplayable()) { 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException ex) { 
     } 
    } 
    //... 

你也应该阅读的JavaDoc上BufferStrategy更好地了解如何管理它们......

+0

好感谢,现在居然窗口弹出。但它并没有画出线条。这就是我对循环: for循环(缩短,以适应) \t \t \t \t \t \t显卡=(Graphics2D的)bufferStrategy.getDrawGraphics(); \t \t \t \t \t \t graphics.setBackground(Color.WHITE); \t \t \t graphics.setColor(Color.BLACK); (Math.random()*((200-50)+ 1)+ 50),(int)(Math.random()*((200-50))。 )+ 1)+50)); \t \t \t \t \t \t graphics.dispose(); \t \t \t bufferStrategy.show(); \t \t \t \t \t \t尝试{ \t \t \t \t主题。睡眠(10); \t \t \t}赶上(InterruptedException的发送){ \t \t \t \t e.printStackTrace(); \t \t \t} \t \t \t \t \t \t infiniteVar = 0; \t \t} – Bloodwing

+0

我在想,'JRootPane'阻止了什么被绘制到了框架上。考虑使用'java.awt.Canvas'来渲染并将其添加到您的框架 – MadProgrammer