浏览 2111 次
锁定老帖子 主题:贴一个简单的击键类Java小游戏
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-13
截图: package fun.dwords; import ...; public class DWords extends JFrame { String[] words = { "good", "slow", "goods", "goodbye", "bye", "byte", "bits", "sister", "monkey", "word", "world", "left", "leave" }; JPanel panel; Image img = new ImageIcon("background/jessica_alba.jpg").getImage(); public DWords() { panel = new JPanel() { @Override public void paint(Graphics g) { g.drawImage(img, 0, 0, null); super.paintChildren(g); } }; add(panel); panel.setLayout(null); JPanel btnPanel = new JPanel(); add(btnPanel, BorderLayout.SOUTH); JButton btnStart = new JButton("Start"); btnPanel.add(btnStart); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread thd = new Thread() { public void run() { downWords(); } }; thd.start(); DWords.this.requestFocus(); } }); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500, 400); setTitle("word word down..."); setVisible(true); this.requestFocus(); } public void resetWords() { Component[] lb = panel.getComponents(); for (int i = 0; i < lb.length; i++) { WordLabel wl = (WordLabel) lb[i]; wl.reset(); } } public void downWords() { Random rand = new Random(System.currentTimeMillis()); for (int i = 0; i < words.length; i++) { WordLabel wl = new WordLabel(words[i]); wl.setForeground(new Color(255, 0, 0)); panel.add(wl); wl.setBounds(rand.nextInt(panel.getWidth() - 50), 0, 120, 25); panel.validate(); addKeyListener(wl); Thread thd = new Thread(wl); thd.start(); try { Thread.sleep(2000); } catch (InterruptedException ex) { } } } static String fmtHeader = "<html><span style=\"color:red\">"; static String fmtMiddle = "</span><span style=\"color:black;\">"; static String fmtTail = "</span></html>"; private class WordLabel extends JLabel implements Runnable, KeyListener { volatile boolean removed = false; volatile String fmtWord; String word; int idx = 0; public WordLabel(String word) { super(fmtHeader + fmtMiddle + word + fmtTail); this.fmtWord = fmtHeader + fmtMiddle + word + fmtTail; this.word = word; } public void run() { int y = getY(); while (y < getParent().getHeight()) { y += 2; setText(fmtWord); setLocation(getX(), y); if (removed) { break; } try { Thread.sleep(150); } catch (InterruptedException ex) { } } JPanel panel = (JPanel) getParent(); panel.remove(this); DWords.this.removeKeyListener(this); panel.validate(); panel.repaint(); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (c == word.charAt(idx)) { idx++; if (idx == word.length()) { removed = true; DWords.this.resetWords(); } } else { idx = 0; } if (idx > 0) { fmtWord = fmtHeader + word.substring(0, idx) + fmtMiddle + word.substring(idx) + fmtTail; } else { fmtWord = fmtHeader + fmtMiddle + word.substring(idx) + fmtTail; } } public void reset() { idx = 0; fmtWord = fmtHeader + fmtMiddle + word.substring(idx) + fmtTail; } } /** * @param args */ public static void main(String[] args) { final DWords dw = new DWords(); new Thread() { public void run() { dw.downWords(); } }.start(); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |