freehand.java~1~
来自「applet java 的编程程序」· JAVA~1~ 代码 · 共 285 行
JAVA~1~
285 行
package freehand;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class Freehand extends JFrame { private static String s="abcdefghijklmnopqrstuvwxyz"; private int get; private int lose; private boolean b=false; private int alltime=40; private int time=15; private String pd="";//存放键盘输入的字符 private LinkedList list=new LinkedList(); private Canvas cc=new Canvas(); private JPanel jPanel2=new JPanel(); private Generator generator; JLabel jLabel1 = new JLabel(); GridLayout gridLayout1 = new GridLayout(10,1); JLabel jLabel2 = new JLabel(); JLabel jLabel3 = new JLabel(); JLabel jLabel4 = new JLabel(); JLabel jLabel5 = new JLabel(); JLabel jLabel6 = new JLabel(); JLabel jLabel7 = new JLabel(); JPanel jPanel1 = new JPanel(); GridLayout gridLayout2 = new GridLayout(2,1); JLabel jLabel8 = new JLabel(); JPanel jPanel3 = new JPanel(); JLabel jLabel9 = new JLabel(); GridLayout gridLayout3 = new GridLayout(2,1); JPanel jPanel4 = new JPanel(); GridLayout gridLayout4 = new GridLayout(2,1); JLabel jLabel10 = new JLabel(); public Freehand() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } void jbInit() throws Exception { this.getContentPane().setLayout(new BorderLayout()); cc.setBackground(Color.yellow); jPanel2.setBackground(Color.orange); jPanel2.setLayout(gridLayout1); jLabel1.setFont(new java.awt.Font("Dialog", 0, 20)); jLabel1.setPreferredSize(new Dimension(100, 29)); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jLabel2.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel2.setMaximumSize(new Dimension(0, 0)); jLabel2.setRequestFocusEnabled(true); jLabel2.setToolTipText(""); jLabel2.setHorizontalAlignment(SwingConstants.CENTER); jLabel2.setHorizontalTextPosition(SwingConstants.CENTER); jLabel2.setText("按F2开始练习"); jLabel3.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel3.setHorizontalAlignment(SwingConstants.CENTER); jLabel3.setText("按F3暂停或继续"); jLabel4.setFont(new java.awt.Font("Dialog", 0, 20)); jLabel4.setMaximumSize(new Dimension(39, 18)); jLabel4.setHorizontalAlignment(SwingConstants.CENTER); jLabel4.setText(" 成 绩 "); jLabel5.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel5.setText(" 字符总数:"); jLabel6.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel6.setHorizontalAlignment(SwingConstants.LEADING); jLabel6.setText(" 正确数:"); jLabel7.setFont(new java.awt.Font("Dialog", 0, 12)); jLabel7.setText(" 丢失数: "); jPanel1.setLayout(gridLayout2); jLabel8.setHorizontalAlignment(SwingConstants.CENTER); jLabel8.setText(""); jLabel9.setHorizontalAlignment(SwingConstants.CENTER); jLabel9.setText(""); jPanel3.setLayout(gridLayout3); jPanel4.setLayout(gridLayout4); jLabel10.setHorizontalAlignment(SwingConstants.CENTER); jPanel3.setBackground(Color.orange); jPanel1.setBackground(Color.orange); jPanel4.setBackground(Color.orange); jPanel2.add(jLabel1, null); jPanel2.add(jLabel2, null); jPanel2.add(jLabel3, null); jPanel2.add(jLabel4, null); jPanel2.add(jPanel1, null); jPanel1.add(jLabel5, null); jPanel1.add(jLabel8, null); jPanel2.add(jPanel3, null); jPanel3.add(jLabel6, null); jPanel3.add(jLabel9, null); jPanel2.add(jPanel4, null); jPanel4.add(jLabel7, null); jPanel4.add(jLabel10, null); this.setResizable(false); this.setTitle("Game"); this.setSize(640,480); this.getContentPane().add(cc, BorderLayout.CENTER); this.getContentPane().add(jPanel2, BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addKeyListener(new MyKeyListener()); this.setVisible(true); myPaint(); } public void myPaint() { Graphics g; g=cc.getGraphics(); g.setColor(Color.blue); g.drawRect(10,10,510,430); g.dispose(); } public void go() { get=0; lose=0; jLabel8.setText(String.valueOf(get+lose)); jLabel9.setText(String.valueOf(get)); jLabel10.setText(String.valueOf(lose)); generator=new Generator(); generator.start(); } //显示成绩 private synchronized void result() { jLabel8.setText(String.valueOf(get+lose)); jLabel9.setText(String.valueOf(get)); jLabel10.setText(String.valueOf(lose)); } public static void main(String[] args) { new Freehand(); } class MyKeyListener extends KeyAdapter implements KeyListener { public synchronized void keyPressed(KeyEvent ke) { if(ke.getKeyCode()==KeyEvent.VK_F2) { if(list.isEmpty()) go(); return; } if(ke.getKeyCode()==KeyEvent.VK_F3) { if(list.isEmpty()) return; Graphics g=cc.getGraphics(); g.setXORMode(cc.getBackground()); g.setFont(new Font("Times New Roman",Font.PLAIN,50)); g.drawString("PAUSE",200,200); g.dispose(); if(b) { generator.setNotify(); for(int i=0;i<list.size();i++) { ((MyThread)list.get(i)).setNotify(); } b=false; }else { generator.setWait(); for(int i=0;i<list.size();i++) { ((MyThread)list.get(i)).setWait(); } b=true; } return; } pd=String.valueOf(ke.getKeyChar()); for(int i=0;i<list.size();i++) { if(((MyThread)list.get(i)).compare()) { get++; return; } } } } //MyThread线程的生成器 class Generator extends Thread { private Random r=new Random(); private boolean wait=false; public void setWait() { wait=true; } public synchronized void setNotify() { wait=false; try { this.notify(); }catch(Exception e){} } public synchronized void run() { int temp=0,i=0; while(i<(alltime*1000/(time*30))) { temp=r.nextInt(s.length()); MyThread t=new MyThread(cc,s.substring(temp,temp+1),(r.nextInt(49)+1)*10,20); list.add(t); t.start(); try { Thread.sleep(30*time); }catch(Exception e){} i++; if(wait) { try { this.wait(); }catch(Exception e){} } } } } //每个下落字母的线程 class MyThread extends Thread { private int x=0; private int y=0; private String s; private Canvas c; private boolean bool=false; private boolean wait=false; public MyThread(Canvas c,String s,int x,int y) { this.c=c; this.x=x; this.y=y; this.s=s; } private void draw() { Graphics g=c.getGraphics(); g.setFont(new Font("Times New Roman",Font.PLAIN,20)); g.setColor(Color.red); g.setXORMode(c.getBackground()); g.drawString(s,x,y); g.dispose(); } private void move() { Graphics g=c.getGraphics(); g.setFont(new Font("Times New Roman",Font.PLAIN,20)); g.setColor(Color.red); g.setXORMode(c.getBackground()); g.drawString(s,x,y); y++; g.drawString(s,x,y); g.dispose(); } public boolean compare() { if(pd.equals(s)==true) { bool=true; pd=""; return true; } return false; } public void setWait() { wait=true; } public synchronized void setNotify() { wait=false; try { this.notify(); }catch(Exception e){} } public synchronized void run() { draw(); while(y<440&&bool!=true) { move(); try { this.sleep(time); }catch(Exception e){} if(wait) { try { this.wait(); }catch(Exception e) {} } } draw(); list.remove(this); if(y>=440) { lose++; } result(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?