【问题标题】:Illegal State Exception With Creating a Buffer Stragety创建缓冲区策略的非法状态异常
【发布时间】:2014-03-14 23:07:39
【问题描述】:

嗨,我正在编码,结果出现了

线程“Thread-2”java.lang.IllegalStateException 中的异常:组件必须有一个有效的对等体 在 java.awt.Component$FlipBufferStrategy.createBuffers(未知来源) 在 java.awt.Component$FlipBufferStrategy.(Unknown Source) 在 java.awt.Component$FlipSubRegionBufferStrategy.(Unknown Source) 在 java.awt.Component.createBufferStrategy(未知来源) 在 java.awt.Canvas.createBufferStrategy(未知来源) 在 java.awt.Component.createBufferStrategy(未知来源) 在 java.awt.Canvas.createBufferStrategy(未知来源) 在 spoderman.game.Main.render(Main.java:79) 在 spoderman.game.Main.run(Main.java:64) 在 java.lang.Thread.run(Unknown Source)

这是代码
我查明了错误是 createBufferStrategy(3);

请帮忙!!!

     package spoderman.game;

     import java.awt.Canvas;
     import java.awt.Dimension;
     import java.awt.Graphics;
     import java.awt.image.BufferStrategy;

     import javax.swing.JFrame;



    public class Main extends Canvas implements Runnable{


private static final long serialVersionUID = 8496269517740959648L;

public static JFrame frame = new JFrame();
public static Thread gameThread = new Thread();

public static final int WIDTH = 720, HEIGHT = 240, SCALE = 2;  

public static String title = "The Adventures of Spoderman";

public static boolean isrunning = false;

public synchronized void start(){
    if(isrunning)return;
        isrunning = true;
        gameThread = new Thread(this);
        gameThread.start();




}


public synchronized void stop(){
    if(!isrunning)return;

        try {
            gameThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isrunning = false;

}


public void run() {
    long lastTime = System.currentTimeMillis();
    final double amountOfTicks = 60D;
    double ns = 1000000000 / amountOfTicks;
            double delta = 0;
    while(isrunning){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if(delta >= 1){
            tick();
            delta--;
        }
        render();
    }
    stop();
}


public void tick(){

}


public void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){

     createBufferStrategy(3);


        return;
    }
    Graphics g = bs.getDrawGraphics();
    //ALL THAT IS RENDERED

    g.fillRect(0, 0, WIDTH, HEIGHT);
    //ALL THAT IS RENDERED
    g.dispose();
    bs.show();
}

public static void main (String [] args){
    Main main = new Main();
    main.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    main.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    main.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));

    main.start();
    JFrame();




}

public static void JFrame(){

frame.setTitle(title);
frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);


}

}

【问题讨论】:

    标签: java canvas illegalstateexception bufferstrategy


    【解决方案1】:

    在 Jframe 上试试这个

        public static void JFrame(Main main) {// <---get de arg game.
            frame.add(main);// add the main(canvas).
            frame.setTitle(title);
            frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    

    在主界面上编辑

        public static void main(String[] args) {
            Main main = new Main();
            main.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
            main.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
            main.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    
            JFrame(main);// get the Frame working and pas the main
    
            main.start();// start main
        }
    

    最后只是改变大小

            // g.fillRect(0, 0, WIDTH, HEIGHT);//The old one
            g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);// change the size
    

    完整代码

    package testCodeDel;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import javax.swing.JFrame;
    
    public class Main extends Canvas implements Runnable {
    
        private static final long serialVersionUID = 8496269517740959648L;
    
        public static JFrame frame = new JFrame();
        public static Thread gameThread = new Thread();
    
        public static final int WIDTH = 720, HEIGHT = 240, SCALE = 2;
    
        public static String title = "The Adventures of Spoderman";
    
        public static boolean isrunning = false;
    
        public synchronized void start() {
            if (isrunning)
            return;
    
            isrunning = true;
            gameThread = new Thread(this);
            gameThread.start();
    
        }
    
        public synchronized void stop() {
            if (!isrunning)
            return;
    
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            isrunning = false;
        }
    
        public void run() {
            long lastTime = System.currentTimeMillis();
            final double amountOfTicks = 60D;
            double ns = 1000000000 / amountOfTicks;
            double delta = 0;
            while (isrunning) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
    
            if (delta >= 1) {
                tick();
                delta--;
            }
            render();
            }
            stop();
        }
    
        public void tick() {
        }
    
            public void render() {
            BufferStrategy bs = this.getBufferStrategy();
    
            if (bs == null) {
                createBufferStrategy(3);
                return;
            }
    
            Graphics g = bs.getDrawGraphics();
            // ALL THAT IS RENDERED
            // g.fillRect(0, 0, WIDTH, HEIGHT);//The old one
            g.fillRect(0, 0, WIDTH * SCALE, HEIGHT * SCALE);// change the size
            // ALL THAT IS RENDERED
            g.dispose();
            bs.show();
        }
    
        public static void main(String[] args) {
            Main main = new Main();
            main.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
            main.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
            main.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    
            JFrame(main);// get the Frame working pas de main
    
            main.start();// start main
        }
    
        public static void JFrame(Main main) {// <---get de arg main.
            frame.add(main);// add the main(canvas).
            frame.setTitle(title);
            frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    或者只是删除 JFrame() 并在你的 main 上执行此操作

    public static void main(String[] args) {
        Main main = new Main();
        main.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        main.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
        main.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    
        //JFrame(main);// get the Frame working pas de main
        JFrame frame = new JFrame();
    
        frame.add(main);// add the main(canvas).
        frame.setTitle(title);
        frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    
        main.start();// start main
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2012-06-07
      • 2013-01-13
      相关资源
      最近更新 更多