java模拟时钟

java模拟时钟,第1张

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)

{

}

}

}

}

import java.awt.*

import java.awt.event.*

import javax.swing.*

import sun.util.calendar.Gregorian

import java.util.Calendar

import java.util.GregorianCalendar

public class ClockPointer extends JFrame{

        int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, 

        ss,mm, hh, old_m, old_h, ang

        final double RAD = Math.PI/180

    public ClockPointer(){

        super("Java时钟")

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

        Image image = getToolkit().getImage("clock.gif")

        setIconImage(image)

        setSize(400,400)

        setBackground(Color.white)

        //setLocation(300,150)

        this.setLocationRelativeTo(null)

        setResizable(true)

        int delay = 1000

        //创建一个监听事件

        setVisible(true)

        ActionListener drawClock = new ActionListener(){

            public void actionPerformed(ActionEvent evt){

                repaint()

            }

        }

        //创建一个时间计数器,每一秒触发一次

        new Timer(delay, drawClock).start()

    }

    

    java.text.SimpleDateFormat fmTime = new java.text.SimpleDateFormat("HH:mm:ss")

    //绘制图形

    public void paint(Graphics g){

        super.paint(g)

        g.setFont(null)

        Graphics2D g2D = (Graphics2D)g

        

        Insets insets = getInsets()

        int L = insets.left/2, T = insets.top/2

        h = getSize().height

        g.setColor(Color.white)

        //画圆

        g2D.setStroke(new BasicStroke(2.0f))

        g.setColor(Color.gray)

        g.drawOval(L+40, T+40, h-80, h-80)

        r = h/2 - 40

        x0 = 40 + r - 5 + L

        y0 = 40 + r - 5 - T

        ang = 60

        //绘制时钟上的12个字

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

            x = (int)((r+10)*Math.cos(RAD*ang)+x0)

            y = (int)((r+10)*Math.sin(RAD*ang)+y0)

            g.setColor(Color.black)

            g.drawString(""+i, x, h-y)

            ang -=30

        }

        //获得现在的时间

        Calendar now = new GregorianCalendar()

        int nowh = now.get(Calendar.HOUR_OF_DAY)

        int nowm = now.get(Calendar.MINUTE)

        int nows = now.get(Calendar.SECOND)

        

        String st=fmTime.format(now.getTime())

        //在窗体上显示时间

        g.setColor(Color.pink)

        g.fillRect(L, T, 50, 28)

        g.setColor(Color.blue)

        g.drawString(st,L+2,T+26)

        

        //计算时间与度数的关系

        ss = 90 - nows*6

        mm = 90 - nowm*6

        hh = 90 - nowh*30 - nowm/2

        x0 = r+40+L

        y0 = r+40+T

        g2D.setStroke(new BasicStroke(1.2f))

        //擦除秒针

        //if(olds_x > 0){

        //    g.setColor(getBackground())

        //    // g.setColor(Color.gray)

        //    g.drawLine(x0, y0, olds_x, h-olds_y) // (?)

        //}

        //绘制秒针

        x = (int)(r*0.9*Math.cos(RAD*ss))+x0

        y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T

        g.setColor(Color.yellow)

        g.drawLine(x0, y0, x, h-y)

        olds_x = x

        olds_y = y

        g2D.setStroke(new BasicStroke(2.2f))

        //擦除分针

        //if(old_m!=mm){

        //    g.setColor(getBackground())

        //    g.drawLine(x0,y0,oldm_x,h-oldm_y)

        //}

        //绘制分针

        x = (int)(r*0.7*Math.cos(RAD*mm))+x0

        y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T

        g.setColor(Color.green)

        g.drawLine(x0,y0,x,h-y)

        oldm_x = x

        oldm_y = y

        old_m = mm

        g2D.setStroke(new BasicStroke(3.2f))

        //擦除时针

        //if(old_h!=hh){

        //    g.setColor(getBackground())

        //    g.drawLine(x0,y0,oldh_x,h-oldh_y)

        //}

        //绘制时针

        x = (int)(r*0.5*Math.cos(RAD*hh))+x0

        y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T

        g.setColor(Color.red)

        g.drawLine(x0,y0,x,h-y)

        oldh_x = x

        oldh_y = y

        old_h = hh

    }

    public static void main(String[] args){

        new ClockPointer()

    }

}

//整理一下


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/20248.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-02-15
下一篇2023-02-15

发表评论

登录后才能评论

评论列表(0条)

    保存