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 回答