2013-10-24 57 views
0

我在創建正方形的動畫時遇到問題,當按下其中一個箭頭鍵時,該動畫將以特定速度移動。我已經實現了keyPressed和actionPerformed方法,並且還在構造函數結束時啓動了定時器的勾號。但廣場仍然不會「移動......我做錯了什麼?Java中的定時器和KeyEvent

以下是JPanel。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class A3JPanel extends JPanel implements KeyListener, ActionListener{ 
public static final int JFRAME_AREA_WIDTH = A3Constants.JFRAME_AREA_WIDTH; 
public static final int JFRAME_AREA_HEIGHT = A3Constants.JFRAME_AREA_HEIGHT;; 

public static final Rectangle HOME_AREA = A3Constants.HOME_AREA; 
public static final Rectangle LEO_LEFT_AREA = A3Constants.LEO_LEFT_AREA; 
public static final Rectangle ALL_WALLS_AREA = A3Constants.ALL_WALLS_AREA; 
public static final Rectangle EXITING_SLIDES_AREA = A3Constants.EXITING_SLIDES_AREA; 

public static final Color NICE_GRAY_COLOUR = A3Constants.NICE_GRAY_COLOUR; 
public static final Color GAME_SCREEN_COLOUR = A3Constants.GAME_SCREEN_COLOUR; 
public static final Color SLIDES_AREA_COLOUR = A3Constants.SLIDES_AREA_COLOUR; 
public static final Color LEO_AREA_COLOUR = A3Constants.LEO_AREA_COLOUR; 
public static final Color HOME_AREA_COLOUR = A3Constants.HOME_AREA_COLOUR; 
public static final Color WALL_COLOUR = A3Constants.WALL_COLOUR; 

public static final int MAX_WALLS = A3Constants.MAX_WALLS; 

public static final Font TINY_FONT = A3Constants.TINY_FONT; 
public static final Font LARGE_FONT = A3Constants.LARGE_FONT; 
public static final Font HUGE_FONT = A3Constants.HUGE_FONT; 

public static final int LARGE_FONT_SIZE = A3Constants.LARGE_FONT_SIZE; 
public static final int HUGE_FONT_SIZE = A3Constants.HUGE_FONT_SIZE; 

public static final Point TICKS_POSITION = A3Constants.TICKS_POSITION; 
public static final Point WINNER_LOSER_INFO_POSITION = A3Constants.WINNER_LOSER_INFO_POSITION; 
public static final Point INFORMATION_POSITION1 = A3Constants.INFORMATION_POSITION1; 
public static final Point INFORMATION_POSITION2 = A3Constants.INFORMATION_POSITION2; 
public static final Point INFORMATION_POSITION3 = A3Constants.INFORMATION_POSITION3; 

public static final int TICKS_ALLOWED = A3Constants.TICKS_ALLOWED; 
public static final int UP = A3Constants.UP; 
public static final int DOWN = A3Constants.DOWN; 
public static final int LEFT = A3Constants.LEFT; 
public static final int RIGHT = A3Constants.RIGHT; 

private CoolCat coolCat; 
private Timer t; 

public A3JPanel() { 
    setBackground(Color.GREEN); 
    coolCat = new CoolCat(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y); 
    addKeyListener(this); 
    t = new Timer(30,this); 
    t.start(); 
    //t.addActionListener(this); 

} 

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    drawGameArea(g); 
    coolCat.draw(g); 
} 

private void drawGameArea(Graphics g){ 
    g.setColor(A3Constants.LEO_AREA_COLOUR); 
    g.fillRect(A3Constants.LEO_LEFT_AREA.x, A3Constants.LEO_LEFT_AREA.y, A3Constants.LEO_LEFT_AREA.width, A3Constants.LEO_LEFT_AREA.height); 
    g.setColor(A3Constants.WALL_COLOUR); 
    g.fillRect(A3Constants.ALL_WALLS_AREA.x, A3Constants.ALL_WALLS_AREA.y, A3Constants.ALL_WALLS_AREA.width, A3Constants.ALL_WALLS_AREA.height); 
    g.setColor(A3Constants.HOME_AREA_COLOUR); 
    g.fillRect(A3Constants.HOME_AREA.x, A3Constants.HOME_AREA.y, A3Constants.HOME_AREA.width, A3Constants.HOME_AREA.height); 

} 

public void keyPressed(KeyEvent e){ 
    if(e.getKeyCode() == KeyEvent.VK_UP){ 
     coolCat.setDirection(A3Constants.UP); 
    } 
    if(e.getKeyCode() == KeyEvent.VK_DOWN){ 
     coolCat.setDirection(A3Constants.DOWN); 
    } 
    if(e.getKeyCode() == KeyEvent.VK_LEFT){ 
     coolCat.setDirection(A3Constants.LEFT); 
    } 
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){ 
     coolCat.setDirection(A3Constants.RIGHT); 
    } 
} 

public void keyReleased(KeyEvent e){} 
public void keyTyped(KeyEvent e){} 


public void actionPerformed(ActionEvent e){ 
    coolCat.move(); 
    repaint(); 
} 

}

並且在下面的其它類。

import java.awt.*; 

