⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 letterchase.java

📁 Typing fingers Typing programme
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**	This class uses 5 ThreeLayeredComponent to make a single typing component.*/

class LetterChase extends JPanel implements TypingComponent{
	private ThreeLayeredComponent threeLayered[];
	private int index;
	private int currentRow;
	private boolean show=true;
	private final int rows=5,eof;
	private String lesson,user="";
	private TypingFrame frame;
	private SpeedGraphFrame speedFrame;
	private boolean startFlag=true;
	private CalculateSpeed calculate;
	private DialogLabel label;
	private KeyboardFrame keyboard;
	LetterChase(String lesson,SpeedGraphFrame speedFrame,KeyboardFrame keyboard){
		this.lesson=lesson;
		eof=lesson.length();
		this.keyboard=keyboard;
		this.speedFrame=speedFrame;
		int w=Utilities.getTypingFrameSize().width;
		int h=Utilities.getTypingFrameSize().height;
		setPreferredSize(new Dimension(w-10,h-5));
		setLayout(new GridLayout(rows+1,0));
		threeLayered=new ThreeLayeredComponent[rows];
		for(int i=0;i<rows;i++){
			threeLayered[i]=new ThreeLayeredComponent();
			add(threeLayered[i]);		
		}
		JPanel panel=new JPanel();
		panel.add(label=new DialogLabel("Speed=0 WPM",new Font("Times new Roman",Font.BOLD,20)));
		add(panel,BorderLayout.SOUTH);
		frame=new TypingFrame(this);
		calculate=new CalculateSpeed(lesson,label);
		speedFrame.setSpeedSource(calculate);
	}
	public TypingFrame getFrame(){
		return frame;
	}
	public void displayText(){
		clearAllText();
		boolean flag;
		char ch;				
		keyboard.highlightKey(lesson.charAt(index));
		for(int i=0;i<rows;i++){
			while(index<eof){
				ch=lesson.charAt(index);
				flag=threeLayered[i].addText(ch);
				if(flag==false)
					break;
				index++;
			}
		}
	}
	public void addChar(char typed){
		if(typed==8){
			if(threeLayered[currentRow].backspace())
				user=user.substring(0,user.length()-1);
			calculate.update(typed);
			return ;
		}
		if(!threeLayered[currentRow].canDisplay(typed)){
			if(typed!=10)
				return;
			else{
				currentRow++;
				if(currentRow==rows){
					currentRow=0;
					show=true;
					repaint();
					return;
				}
			}
			threeLayered[currentRow].showMagicChar();
		}
		else{
			threeLayered[currentRow].addChar(typed);
			user=user+typed;
			calculate.update(typed);
			if(startFlag){
				calculate.startLesson();
				speedFrame.lessonStarted();
				startFlag=false;
			}
		}
		if(user.length()>=lesson.length()){
			MainScreen.getInstance().lessonEnded();
			return;
		}
		keyboard.highlightKey(lesson.charAt(user.length()));
	}
	public void paint(Graphics g){
		if(show==true){
			displayText();
			show=false;
			threeLayered[0].showMagicChar();
		}
		super.paint(g);
	}
	private void clearAllText(){
		for(int i=0;i<rows;i++){
			threeLayered[i].clearText();
		}
	}
	public Dimension getSize(){
		return getPreferredSize();
	}
	public void updateValues(){
		MainScreen screen=MainScreen.getInstance();
		TempInfo temp=screen.getTempInfo();
		temp.updateValues(calculate.getWords(),calculate.getAccuracy(),
									calculate.getTime());
		temp.updateUserData();
	}
}

/**	This class has three RichLabels in the form of rows.The upper row indicates errors.
*	The middle row display text to be typed and the lower row displays the text typed by
*	the user. 
	
*/
class ThreeLayeredComponent extends JPanel{
	RichLabel upper,middle;
	MagicLabel lower;
	private Color uppercolor;
	private Color middlecolor;
	private Color lowercolor;
	private boolean flag=true;
	private int index;
	private Sound sound;
	ThreeLayeredComponent(){
		setLayout(new GridLayout(3,0));
		upper=new RichLabel();
		middle=new RichLabel();
		lower=new MagicLabel('^',Color.black);
		add(upper);
		add(middle);
		add(lower);
		UserData user=MainScreen.getInstance().getCurrentUser();
		uppercolor=user.getErrorColor();
		middlecolor=user.getLessonColor();
		lowercolor=user.getTypingColor();
		setBackground(user.getBackground());
		sound=new Sound(user.getErrorSound());
	}
	public void setUpperColor(Color c){
		uppercolor=c;
	}
	public void setMiddleColor(Color c){
		middlecolor=c;
	}
	public void setLowerColor(Color c){
		lowercolor=c;
	}
	public boolean addText(char ch){
		if(!middle.nextAllowed())
			return false;
		middle.addChar(ch,middlecolor);
		return true;
	}
	public void addChar(char ch){
		lower.addChar(ch,lowercolor);
		ch=ch==middle.charAt(index)? ' ': 'v';
		upper.addChar(ch,uppercolor);
		if(ch=='v'){
			sound.play();
		}
		index++;
	}
	public boolean canDisplay(char ch){
		if(lower.nextAllowed()==false&&flag){
			lower.removeMagicChar();
			flag=false;
		}
		return flag;
	}
	public boolean nextAllowed(){
		return lower.nextAllowed();
	}
	public boolean backspace(){
		if(index<=0||flag==false)
			return false;
		lower.removeChar();
		upper.removeChar();
		index--;
		return true;
	}
	public int getIndex(){
		return index;
	}
	public void resetIndex(){
		index=0;
	}
	public void showMagicChar(){
		lower.showMagicChar();
	}
	public void clearText(){
		upper.clearText();
		middle.clearText();
		lower.clearText();
		flag=true;
		index=0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -