有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java创建缓冲策略(3);不起作用

我有这样的代码:

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.

这是我第一次使用canvas,当我遇到这些恼人的错误时,我会发疯,
但是这个错误把我吓到了下一个层次

主类的完整代码:

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();

}
}

共 (1) 个答案

  1. # 1 楼答案

    代码应该是这样一种方法

    void method(){
        BufferStrategy bs = this.getBufferStrategy();;
        if(bs == null) {
            createBufferStrategy(3);
            return;
        }
    }