0

私はそのようなコードを持っています:

private int[] pixels;
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
    createBufferStrategy(3);
    return;
}

コード例の 2 行目でエラーを返します。

Syntax error on token ; { expected after this token.

}そして、コード例の最後の行 ( ) でエラーを返します:

Insert } to complete block.

キャンバスを使用するのはこれが初めてで、これらの厄介なエラーが発生するとびっくりし
ますが、このエラーは次のレベルに私を驚かせます..

メインクラスの完全なコード:

package game.display;

import game.display.graphics.Render;
import game.display.graphics.Screen;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;

public class Display extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static final String TITLE = "Ultimate Game | Pre alpha 0.01";

private Thread thread;
private Boolean isRunning = false;

private Render render;
private Screen screen;
private BufferedImage img;
private int[] pixels;
BufferStrategy bs = this.getBufferStrategy();;
if(bs == null) {
    createBufferStrategy(3);
    return;
}

public Display() {
    screen = new Screen(WIDTH, HEIGHT);
    img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}

private void start() {
    if (isRunning)
        return;

    isRunning = true;
    thread = new Thread(this);

    System.out.println("Initialize the thread game!");

    thread.start();

    System.out.println("The thread has initialized!");

    System.out.println("The game has started!");
}

private void stop() {
    if (!isRunning)
        return;

    isRunning = false;

    System.out.println("The game stopped!");

    System.out.println("Stopping the thred!");
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
        System.exit(0);
    }
}

public void run() {
    while (isRunning) {
        System.out.println("The Game is Running!");
        tick();
        render();
    }
}

private void tick() {

}

private void render() {
    bs = this.getBufferStrategy();

    screen.render();

    for(int i = 0; i < WIDTH * HEIGHT; i++) {
        pixels[i] = screen.pixels[i];
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();
}

public static void main(String[] args) {

    System.out.println("The program called!");
    System.out.println("Initialize the display and the JFrame!");

    Display game = new Display();
    JFrame frame = new JFrame();
    frame.add(game);
    frame.pack();
    frame.setTitle(TITLE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    System.out.println("Starting the game!");
    game.start();

}
}
4

1 に答える 1

2

コードはこのようなメソッドにある必要があります。

void method(){
    BufferStrategy bs = this.getBufferStrategy();;
    if(bs == null) {
        createBufferStrategy(3);
        return;
    }
}
于 2013-09-14T12:11:27.037 に答える