模拟时钟java代码 java课程设计电子时钟代码

怎么用JAVA编程实现实时动态运行的模拟时钟?

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)

{

}

}

}

}

使用Java Applet开发一个模拟时钟

这里有书上源码一篇,可以参考。

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.util.Calendar;

import javax.swing.*;

public class Clock extends JPanel implements ActionListener

{

// 创建时钟的外形

protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);

// 创建时钟的标记

protected static GeneralPath tick = new GeneralPath();

static

{

tick.moveTo(100, 100);

tick.moveTo(49, 0);

tick.lineTo(51, 0);

tick.lineTo(51, 6);

tick.lineTo(49, 6);

tick.lineTo(49, 0);

}

// 创建时针

protected static GeneralPath hourHand = new GeneralPath();

static

{

hourHand.moveTo(50, 15);

hourHand.lineTo(53, 50);

hourHand.lineTo(50, 53);

hourHand.lineTo(47, 50);

hourHand.lineTo(50, 15);

}

// 创建分针

protected static GeneralPath minuteHand = new GeneralPath();

static

{

minuteHand.moveTo(50, 2);

minuteHand.lineTo(53, 50);

minuteHand.lineTo(50, 58);

minuteHand.lineTo(47, 50);

minuteHand.lineTo(50, 2);

}

// 创建秒针

protected static GeneralPath secondHand = new GeneralPath();

static

{

secondHand.moveTo(49, 5);

secondHand.lineTo(51, 5);

secondHand.lineTo(51, 62);

secondHand.lineTo(49, 62);

secondHand.lineTo(49, 5);

}

// 设置时钟的颜色

protected static Color faceColor = new Color(220, 220, 220);

protected static Color hourColor = Color.red.darker();

protected static Color minuteColor = Color.blue.darker();

protected static Color secondColor = new Color(180, 180, 0);

protected static Color pinColor = Color.gray.brighter();

// 创建时钟的枢纽

protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);

protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);

// 创建绕时钟枢纽转的变换

protected AffineTransform hourTransform =

AffineTransform.getRotateInstance(0, 50, 50);

protected AffineTransform minuteTransform =

AffineTransform.getRotateInstance(0, 50, 50);

protected AffineTransform secondTransform =

AffineTransform.getRotateInstance(0, 50, 50);

// 创建每秒触发一次的Timer

protected Timer timer = new Timer(1000, this);

protected Calendar calendar = Calendar.getInstance();

public Clock()

{

setPreferredSize(new Dimension(100, 100));

}

// 当plane加入container中时发生

public void addNotify()

{

super.addNotify();

timer.start();

}

// 当plane从container中移处时发生

public void removeNotify()

{

timer.stop();

super.removeNotify();

}

public void actionPerformed(ActionEvent event)

{

// 更新calender的时间

this.calendar.setTime(new java.util.Date());

int hours = this.calendar.get(Calendar.HOUR);

int minutes = this.calendar.get(Calendar.MINUTE);

int seconds = this.calendar.get(Calendar.SECOND);

//设置变换,使得时针、分针、秒针各自按绕枢纽旋转一定的角度

hourTransform.setToRotation(((double) hours) *

(Math.PI / 6.0), 50, 50);

minuteTransform.setToRotation(((double) minutes) *

(Math.PI / 30.0), 50, 50);

secondTransform.setToRotation(((double) seconds) *

(Math.PI / 30.0), 50, 50);

repaint();

}

public void paint(Graphics g)

{

super.paint(g);

// 得到图形上下文和抗锯齿处理

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(faceColor);

g2.fill(face);

g2.setPaint(Color.black);

g2.draw(face);

// 产生钟面上12个滴答位置

for (double p = 0.0; p 12.0; p += 1.0)

{

//利用变换画出同心的滴答的标线

g2.fill(tick.createTransformedShape(

AffineTransform.getRotateInstance((Math.PI / 6.0) * p,

50, 50)));

}

g2.setPaint(hourColor);

g2.fill(hourHand.createTransformedShape(hourTransform));

g2.setPaint(minuteColor);

g2.fill(minuteHand.createTransformedShape(minuteTransform));

g2.setPaint(secondColor);

g2.fill(secondHand.createTransformedShape(secondTransform));

g2.fill(pivot);

g2.setPaint(pinColor);

g2.fill(centerPin);

}

public static void main(String[] args)

{

JFrame frame = new JFrame();

frame.setLocation(700, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new Clock());

frame.pack();

frame.show();

}

}

怎样用java 程序写一个时钟程序

面向对象思想写成:

下面是一个显示器类

public class Display {

private int value;//现在的值

private int limit;//上限值

Display( int limit) {

this.limit = limit;

}

public void increase() {

value++;

if(value == limit) {

value = 0;

}

}

public int getValue() {

return value;

}

public static void main(String[] args) {

Display d = new Display(24);

for(;;) {

d.increase();

System.out.println(d.getValue());

}

}

}

下面创建一个时钟对象:

public class Clock {

private Display h = new Display(24);

private Display min = new Display(60);

private Display s = new Display(60);

public void start () {

for(;;) {

s.increase();

if(s.getValue() == 0){//如果分重置,小时+1

min.increase();

if(min.getValue() == 0){//如果分重置,小时+1

h.increase();

}

}

System.out.printf("%02d:%02d:%02d\n",h.getValue(), min.getValue(),s.getValue());//格式输出

}

}

public static void main(String[] args) {

Clock clock = new Clock();

clock.start();

}


新闻名称:模拟时钟java代码 java课程设计电子时钟代码
标题链接:http://cdiso.cn/article/dohocii.html

其他资讯