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

📄 applet1.java

📁 Java Applet入门教程
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;
import java.util.Date;

public class Applet1 extends Applet implements Runnable 
{
	
	Date timenow;
	Clock myClock;
	Thread clockthread=null;
		
	public void start()
	{
		if (clockthread==null)
		{
			clockthread=new Thread (this);
			clockthread.start();
		}
	}
	
	public void stop()
	{
		clockthread.stop();
		clockthread=null;
	}
	
	public void run()
	{
		while(true)
		{
			repaint();
			try{Thread.sleep(1000);}
			catch(InterruptedException e){}
		}
	}
		
	public void paint(Graphics g)
	{
		timenow=new Date();
		myClock=new Clock(timenow.getHours (),
							timenow.getMinutes (),
							timenow.getSeconds ());
		g.drawString(timenow.toString(),25,240);
		myClock.show(g,100,100,100);
	
	}
}

class Clock
{
	Clock(int hrs,int min,int sec)
	{
		hour=hrs%12;
		minute=min;
		second=sec;
	}
	void show(Graphics g,int cx,int cy,int rad)
	{
		int hrs_len=(int)(rad*0.5),
			min_len=(int)(rad*0.6),
			sec_len=(int)(rad*0.9);
		double theta;
		//画出钟面
		g.drawOval(cx-rad,cy-rad,rad*2,rad*2);
		//画出时钟
		theta=(double)(hour*60*60+minute*60+second)/43200.0*2.0*Math.PI ;
		drawNiddle(g,Color.blue,cx,cy,hrs_len,theta);
		//
		theta=(double)(minute*60+second)/3600.0*2.0*Math.PI ;
		drawNiddle(g,Color.red,cx,cy,sec_len,theta);
		//
		theta=(double)(second)/60.0*2.0*Math.PI ;
		drawNiddle(g,Color.green ,cx,cy,sec_len,theta);
	}
	private void drawNiddle(Graphics g,Color c,int x,int y,int len,double theta)
	{
		int ex=(int)(x+len*Math.sin(theta));
		int ey=(int)(y-len*Math.cos(theta));
		g.setColor (c);
		g.drawLine(x,y,ex,ey);
	}
	int hour,minute,second;
}

⌨️ 快捷键说明

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