重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
站在用户的角度思考问题,与客户深入沟通,找到桓仁网站设计与桓仁网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站建设、企业官网、英文网站、手机端网站、网站推广、空间域名、网络空间、企业邮箱。业务覆盖桓仁地区。
import java.applet.Applet;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ClockApplet extends Applet implements Runnable //Applet支持线程
{
private Thread athread; //线程
private SimpleDateFormat sdateformat; //日期格式
public void init()
{
this.setBackground(Color.white);//背景颜色设为白色
this.athread = null;
}
public void paint(Graphics g)
{
this.sdateformat = new SimpleDateFormat("hh时mm分ss秒");
g.drawString(this.sdateformat.format(new Date()),25,131);
Calendar rightnow = Calendar.getInstance();
int second = rightnow.get(Calendar.SECOND);
int minute = rightnow.get(Calendar.MINUTE);
int hour = rightnow.get(Calendar.HOUR);
//半径
int R_H = 20,R_M = 4,R_S = 4;
//时针的坐标
//x ====(9-3)[0-6] (3-9)[6-0]
//y ====(12-6)[0-6] (6-12)[6-0]
int H_x ;
int H_y;
//x
if(hour == 0)
{
hour = 12;
}
if( hour = 3 hour = 9 )
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
if(hour 9)
{
H_x = R_H*Math.abs(hour - 9);
}
else
{
H_x = R_H*Math.abs(hour+3);
}
}
//y
if( hour = 6 hour = 12 )
{
H_y = R_H*Math.abs(hour - 12);
}
else
{
H_y = R_H*hour;
}
//分针的坐标
int M_x;
int M_y;
if(minute == 0)
{
minute = 60;
}
if( minute = 15 minute = 45 )
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
if(minute 45)
{
M_x = R_M*Math.abs(minute - 45);
}
else
{
M_x = R_M*Math.abs(minute+15);
}
}
//y
if( minute = 30 minute 60 )
{
M_y = R_M*Math.abs(minute - 60);
}
else
{
M_y = R_M*minute;
}
//秒针的坐标
int S_x;
int S_y;
if(second == 0)
{
second = 60;
}
if( second = 15 second = 45 )
{
S_x = R_S*Math.abs(second - 45);
}
else
{
if(second 45)
{
S_x = R_S*Math.abs(second - 45);
}
else
{
S_x = R_S*Math.abs(second+15);
}
}
//y
if( second = 30 second = 60 )
{
S_y = R_S*Math.abs(second - 60);
}
else
{
S_y = R_S*second;
}
// g.drawString(String.valueOf(second),25,50);
// g.drawString(String.valueOf(minute),25,60);
// g.drawString(String.valueOf(hour),25,70);
// g.drawString(String.valueOf(H_x),25,80);
// g.drawString(String.valueOf(H_y),25,90);
g.drawOval(0,0,120,120);//距离相差10像素
g.setColor(Color.darkGray);
g.drawString("9",5,65);
g.drawString("3",110,65);
g.drawString("12",55,15);
g.drawString("6",55,115);
g.drawString("1",80,20);
g.drawString("2",100,40);
g.drawString("4",100,90);
g.drawString("5",80,110);
g.drawString("7",30,110);
g.drawString("8",10,90);
g.drawString("10",10,40);
g.drawString("11",30,20);
g.setColor(Color.red);
g.drawLine(60,60,H_x,H_y);//前一个点表示起点,另一个表示终点
g.setColor(Color.blue);
g.drawLine(60,60,M_x,M_y);
g.setColor(Color.yellow);
g.drawLine(60,60,S_x,S_y);
}
public void start()
{
if(athread == null)
{
athread = new Thread(this);
athread.start();
}
}
public void stop()
{
if(athread != null)
{
athread.interrupt();
athread = null;
}
}
public void run()
{
while(athread != null)
{
repaint();
try
{
athread.sleep(1000);
}
catch(InterruptedException e)
{
}
}
}
}
利用死循环和线程,让线程在循环中每sleep1秒,重新获取下系统时间不就是动态显示时间了吗
while(true){
Date date=new Date(System.currentTimeMillis());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);
//每一秒刷新下时间
try {
Thread.sleep(1000);//sleep是以ms为单位
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
你可以试下代码,看看是不是你要的效果
可以的,可以实时动态显示当前时间与当前日期,代码结构简洁、清晰、明了,(网络搜集,供参考)
知识的汇总:
1.HTML5
2.CSS3
3.JavaScript
重难点汇总:
1.各个指针的旋转角度的获取,首先要明确以下概念:
一周为360度、12小时、60分钟、60秒;
公式:一周的度数/一周的时间;
即得出时针每过一小时要旋转30度;
分针每过一分钟要旋转6度;
秒针每过一秒钟要旋转6度;
下面是代码部分:
HTML:
div id="box"
div id="h"/div
div id="min"/div
div id="s"div class="cen"/div/div
div id="data"/div
/div
CSS3:
body{
background-color: #aaa;
margin: 0px;
padding: 0px;
}
#box{
width: 400px;
height: 400px;
border-radius: 100%;
background: url(img/4706.jpg_wh860.jpg)0px 0px no-repeat;
background-size: 400px;
position: absolute;
left: 500px;
top: 200px;
}
#h{
width: 100px;
height: 10px;
background-color: red;
position: relative;
top: 195px;
left: 200px;
}
#min{
width: 140px;
height: 10px;
background-color: yellow;
position: relative;
top: 185px;
left: 200px;
}
#s{
width: 180px;
height: 10px;
background-color: blue;
position: relative;
top: 175px;
left: 200px;
}
.cen{
width: 10px;
height: 10px;
background-color: white;
border-radius: 100%;
}
#data{
position: relative;
top: 100px;
left: 150px;
color: red;
font-size: 20px;
}
JavaScript:
function tim(){
var d = new Date(),//获取当前系统时间
year = d.getFullYear(),//得到当前年份
mon = d.getMonth(),//得到当前月份
date = d.getDate(), //得到当前日期
hours = d.getHours(), //得到当前小时
minutes = d.getMinutes(), //得到当前分钟
seconds = d.getSeconds();//得到当前秒
var hou = "";
if(hours12){
hou = "下午";
}
else{
hou = "上午";
}
document.getElementById("data").innerHTML= year+"年"+mon+"月"+date+"日"+"
"+hou;
var n = document.getElementById("s");//获取秒针ID
n.style.transform = "rotate("+(seconds*6-90)+"deg)";//通过当前秒数,得到秒针旋转度数
n.style.transformOrigin = "left";//设置秒针旋转的基点
var i = document.getElementById("min");//获取分针ID
i.style.transform = "rotate("+(minutes*6-90)+"deg)"//通过当前分钟数,得到分针旋转度数
i.style.transformOrigin = "left";//设置分针旋转的基点
var h = document.getElementById("h");//获取时针ID
h.style.transform = "rotate("+((hours*30)+(minutes*0.5)-90)+"deg)"//通过当前小时数,得到时针旋转度数
h.style.transformOrigin = "left";//设置时针旋转的基点
}
setInterval("tim()",1000)