Java 创建缓冲区策略时出现非法状态异常

Java 创建缓冲区策略时出现非法状态异常,java,canvas,illegalstateexception,bufferstrategy,Java,Canvas,Illegalstateexception,Bufferstrategy,嗨,我在编代码,这个出现了 线程“thread-2”java.lang.IllegalStateException中的异常:组件必须具有有效的对等方 位于java.awt.Component$FlipBufferStrategy.createBuffers(未知源) 位于java.awt.Component$FlipBufferStrategy。(未知来源) 位于java.awt.Component$FlipSubcionBufferStrategy。(未知源) 位于java.awt.Compo

嗨,我在编代码,这个出现了

线程“thread-2”java.lang.IllegalStateException中的异常:组件必须具有有效的对等方 位于java.awt.Component$FlipBufferStrategy.createBuffers(未知源) 位于java.awt.Component$FlipBufferStrategy。(未知来源) 位于java.awt.Component$FlipSubcionBufferStrategy。(未知源) 位于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(未知源)

这是代码
我指出了错误,这是我的策略(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);


}
}

在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);
    }
最后,改变一下尺寸

        // 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);
    }
}
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);
    }
}
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
}