日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编写时钟aplet程序java,编写时钟 Applet 程序

發布時間:2025/4/17 编程问答 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编写时钟aplet程序java,编写时钟 Applet 程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這是學校 java的一道期末考試題,鐘表的指針借鑒的大佬的代碼。。。。

代碼流程

publicpublic class ClockApplet extends Applet implements Runnable{

全局變量的設置

初始化函數{

............

}

畫圖函數{

............

}

線程開始函數{

............

}

}

話不多說,直接上代碼

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class ClockApplet extends Applet implements Runnable{

int x,y,r;

int h,m,s;//時,分,秒

double rad = Math.PI/180;// ?1°

//初始化函數

public void init() {

x = 0;

y = 0;

r = 100;

Calendar now = new GregorianCalendar();

s = now.get(Calendar.SECOND)*6;//轉換成角度

m = now.get(Calendar.MINUTE)*6;//轉換為角度

h = (now.get(Calendar.HOUR_OF_DAY)-12)*30

+ now.get(Calendar.MINUTE)/12*6;//獲取小時

//線程

Thread t =new Thread(this);

t.start();

}

//畫圖函數

public void paint(Graphics g){

super.paint(g);

g.setColor(Color.WHITE);

g.fillRect(0, 0, 300, 300);//填充背景

g.setColor(Color.BLACK);

g.drawOval(x, y, r*2, r*2);//畫表盤

/*drawLine(a,b,c,d)

* a b 起點坐標

* c d 終點坐標

*/

//秒針

int x1 = (int)(90*Math.sin(rad*s));

int y1 = (int)(90*Math.cos(rad*s));

g.drawLine(x+r, y+r, (x+r)+x1, (y+r)-y1);

//分針

x1 = (int)(80*Math.sin(rad*m));

y1 = (int)(80*Math.cos(rad*m));

g.drawLine(x+r, y+r, (x+r)+x1, (y+r)-y1);

//時針

x1 = (int)(60*Math.sin(rad*h));

y1 = (int)(60*Math.cos(rad*h));

g.drawLine(x+r, y+r, (x+r)+x1, (y+r)-y1);

//畫數字

int d = 28;

for(int i=1;i<=12;i++){

x1 = (int)((r-10)*Math.sin(rad*d));

y1 = (int)((r-10)*Math.cos(rad*d));

g.drawString(i+"", x+r+x1-4, x+r-y1+5);

d += 30;

}

//畫刻度

d = 0;

for(int i=1;i<=60;i++){

x1 = (int)((r-2)*Math.sin(rad*d));

y1 = (int)((r-2)*Math.cos(rad*d));

g.drawString(".", x+r+x1-1, x+r-y1+1);

d += 6;

}

//顯示時間

Calendar now1 = new GregorianCalendar();

int a,b,c;

a = now1.get(Calendar.HOUR_OF_DAY);

b = now1.get(Calendar.MINUTE);

c = now1.get(Calendar.SECOND);

g.drawString(a + ":" + b + ":" + c, 0, 10);

}

//實現Runnable

public void run(){

while(true){

try{

Thread.sleep(1000);//間隔一秒

}catch(Exception ex){}

//秒針每次走6°

s += 6;

//秒針歸零

if(s>=360){

s = 0;

m += 6;

//分針走72°,時針走6°

if(m==72 || m==144 || m==288){

h += 6;

}

//分針歸零

if(m>=360){

m = 0;

h += 6;

}

//時針歸零

if(h>=360){

h = 0;

}

}

//重新繪制

this.repaint();

}

}

}

結果如下圖,遺憾的是左上角的時間顯示比系統時間晚了幾毫秒。

標簽:

總結

以上是生活随笔為你收集整理的编写时钟aplet程序java,编写时钟 Applet 程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。