2014-03-30 87 views
0

嘿,我是新來的Java遊戲編程,我現在正在使用Runnable接口。出於某種原因,我的run()方法永遠不會被調用,我不知道爲什麼。我試過把很多System.out.println語句放在裏面,但是他們從來沒有打印過。任何幫助將不勝感激!「運行」方法不工作

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import javax.swing.JPanel; 


public class GamePanel extends JPanel implements Runnable 
{ 
private final int WIDTH = 160; 
private final int HEIGHT = WIDTH/12 *9; 
private final int RATIO = 3; 

private Thread animator; 
private volatile boolean running; 
private volatile boolean gameOver; 

private double FPS = 60D; 
private double period = 1000/FPS; 

private Image dbImage; 
private Graphics dbg; 

public GamePanel() 
{ 
    setPreferredSize(new Dimension(WIDTH *3, HEIGHT*3)); 
    setBackground(Color.WHITE); 
    setFocusable(true); 
    requestFocus(); 
    terminate(); 
} 

public void addNotify() 
{ 
    super.addNotify(); 
    startGame(); 
} 

public void startGame() 
{ 
    System.out.println("Thread started"); 
    animator = new Thread(); 
    animator.start(); 
} 

public void stopGame() 
{ 
    System.out.println("Thread stopped"); 
    running = false; 
} 

public void run() 
{ 
    long beforeTime, timeDiff, sleepTime; 
    beforeTime = System.currentTimeMillis(); 
    System.out.println(beforeTime); 

    running = true; 
    while (running) 
    { 
     System.out.println("Running"); 
     gameUpdate(); 
     gameRender(); 
     paintScreen(); 

     timeDiff = System.currentTimeMillis() - beforeTime; 
     sleepTime = (long) period - timeDiff; 

     if(sleepTime <= 0) 
      sleepTime = 5; 

     try 
     { 
      Thread.sleep(sleepTime); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
     beforeTime = System.currentTimeMillis(); 
    } 
    System.exit(0); 
} 

public void gameRender() 
{ 
    if (dbImage == null) 
    { 
     dbImage = createImage(WIDTH, HEIGHT); 
    } 
    else 
     dbg = dbImage.getGraphics(); 

    dbg.setColor(Color.WHITE); 
    dbg.fillRect(0, 0, WIDTH, HEIGHT); 
} 

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.drawImage(dbImage, 0, 0, null); 
} 

public void gameUpdate() 
{ 

} 

private void paintScreen() 
{ 
    Graphics g; 
    try 
    { 
     g = this.getGraphics(); 
     if (g!= null && dbImage!= null) 
      g.drawImage(dbImage, 0, 0, null); 
     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 
    catch (Exception e) 
    { 
     System.out.println("Error: " + e.getMessage()); 
    } 
} 

public void terminate() 
{ 
    addKeyListener (new KeyAdapter() 
    { 
     public void keyPressed(KeyEvent e) 
     { 
      int keyCode = e.getKeyCode(); 
      if (keyCode == KeyEvent.VK_ESCAPE) 
      { 
       stopGame(); 
      } 
     } 
    }); 
} 


} 


import javax.swing.JFrame; 


public class GameFrame extends JFrame 
{ 
private final int WIDTH = 160; 
private final int HEIGHT = WIDTH/12*9; 
private final int RATIO = 3; 

public GameFrame() 
{ 
    setTitle("User Input Game"); 
    setSize(WIDTH*3,HEIGHT*3); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setResizable(false); 
    GamePanel mainPanel = new GamePanel(); 
    add(mainPanel); 
} 
} 




public class Main 
{ 

public static void main(String[] args) 
{ 
    new GameFrame().setVisible(true); 
} 

} 
+2

爲什麼你覺得這個方法應該被調用?由誰? –

回答

1

你需要改變你的startGame()方法:

animator = new Thread(new GamePanel()); 

你需要傳遞一個Runnable(其中的GamePanel就是其中之一)到Thread構造。線程在啓動時運行可運行的程序。

+0

感謝一堆這工作! – MattKos

0

你似乎沒有任何地方的主要方法。無論是在這個類還是外部類中,您都需要一個創建GamePanel實例並將其作爲參數傳遞給Thread類的主方法。像這樣:

public class Test 
{ 
    public static void main(String[] args) 
    { 
    Thread t = new Thread(new GamePanel()).start(); 
    } 
} 
+0

我忘了在帖子中包含該方法。我只是添加了我的JFrame類和主要方法。 – MattKos

+0

好的,所以現在在你的GameFrame類中,你需要在行後面添加上面的代碼:add(mainPanel)。這將開始你的線程。 – Alan