public class CoolCat { 
public static final Rectangle LEO_START_AREA = A3Constants.LEO_START_AREA; 

public static final int GAME_SCREEN_AREA_TOP = A3Constants.GAME_SCREEN_AREA_TOP; 
public static final int GAME_SCREEN_AREA_BOTTOM = A3Constants.GAME_SCREEN_AREA_BOTTOM; 
public static final int GAME_SCREEN_AREA_LEFT = A3Constants.GAME_SCREEN_AREA_LEFT; 
public static final int GAME_SCREEN_AREA_RIGHT = A3Constants.GAME_SCREEN_AREA_RIGHT; 

public static final int UP = A3Constants.UP; 
public static final int DOWN = A3Constants.DOWN; 
public static final int LEFT = A3Constants.LEFT; 
public static final int RIGHT = A3Constants.RIGHT; 

private Rectangle area; 
private int speed; 
private int direction; 

public CoolCat(int x, int y){ 

    this.speed = speed; 
    speed = (int)(Math.random() * (8 - 4 + 1)) + 4; 
    area = new Rectangle(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y); 
    direction = RIGHT; 

} 

public Rectangle getArea(){ 
    area = new Rectangle(LEO_START_AREA.x, LEO_START_AREA.y, LEO_START_AREA.width, LEO_START_AREA.height); 
    return area; 

} 

public void setDirection(int direction){ 
    this.direction = direction; 
} 

//public boolean hasReachedHome(Rectangle zhomeArea){} 

public void move(){ 
    if(direction == A3Constants.UP){ 
     area.y -= speed; 
    } else if(direction == A3Constants.DOWN){ 
     area.y += speed; 
    } else if(direction == A3Constants.LEFT){ 
     area.x -= speed; 
    } else if(direction == A3Constants.RIGHT){ 
     area.x += speed; 
    } 
} 

public void draw(Graphics g){ 
    g.setColor(Color.RED); 
    g.fillOval(A3Constants.LEO_START_AREA.x, A3Constants.LEO_START_AREA.y, A3Constants.LEO_START_AREA.width, A3Constants.LEO_START_AREA.height); 
} 

}

+0

代碼,大量的代碼... –

+0

@AliAlamiri有像20行的常量... – Cruncher

+0

這個問題的一個可能重複[一邊聽Java中的按鍵如何使圖像的舉動。] (http://stackoverflow.com/questions/6887296/how-to-make-an-image-move-while-listening-to-a-keypress-in-java)。特別是,請看這個問題的關鍵綁定解決方案。 –

回答

1

要添加的KeyListener到一個JPanel,在默認情況下不能接受焦點的組件,焦點所需的關鍵監聽工作。

一個簡單的解決方案是讓您的JPanel接受焦點,然後請求焦點給予它。

更好的解決方案是避免在Swing應用程序中使用KeyListeners,而是使用Key Bindings(正如前面類似問題中的許多次所討論的)。例如,請查看:How to make an image move while listening to a keypress in Java.

0

嘗試類似這樣的操作。我只是很快做到了這一點,所以請原諒任何錯誤。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
public class g{ 
    public static void main(String args){ 
     SwingUtilities.invokeLater(new Runnable(){ 
      public void run(){ 
       create(); 
      } 
     }); 
    } 
    public static MyPanel my; 
    public static void create(){ 
     JFrame f = new JFrame("test"); 
     my = new MyPanel(); 
     f.add(my); 
     f.setSize(1000,1000); 
     f.setVisible(true); 
     f.addKeyListener(new KeyListener(){ 
      @Override 
      public void keyPressed(KeyEvent e){ 
       if(e.getKeyCode == 37){ //left 
        my.move(4); //1 = up, 2 = right, 3 = down, 4 = left 
       } 
       if(e.getKeyCode == 39){ //left 
        my.move(2); //1 = up, 2 = right, 3 = down, 4 = left 
       } 
       if(e.getKeyCode == 38){ //left 
        my.move(1); //1 = up, 2 = right, 3 = down, 4 = left 
       } 
       if(e.getKeyCode == 40){ //left 
        my.move(3); //1 = up, 2 = right, 3 = down, 4 = left 
       } 
      } 
      @Override 
      public void keyReleased(KeyEvent e){ 
       //do nothings 
      } 
      @Override 
      public void keyTyed(KeyEvent e){ 
       //do nothing 
      } 
     }); 
    } 
} 

public class MyPanel extends JPanel implements ActionListener{ 
    public static Timer time1; 
    public Square s; 
    public MyPanel(){ 
     time1 = new Timer(50, this); 
     time1.start(); 
    } 
    public void actionPerformed(ActionEvent e){ 
     repaint(); 
    } 
    public void move(int d){ 
     if(d == 1){ 
      s.setY(s.getY()-1); 
     } 
     if(d == 2){ 
      s.setX(s.getX() + 1); 
     } 
     if(d == 3){ 
      s.setY(s.getY() + 1); 
     } 
     if(d == 4){ 
      s.setX(set.getX() - 1); 
     } 
     repaint(); 
    } 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     s.paintSquare(g); 
    } 
} 

public class Square{ 
    public int x; 
    public int y; 
    public void setX(int xPos){ 
     this.x = xPos; 
    } 
    public int getX(){ 
     return x; 
    } 
    public void setY(int yPos){ 
     this.y = yPos; 
    } 
    public int getY(){ 
     return y; 
    } 
    public void paintSquare(Graphics g){ 
     g.setColor(Color.WHITE); 
     g.fillRect(0,0,1000,1000); 
     g.setColor(Color.RED); 
     g.fillRect(x,y,30,30); 
    } 
}