「图形时钟java」图形时钟源代码

博主:adminadmin 2022-11-25 13:28:05 57

本篇文章给大家谈谈图形时钟java,以及图形时钟源代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

JAVA画时钟代码

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 = 1;i = 12;i ++){

            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();

    }

}

//整理一下

java时钟设计

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.*;

import java.util.Calendar;

import java.util.GregorianCalendar;

class Clock extends JFrame implements ActionListener{

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 Clock(){

super("Clock by kikiwawa");

setDefaultCloseOperation(3);

setSize(200,200);

setBackground(Color.BLACK);

setLocation(300,150);

setResizable(false);

setVisible(true);

int delay = 1000;

ActionListener drawClock = new ActionListener(){

public void actionPerformed(ActionEvent evt){

repaint();

}

};

new Timer(delay,drawClock).start();

}

public void actionPerformed(ActionEvent e){

}

public void paint(Graphics g){

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(4.0f));

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;

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

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

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

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;

if(nowh10) st="0"+nowh;else st=""+nowh;

if(nowm10) st+=":0"+nowm;else st+=":"+nowm;

if(nows10) st+=":0"+nows;else st+=":"+nows;

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;

if(olds_x0){

g.setColor(getBackground());

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

}

else{

old_m = mm;

old_h = hh;

}

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.4f));

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){

Clock c = new Clock();

} }

求高手帮忙写个JAVA程序,图形时钟

package com.clock;

import java.awt.BasicStroke;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.util.Date;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class ClockPanel extends JPanel{

private static final long serialVersionUID = 4523747293746641642L;

@Override

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2d = (Graphics2D)g;

g2d.setStroke(new BasicStroke(3));

g2d.drawOval(50,50,400,400);//圆点为(250,250)

for(int i = 0;i 60;i++){

int[] t = getPoint(i*6,200);

int[] t1 = new int[2];

if(i%5 == 0){

if(i%3 == 0){

g2d.setStroke(new BasicStroke(3));

t1 = getPoint(i*6,180);

}else{

g2d.setStroke(new BasicStroke(2));

t1 = getPoint(i*6,190);

}

}else{

g2d.setStroke(new BasicStroke(1));

t1 = getPoint(i*6,195);

}

g2d.drawLine(t[0], t[1], t1[0], t1[1]);

}

Date d = new Date();

int h = d.getHours();

int m = d.getMinutes();

int s = d.getSeconds();

double arc_s = s * 6 + 90;

double arc_m = (m + s/60.0) * 6 + 90;

double arc_h = 0;

if(h 12)

h = h - 12;

arc_h = (h + m / 60.0) * 30 + 90;

int[] st = getPoint(arc_s,160);

int[] mt = getPoint(arc_m,130);

int[] ht = getPoint(arc_h,90);

g2d.setStroke(new BasicStroke(1));

g2d.setColor(Color.BLACK);

g2d.drawLine(250,250,st[0],st[1]);

g2d.setStroke(new BasicStroke(2));

g2d.setColor(Color.RED);

g2d.drawLine(250,250,mt[0],mt[1]);

g2d.setStroke(new BasicStroke(3));

g2d.setColor(Color.BLUE);

g2d.drawLine(250,250,ht[0],ht[1]);

g2d.setColor(Color.RED);

Font font = new Font("新宋体",Font.BOLD,17);

g2d.setFont(font);

g2d.drawString("Hour: "+h+" Minute: "+m+" Second: "+s, 150, 500);

}

public int[] getPoint(double arc,int r){

double sin = Math.sin(Math.toRadians(arc));

double cos = Math.cos(Math.toRadians(arc));

return new int[]{(int)(250-r*cos),(int)(250-r*sin)};

}

public static void main(String[] args){

final JFrame frame = new JFrame();

frame.add(new ClockPanel(), BorderLayout.CENTER);

frame.setTitle("Clock!");

frame.setSize(600, 600);

frame.setLocation(100, 100);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false);

frame.setVisible(true);

new Thread(new Runnable(){

@Override

public void run() {

while(true){

frame.repaint();

try{

Thread.currentThread().sleep(1000);

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

}).start();

}

}

图形时钟java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于图形时钟源代码、图形时钟java的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。