java - 创建缓冲区策略(3);不工作

标签 java canvas buffer bufferedimage

我有这样的代码:

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

它在代码示例中的第 2 行返回错误:

token 语法错误; { 预计在此标记之后。

并在代码示例中的最后一行 (}) 返回错误:

插入}来完成 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();

}
}

最佳答案

代码应该在这样的方法中。

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

关于java - 创建缓冲区策略(3);不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801550/

相关文章:

java - 该组件未出现在西边框布局中

javascript - HTML5 如何让 canvas 不拉伸(stretch)页面?

c# - Websockets 将 byte[] 转换为字符串

node.js - NODEJS + WS 库 = 缓冲区分配错误

ruby-on-rails - 发送通过 HTTParty 发送的帖子查询

java - 是否可以在 Xamarin 中检索存储在 Java 中的 Android 共享首选项

java - 任何服务器的最大连接超时是多少?

java - 如何避免重复?

javascript - 如何居中 FabricJS Canvas 背景图像?

javascript - 在 HTML5 Canvas 上使用 drawImage 调整颜色