import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MKPD extends JFrame
{
public MKPD()
{
super("MKPD");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(new KeyPanel());
pack();
setVisible(true);
}
public static void main(String[] args)
{
Runnable r = new Runnable()
{
public void run()
{
new MKPD();
}
};
EventQueue.invokeLater(r);
}
}
class KeyPanel extends JPanel
{
private enum GameState { MOVED, JUMPED, MOVED_THEN_JUMPED,
JUMPED_THEN_MOVED };
private GameState gs = null;
private boolean rightPressed = false;
private boolean upPressed = false;
private JLabel lblStatus = null;
KeyPanel()
{
KeyAdapter ka = new KeyAdapter()
{
public void keyPressed(KeyEvent ke)
{
handleKeyPress(ke);
}
public void keyReleased(KeyEvent ke)
{
handleKeyRelease(ke);
}
};
addKeyListener(ka);
lblStatus = new JLabel("", JLabel.CENTER);
lblStatus.setPreferredSize(new Dimension(200, 50));
add(lblStatus);
setFocusable(true);
requestFocus();
}
private void handleKeyPress(KeyEvent ke)
{
int keyCode = ke.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT)
rightPressed = true;
else
if (keyCode == KeyEvent.VK_UP)
upPressed = true;
updateState();
updateLabel();
}
private void handleKeyRelease(KeyEvent ke)
{
int keyCode = ke.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT)
rightPressed = false;
else
if (keyCode == KeyEvent.VK_UP)
upPressed = false;
updateState();
updateLabel();
}
private void updateLabel()
{
if (gs == null)
{
lblStatus.setText("");
return;
}
switch (gs)
{
case MOVED_THEN_JUMPED:
lblStatus.setText("Moved and then jumped");
break;
case JUMPED_THEN_MOVED:
lblStatus.setText("Jumped and then moved");
break;
case MOVED:
lblStatus.setText("Moved");
break;
case JUMPED:
lblStatus.setText("Jumped");
}
}
private void updateState()
{
if (rightPressed && upPressed)
{
if (gs == GameState.MOVED)
gs = GameState.MOVED_THEN_JUMPED;
else
if (gs == GameState.JUMPED)
gs = GameState.JUMPED_THEN_MOVED;
}
else
if (rightPressed)
gs = GameState.MOVED;
else
if (upPressed)
gs = GameState.JUMPED;
else
gs = null;
}
}
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2013年6月16日 星期日
Java多重按鍵偵測.
訂閱:
張貼留言 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)
沒有留言:
張貼留言