赞
踩
;;;;;;用Applet小程序写的;;;;;;;;
设计思路:: 画图,先是画指针起点就是圆心,终点是一个可以变的量,用系统获取的时间作为自变量
讲一下重要的,就是计算
- package practice1;
-
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.sql.Time;
- import java.time.chrono.ChronoLocalDate;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
-
- import javax.xml.crypto.Data;
-
- import org.w3c.dom.CDATASection;
-
- import practice3.Carculator;
-
- public class ClockApplet extends Applet {
- // 圆的左上角坐标(x,y) 半径为r
- private int x,y,r;
- //刻度的长度
- private int length;
- @Override
- public void init() {
- // TODO Auto-generated method stub
- super.init();
- x = y = 150;
- r = 100;
- length = 10;
- this.resize(new Dimension(500, 500));
- }
- @Override
- public void paint(Graphics g) {
- //画出四个刻度
- g.drawOval(x, y, 2*r, 2*r);
- g.setColor(Color.BLUE);
- //上刻度 0时
- g.drawLine(x+r, y, x+r, y+length);
- //12点的字样,坐标是调整出来的
- g.drawString("12", x+r-length/2, y+2*length+5);
-
- //下刻度 6时
- g.drawLine(x+r, y+r*2, x+r, y+r*2-length);
- //6点的字样,坐标是调整出来的
- g.drawString("6", x+r-2, y+r*2-length-5);
-
- //左刻度 9时
- g.drawLine(x, y+r, x+length, y+r);
- //9点的字样,坐标是调整出来的
- g.drawString("9", x+length+5, y+r+length/2);
-
- //右刻度 3时
- g.drawLine(x+2*r, y+r, x+2*r-length, y+r);
- //3点的字样,坐标是调整出来的
- g.drawString("3", x+2*r-length*2, y+r+length/2);
- //获取当前时间
- Calendar c = Calendar.getInstance();
- int hour = c.get(Calendar.HOUR);
- int minute = c.get(Calendar.MINUTE);
- int second = c.get(Calendar.SECOND);
- String s = new String(hour+":"+minute+":"+second);
- double angle1 = second*Math.PI/30.0;
- double angle2 = (second/60 + minute)*Math.PI/30.0;
- double angle3 = (minute/60 + hour)*Math.PI/6;
- //画时针、分针、秒针
- //圆心坐标(x+r,y+r)
-
- g.drawString(s, 100, 100);
- g.setColor(Color.RED);
- // TODO Auto-generated method stub
- //秒针
- g.drawLine(x+r, y+r, (int)(x+r+r*Math.sin(angle1)), (int)(x+r-r*Math.cos(angle1)));
- //分针
- g.drawLine(x+r, y+r, (int)(x+r+(r-length)*Math.sin(angle2)), (int)(x+r-(r-length)*Math.cos(angle2)));
- //时针
- g.drawLine(x+r, y+r, (int)(x+r+(r-length*3)*Math.sin(angle3)), (int)(x+r-(r-length*3)*Math.cos(angle3)));
- EXE();
- }
- public void EXE() {
- Timer t = new Timer();
- t.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- repaint();
- }
- }, 1000);
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。