【问题标题】:Buffer Strategy IllegalStateException缓冲策略 IllegalStateException
【发布时间】:2015-06-30 21:53:37
【问题描述】:

我知道以前有人问过这个问题,但我仍然无法让它工作。

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,在那里我尝试制​​定缓冲策略。我想我应该先制作框架,但我确实在制作制作缓冲区策略的线程之前调用它。

【问题讨论】:

    标签: java swing bufferstrategy


    【解决方案1】:

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

    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,但此实例在屏幕上不可见(可显示,或附加到本机对等方),因此您的问题...

    相反,摆脱GUI 中的main 方法,它并没有真正为您带来任何好处,并将其逻辑应用到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) {
            }
        }
        //...
    

    您还应该阅读BufferStrategy 上的 JavaDocs 以更好地了解如何管理它们...

    【讨论】:

    • 好的,谢谢,现在窗口真的弹出了。但它没有画线。这就是我的循环: for loop(shortened to fit) graphics = (Graphics2D) bufferStrategy.getDrawGraphics(); graphics.setBackground(Color.WHITE); graphics.setColor(Color.BLACK); graphics.drawLine(100, 100, (int) (Math.random() * ((200-50) + 1) + 50), (int) (Math.random() * ((200-50) + 1) + 50)); graphics.dispose(); bufferStrategy.show();尝试 { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } 无限变量 = 0; }
    • 我在想,JRootPane 正在阻挡画在框架上的东西。考虑使用java.awt.Canvas 渲染并将其添加到您的框架中
    猜你喜欢
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 2018-03-11
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多