任何人都可以帮助修复我的代码

时间:2014-01-08 01:46:04

标签: java macos

这些行有错误,bs.showg.dispose在此令牌后有错误语法错误标识符,g.drawImage部分表示删除令牌和 说这个令牌之后的语法错误decleratorid,我做错了什么

g.drawImage(img, 0, 0, WIDTH HEIGHT, null);
g.dispose();
bs.show();

这是完整的代码

package com.mime.WorldExplorer;


public static final int WIDTH = 800;
public static final int HEIGHT = 600; 
public static final String TITLE = "World explorer(Name not final) Pre-Alpha 0.0.1";
private static final String createBufferStrategy = null;

private Thread thread;
private BufferedImage img;
private boolean running = false;
private Render render;
private Screen screen;
private int[]pixels;
private int i;
private BufferStrategy bs;


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


private void start() { 
    if (running)
        return;
    running = true; 
    thread =  new Thread(this); }



    private void stop() {
        if(running)
            return; 
        running = false; 
        try {
        thread.join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);

        }


    }


    public void run() {
        while(running);
        tick();
        render();

        }

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



    }


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




private void tick() {


    }


public static void main(String[] args) {
    Display game = new Display();
    JFrame frame = new JFrame();
    frame.add(game);
    frame.pack();
    frame.setTitle(TITLE);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(WIDTH, HEIGHT);
    frame.setResizable(true);
    frame.setVisible(true);

    System.out.println("Running....");

    System.out.println("Working!");

    game.start();


}


}

1 个答案:

答案 0 :(得分:0)

我不知道OSX代码 - 但是,看起来你有这个:

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

..坐在任何方法之外 - 它似乎没有任何东西包裹......

我建议你从顶部开始,确保你有匹配的{}大括号 - 也就是说你有一个开放的{你也有一个关闭一个} - 当您确定没有结束(或开始)的那个时,您将在代码中找到错误。

我认为

你需要改变这个:

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



    }


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

To This - 注意它现在包含在{ } ok:)

再次 - 我不知道OSX,虽然下面的语法看起来正确

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

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

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

} // added this here