嗨我正在编码,这就出现了
线程“Thread-2”中的异常java.lang.IllegalStateException:组件必须具有有效的对等方 at java.awt.Component $ FlipBufferStrategy.createBuffers(Unknown Source) at java.awt.Component $ FlipBufferStrategy。(Unknown Source) at java.awt.Component $ FlipSubRegionBufferStrategy。(Unknown Source) at java.awt.Component.createBufferStrategy(Unknown Source) at java.awt.Canvas.createBufferStrategy(Unknown Source) at java.awt.Component.createBufferStrategy(Unknown Source) at java.awt.Canvas.createBufferStrategy(Unknown Source) 在spoderman.game.Main.render(Main.java:79) 在spoderman.game.Main.run(Main.java:64) 在java.lang.Thread.run(未知来源)
这是守则
我确定了错误,即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);
}
}
答案 0 :(得分:0)
在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()并在主
上执行此操作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
}