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

📄 timecomponent.java

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

/**	This class shows a time graph i.e time elapsed and time remaining.
*/

class TimeComponent extends JPanel implements Runnable{
	private DialogLabel timeRemaining;
	private int sleepAmount=1000;
	private long time,seconds,elapsed;
	private Thread thread;
	private TimeGraph timeGraph;
	TimeComponent(int seconds){
		this.seconds=seconds;
		setBorder(new CustomTitledBorder("Time Graph",Color.blue,Color.cyan));
		setLayout(new GridLayout(4,0));
		add(new DialogLabel("Total Time:"+seconds+" seconds"));
		add(timeRemaining=new DialogLabel("Time Remaining:"+seconds+" seconds"));
		add(new JLabel());
		add(timeGraph=new TimeGraph(seconds));
		MainScreen mainScreen=MainScreen.getInstance();
		Sound sound=mainScreen.getMusic();
		UserData user=mainScreen.getCurrentUser();
		String file=user.getBackgroundMusic();
		if(file.length()>1&&file.startsWith("Music")){
			sound.loop();
		}
	}
	public void setSpeedSource(CalculateSpeed speed){
		timeGraph.setSpeedSource(speed);
	}
        public void lessonStarted() {
		thread = new Thread(this);
		thread.setPriority(Thread.MIN_PRIORITY);
		thread.setName("TimeComponent");
	    	time=System.currentTimeMillis();
		thread.start();
        }
	public void run(){
		while(true){
			Thread me = Thread.currentThread();
			while (thread == me && !isShowing() || getSize().width == 0) {
		                try {
        		            thread.sleep(500);
	               	} catch (InterruptedException e) { return; }
			}	
			timeRemaining.setText("Time Remaining:"+(seconds-elapsed)+" seconds");
			timeGraph.click(elapsed);
	                repaint();
	                try {
	                    thread.sleep(sleepAmount);
	                } catch (InterruptedException e) { break ; }
			elapsed=(System.currentTimeMillis()-time)/1000;
			if(elapsed>=seconds){
				MainScreen.getInstance().lessonEnded();
				break;
			}
		}
        }
}
		//*************************************

class TimeGraph extends JProgressBar{
	private long seconds;
	CalculateSpeed speed;
	TimeGraph(long seconds){
		super(1,(int)seconds);
		this.seconds=seconds;
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setPreferredSize(new Dimension(Utilities.getLabelHeight(),Utilities.getLabelHeight()));
	}
	public void setSpeedSource(CalculateSpeed speed){
		this.speed=speed;
	}
	public void click(long elapsed){
		if(elapsed<1||elapsed>seconds)
			return;
		setValue((int)elapsed);
		speed.update();
	}
}

⌨️ 快捷键说明

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