【问题标题】:createBufferStrategy(3); not working创建缓冲区策略(3);不工作
【发布时间】:2013-09-14 12:01:41
【问题描述】:

我有这样的代码:

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

}
}

【问题讨论】:

  • 给出你的完整代码而不是部分代码
  • 好吧,你问了 :) 我会在一秒钟内更新

标签: java canvas buffer bufferedimage


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多