2015-04-27 167 views
0

我正在嘗試設置我想要創建的此遊戲的尺寸。 (運行)時出錯:;我去了一個小時,但我似乎無法趕上錯誤。我也使用NetbeansIDE setPreserredSize(new Dimension(WIDTH,HEIGHT));給我一個錯誤,它要求創建方法「setPrefferedSize(java.awt.Dimension中)」 in.com.francesc.game.Game遊戲Applet未在2D遊戲中運行(while循環)

private void setPreserredSize(Dimension dimension) { 
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
} 

當我運行它,我得到這個錯誤

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of type 
at com.francescstudio.game.Game.<init>(Game.java:67) 
at com.francescstudio.game.Start.main(Start.java:16) 
Java Result: 1 

繼承人所有的代碼

import java.awt.Color; 
    import java.awt.Dimension; 
    import java.awt.Font; 
    import java.awt.Graphics2D; 
    import java.awt.event.KeyEvent; 
    import java.awt.event.KeyListener; 
    import java.awt.image.BufferedImage; 

    import javax.swing.JPanel; 

    /** 
    * 
    * @author francesc 
    */ 

    public class Game extends JPanel implements KeyListener, Runnable { 

    public static final long SerialVersionUID = 1L; 
    public static final int WIDTH = 400; 
    public static final int HEIGHT = 630; 
    public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28); 
    private Thread game; 
    private boolean running; 
    private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 

    private long startTime; 
    private long elapse; 
    private boolean set; 

    public Game() { 
     setFocusable(true); 
     setPreserredSize (new Dimension(WIDTH, HEIGHT)); 
     addKeyListener(this); 
    } 

    private void update() { 
    } 

    private void render() { 
     Graphics2D g = (Graphics2D) Image.getGraphics(); 
     g.setColor(Color.white); 
     g.fillRect(0, 0, WIDTH, HEIGHT); 
     // render board 
     g.dispose(); 

     Graphics2D g2d = (Graphics2D) getGraphics(); 
     g2d.drawImage(Image, 0, 0, null); 
     g2d.dispose(); 

    } 

    @Override 
    public void run() { 
    } 
    int fps = 0, update = 0; 
    long fpsTimer = System.currentTimeMillis(); 
    double nsPerUpdate = 1000000000.0/60; 
    // last update time in nano seconds 
    double then = System.nanoTime(); 
    double unprocessed = 0; 

    while (running){ 

    boolean shouldRender = false; 
     double now = System.nanoTime(); 
     unprocessed += (now - then)/nsPerUpdate; 
     then = now; 


     // update queque 
     while (unprocessed >= 1) { 
      update++; 
      update(); 
      unprocessed--; 
      shouldRender = true; 
     } 

     // render 
     if (shouldRender) { 
      fps++; 
      render(); 
      shouldRender = false; 
     } else { 
      try { 
       Thread.sleep(1); 
      } catch (Exception e) { 
       e.printStackTrace(); 

      } 
     } 
    } 

    public synchronized start(){ 
     if(running)return; 
     running = true; 
     game = new Thread (this, "game"); 
     game.start(); 

    } 


    public synchronized stop(){ 
     if(!running) return; 
     running = false; 
     System.exit(0); 
    } 

    @Override 
     public void keyTyped(KeyEvent e) { 

    } 

    @Override 
     public void keyPressed(KeyEvent e) { 

    } 

    @Override 
     public void keyReleased(KeyEvent e) { 

    } 
    } 
+0

不要嘗試運行的源代碼,直到它編譯乾淨!順便說一句 - 爲什麼編寫一個小程序?如果是由於老師指定它,請將它們轉介給[爲什麼CS教師應該**停止**教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop教學-java的小應用程序/)。 –

回答

0

你的問題是while (running) {}不是方法的裏面,你在課堂,它永遠不會被調用定義它。

編輯: 您的代碼看起來應該多一點這樣的

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.image.BufferedImage; 

import javax.swing.JPanel; 

/** 
* 
* @author francesc 
*/ 

public class Game extends JPanel implements KeyListener, Runnable { 
    public static final long SerialVersionUID = 1L; 
    public static final int WIDTH = 400; 
    public static final int HEIGHT = 630; 
    public static final Font main = new Font("Bebas Nue Regular", Font.PLAIN, 28); 
    private Thread game; 
    private boolean running; 
    private BufferedImage Image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 

    private long startTime; 
    private long elapse; 
    private boolean set; 

    public Game() { 
     setFocusable(true); 
     setPreferredSize(new Dimension(WIDTH, HEIGHT)); 
     addKeyListener(this); 
    } 
    private void update() {} 
    private void render() { 
     Graphics2D g = (Graphics2D) Image.getGraphics(); 
     g.setColor(Color.white); 
     g.fillRect(0, 0, WIDTH, HEIGHT); 
     // render board 
     g.dispose(); 

     Graphics2D g2d = (Graphics2D) getGraphics(); 
     g2d.drawImage(Image, 0, 0, null); 
     g2d.dispose(); 

    } 
    @Override 
    public void run() { 
     int fps = 0, update = 0; 
     long fpsTimer = System.currentTimeMillis(); 
     double nsPerUpdate = 1000000000.0/60; 
     // last update time in nano seconds 
     double then = System.nanoTime(); 
     double unprocessed = 0; 

     while (running) { 

      boolean shouldRender = false; 
      double now = System.nanoTime(); 
      unprocessed += (now - then)/nsPerUpdate; 
      then = now; 

      // update queque 
      while (unprocessed >= 1) { 
       update++; 
       update(); 
       unprocessed--; 
       shouldRender = true; 
      } 
      // render 
      if (shouldRender) { 
       fps++; 
       render(); 
       shouldRender = false; 
      } else { 
       try { 
        Thread.sleep(1); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
    public synchronized start(){ 
     if(running)return; 
     running = true; 
     game = new Thread (this, "game"); 
     game.start(); 

    } 
    public synchronized stop(){ 
     if(!running) return; 
     running = false; 
     System.exit(0); 
    } 
    @Override 
     public void keyTyped(KeyEvent e) { 
    } 
    @Override 
     public void keyPressed(KeyEvent e) { 
    } 
    @Override 
     public void keyReleased(KeyEvent e) { 
    } 
} 
+0

我在哪裏定義它? – Katz

+0

它實際上是while(running){不是半冒號,仍然得到錯誤 – Katz

+0

我也看到你拼錯了'setPreferredSize()'你說'setPreserredSize(new Dimension(WIDTH,HEIGHT));'它應該在哪裏'setPreferredSize新的尺寸(WIDTH,HEIGHT));' –