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

📄 timer.java

📁 java applet趣味小程序,有助于applet学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.applet.*;
import java.awt.*;
import java.util.*;
//代表下拉式编辑框,用来输入小时、分钟和秒
class SpinButton
{		
	int x, y, number, maxVal, runner;	
	Font font1 = new Font("Dialog", Font.BOLD,14);
	boolean up, down;
	long timeSet;
        //构造函数
	public SpinButton(int x, int y, int max)
	{			
		this.x=x;
		this.y=y;
		maxVal=max;
	}
        //初始化
	public void init()
	{
		number=0;
	}
	//画组件
	public void DrawControl(Graphics g)
	{
		g.setColor(Color.white);
		g.fillRect(x, y, 25, 18);
		g.setColor(Color.lightGray);
		g.fill3DRect(x+25, y, 14, 9, !up);
		g.fill3DRect(x+25, y+9, 14, 9, !down);
		g.setColor(Color.black);
		UpTriangle(g, x+28, y+2, 8, 5);
		DownTriangle(g, x+29, y+16, 7, 4);
		g.setFont(font1);					
		g.drawString(""+number,x+5,y+14);	
	}
        //画向上的三角形
	public void UpTriangle(Graphics g, int x, int y, int w, int h) 
	{
			int a[] = {x,(x+w/2),x+w};
			int b[] = {y+h,y,y+h};
			g.fillPolygon(a, b, 3);
	}
        //画向下的三角形
	public void DownTriangle(Graphics g, int x, int y, int w, int h) 
	{			
			int a[] = {x,(x+w/2),x+w};
			int b[] = {y-h,y,y-h};
			g.fillPolygon(a, b, 3);
	}
        //处理mouseDown事件    
	public boolean mouseDown(int xx, int yy)
	{	
		if(xx>x+25&&xx<x+40&&yy>y&&yy<y+9)
		{up=true; return true;}
		if(xx>x+25&&xx<x+40&&yy>y+9&&yy<y+18)
		{down=true; return true;}
		return false;		
	}
        //处理mouseUp事件
	public void mouseUp()
	{	
		up=down=false;			
	}
	public int GetNumber()
	{	
		return number;		
	}
        //当用户按住上下键不松开时,增加或减小值
	public void Update()
	{			
		runner++;
		if(runner%2==0)
		{
			if(up){if(number<maxVal)number++; else number=0;}
			if(down){if(number>0)number--; else number=maxVal;}
		}		
	}
}
//代表是否播放声音、倒计时还是正计时的CheckBox类
class CheckBox
{		
	int x, y;	
	Font font1 = new Font("Helvetica", Font.BOLD,9);
	boolean checked, clicked;
	String string, message;
	int stringWidth;
        //构造函数   	
	public CheckBox(int x, int y, String str, boolean checked)
	{			
		this.x=x;
		this.y=y;	
		this.string=str;
		this.checked=checked;
	}
        //初始化
	public void init()
	{
		
	}
	//画组件
	public void DrawControl(Graphics g)
	{
		g.setFont(font1);
		FontMetrics fm = g.getFontMetrics(font1);		
		stringWidth=fm.stringWidth(string);
		g.setColor(Color.white); 
		g.fillOval(x,y,13,13);
		g.fillRect(x+20,y,stringWidth+9,13);
		g.setColor(Color.black);			
		if(checked)
			g.fillOval(x+4,y+4,5,5);							
		g.drawString(string,x+25,y+10);			
	}
        //处理mouseDown事件
	public boolean mouseDown(int xx, int yy)
	{	
		if(xx>x&&xx<x+stringWidth+30&&yy>y&&yy<y+13)
		{clicked=true;	return true;}
		return false;
	}
        //处理mouseUp事件
	public void mouseUp()
	{	
		clicked=false;			
	}
}
//定义Timer类
public class Timer extends Applet implements Runnable
{       	
        Thread Clock_animation;//定义线程
        private Image Buffer;
        private Graphics gBuffer;	
		long currTime, startTime, diffTime, pauseValue, timeSet;
		int hours, minutes, seconds, runner, soundRunner;
		boolean pressedStart, pressedStop, pressedReset, pressedEnlarge, countDown;	
		boolean paused, enlarged, running, playSound, input, blink, framed;
		boolean soundDelay;
		//用来完成旋转的变量
		int ox=0;
		int oy=0;
		double radians = 0.0;
		double cos = 1.0;
		double sin = 0.0;
		AudioClip alarmSound;
		Font font1 = new Font("Dialog", Font.PLAIN,14); 
		Font font2 = new Font("Dialog", Font.BOLD,11);
		Font font3 = new Font("Helvetica", Font.BOLD,25);
		Font font4 = new Font("Helvetica", Font.BOLD,9);
		Font font5 = new Font("Helvetica", Font.BOLD,16);
		Color color1 = new Color(25,55,135);
		Color color2 = new Color(225,225,225);
		Color bgColor = new Color(0,0,0);
		SpinButton sb1, sb2, sb3;
		CheckBox cb1, cb2, cb3;		
		String message;		
                //初始化
		public void init()
		{						
		Buffer=createImage(size().width,size().height);
		gBuffer=Buffer.getGraphics();	
		//载入警报声
		try{
			alarmSound=getAudioClip(getCodeBase(),"alarm.au");							
			}
		catch (Exception e){}
			sb1 = new SpinButton(110, 280, 99);
			sb2 = new SpinButton(110, 300, 59);
			sb3 = new SpinButton(110, 320, 59);
			cb1 = new CheckBox(52,124, "Count Down", false);
			cb2 = new CheckBox(52,144, "Count Up", true);
			cb3 = new CheckBox(10,244, "Play Sound", true);			
			playSound=true;				
			currTime=startTime=diffTime=pauseValue=timeSet=0;			
			message=getParameter("timer_name");
			String r, g, b;
			int rt, gr, bl;
			//设置颜色参数
			String clockColorStr=getParameter("clock_color");
			r=clockColorStr.substring(0,2);
			g=clockColorStr.substring(2,4);
			b=clockColorStr.substring(4);
			rt=Integer.parseInt(r, 16);
			gr=Integer.parseInt(g, 16);
			bl=Integer.parseInt(b, 16);
			color1=(new Color(rt, gr, bl));
			String bgColorStr=getParameter("bg_color");
			r=bgColorStr.substring(0,2);
			g=bgColorStr.substring(2,4);
			b=bgColorStr.substring(4);
			rt=Integer.parseInt(r, 16);
			gr=Integer.parseInt(g, 16);
			bl=Integer.parseInt(b, 16);
			bgColor=(new Color(rt, gr, bl));
		} 
      //开始线程			 	
      public void start()
		{
             if (Clock_animation == null) 
			 {
                  Clock_animation = new Thread (this);
                  Clock_animation.start();
             }
      }
                //停止线程
		public void stop()
		{
                if (Clock_animation != null) {
                        Clock_animation.stop();
                        Clock_animation = null;
                }
        }
       //运行
       public void run()
		{			
          while (true) 
			{					
				try  {Clock_animation.sleep (100);}
                    catch (Exception e) { }							                     
			
				runner++;
				if(runner>5)runner=0;

				if(soundDelay)
				{					
					soundRunner++;

					if(soundRunner>50)
					{
						alarmSound.stop();
						soundRunner=0;
						soundDelay=false;
					}
				}
				if(runner<3)blink=true;	else blink=false;				
				sb1.Update();
				sb2.Update();
				sb3.Update();
				DrawBackground();
				DrawTextArea();
				DrawGauge();
				DrawClock();	
				DrawCircle(); 
				if(!paused&&running)CalculateTime();				
				ObserveTimer();
				repaint();				
			}   			
        }
                //播放警报声
		void ObserveTimer()
		{
			if(countDown&&running)
				if(hours==0&&minutes==0&&seconds==0)
			{				
				if(playSound) 
					alarmSound.loop();

				soundDelay=true;
				running=false;
			}
		}
                //画背景
		void DrawBackground() 
		{
			gBuffer.setColor(bgColor);
			gBuffer.fillRect(0,0,190,350);
			gBuffer.setColor(color1);
            if(enlarged)
				gBuffer.fillRoundRect(0,0,190,350,32,32);
			else
				gBuffer.fillRoundRect(0,0,190,270,32,32);			

			gBuffer.setColor(Color.lightGray);

			gBuffer.fill3DRect(22,90,46,20,!pressedStart);

			gBuffer.fill3DRect(72,90,46,20,!pressedStop);

			gBuffer.fill3DRect(122,90,46,20,!pressedReset);
			gBuffer.setFont(font2);	
			gBuffer.setColor(Color.black);			
			gBuffer.drawString("START",24,104);	
			gBuffer.drawString("STOP",78,104);	
			gBuffer.drawString("RESET",125,104);	
			gBuffer.setColor(color2);
			gBuffer.fill3DRect(100,242,19,18,!pressedEnlarge);
			gBuffer.setColor(Color.black);			
			DrawTriangle(104, 256, 10, 8);
			gBuffer.setFont(font2);	
			gBuffer.setColor(Color.white);			
			gBuffer.drawString("Set Timer",125,255);			
			if(enlarged)
			{
				gBuffer.drawString("Hours",30,295);
				gBuffer.drawString("Minutes",30,315);
				gBuffer.drawString("Seconds",30,335);
				sb1.DrawControl(gBuffer);
				sb2.DrawControl(gBuffer);
				sb3.DrawControl(gBuffer);
			}			
			else
			{	
				gBuffer.setFont(font4);	
				gBuffer.setColor(Color.black);							
				gBuffer.drawString("?2003 by Ali",2,322);

				gBuffer.setColor(Color.white);			
				gBuffer.drawString("?2003 by Ali",0,320);
			}
			cb1.DrawControl(gBuffer);

⌨️ 快捷键说明

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