2015-10-16 183 views
-1
public class game extends Applet implements KeyListener { 
int movex = 10; 
int movey = 10; 
int x = 50, y = 50; 
JTextArea typingArea; 
Random rand = new Random(); 
public void paint(Graphics page){ 
     typingArea = new JTextArea(); 
     typingArea.addKeyListener(this); 

     //Drawing borders w/ Random colors 
     page.setColor(new Color(rand.nextFloat(), rand.nextFloat(), rand.nextFloat())); 
     page.drawRect(0,0,200,200); 

     //delay 
     try { 
      Thread.sleep(1); 
     } catch (InterruptedException e) { 
     } 

     //Draws square   
     page.drawRect(x, y, 10, 10); 
     x+=movex; 
     y+=movey; 

     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
     } 
     Dimension d = getSize(); 
     page.setColor(Color.WHITE); 
     page.fillRect(0, 0, d.width, d.height); 
     paint(page); 
    } 

public void keyPressed(KeyEvent e) { 
    int keyCode = e.getKeyCode(); 
    System.out.println("Key is pressed"); 
    switch(keyCode) { 
     case KeyEvent.VK_UP: 
      movex = 0; 
      movey = 10; 
      break; 
     case KeyEvent.VK_DOWN: 
      movex = 0; 
      movey = -10; 
      break; 
     case KeyEvent.VK_LEFT: 
      movex = -10; 
      movey = 0; 
      break; 
     case KeyEvent.VK_RIGHT : 
      movex = 10; 
      movey = 0; 
      break; 
    } 
} 
@Override 
public void keyReleased(KeyEvent arg0) { 

} 
@Override 
public void keyTyped(KeyEvent e) { 

}} 

目前我正在做一個快速的貪吃蛇遊戲,因爲我對Java相對陌生,但對編程不熟悉。使用Applet我繪製了一個正方形,並通過遞增x,y座標來移動它,然後遞歸地調用paint方法。我使用了錯誤的對象(JTextArea)還是使用了我的方法?請快速回答,謝謝。爲什麼這個keyListener不起作用?

+0

「KeyListener」不起作用的「主要」原因是它從未實際註冊過屏幕上顯示的任何內容,因此永遠無法獲得鍵盤焦點。你不應該使用帶有文本組件的'KeyListener',它完全不合適。 – MadProgrammer

+0

我首先看看[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)和[Performing Custom Painting](http:// docs .oracle.com/javase/tutorial/uiswing/painting /)瞭解繪畫的工作原理。然後,我將刪除applet/AWT API並使用[Swing](http://docs.oracle.com/javase/tutorial/uiswing/)或JavaFX,您將獲得更好的一般支持。我還會看看[如何使用Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)瞭解如何生成遊戲循環的一些想法 – MadProgrammer

+0

你可以看看這個[例子](http://stackoverflow.com/questions/31508913/snakegame-how-to-make-the-tail-follow-the-head/31509204#31509204) 。如果你打算使用Swing,我建議看看[如何使用關鍵綁定](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html),並忘記'KeyListener' – MadProgrammer

回答

0

讓我們看看我們是否能夠打破這一點...

在你paint方法,爲您打造JTextArea一個新的實例,並添加您自己作爲KeyListener

public void paint(Graphics page) { 
    typingArea = new JTextArea(); 
    typingArea.addKeyListener(this); 

三個問題在這裏...

  1. 你真的不應該被一個paint方法中創建新的組件。當組件需要重新繪製(並且可能經常快速連續調用)時,paint方法將被調用,並且paint應該只繪製組件的當前狀態,從不改變它
  2. 您真的不應該使用KeyListener與文本組件
  3. 你從來沒有真正加入JTextArea到顯示的組件,這意味着它永遠無法獲得鍵盤焦點,因此從來沒有觸發KeyEvent小號

然後調用...

//delay 
    try { 
     Thread.sleep(1); 
    } catch (InterruptedException e) { 
    } 
    //... 
    try { 
     Thread.sleep(100); 
    } catch (InterruptedException e) { 
    } 

您的paint方法。這是一個非常糟糕的主意,萬一發生繪畫儘可能快,因爲直到paint方法存在,Graphics上下文的內容可能不會被畫到屏幕

最後,你呼喚我

paint(page); 
} 

這是一個壞主意,因爲一旦它被調用,您將永遠不會真的退出paint方法(請參閱上一條評論),最終將以StackOverflowException結尾。

AWT會在您要重新繪製組件時調用您的paint方法。您可以通過調用repaint來請求更新,但是您應該知道,繪畫將在重繪管理器決定需要更新的內容時發生,因此可能不會立即發生。