2014-01-08 112 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