2014-01-08 108 views
-6

这些线路对他们的错误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

请 - 尝试并在您的问题中键入一些适当的英语。我不知道什么是错误信息,什么是问题。 – 2014-01-08 01:48:23

+0

第一步将修复您的缩进。你的代码现在很难阅读。另外,请发布您收到的任何编译器错误的完整文本,并指出每个引用的哪一行。 –

+0

没有办法完整的源代码,没有类声明... – MadProgrammer

回答

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

为了这一点 - 注意到它现在包裹在{} 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 
+0

是的,你们解决了很多谢谢:) – Gamer300090

+0

不客气:)当你得到机会时,请标记为已回答 – Darren

+0

这听起来很愚蠢,但怎么回事? – Gamer300090