Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 371|回复: 0

[Java基础知识]一个经典的小时钟

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 02:50:39 | 显示全部楼层 |阅读模式
    程序运行图:



    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    import java.util.*;
    import java.awt.geom.*;
    import java.text.*;
    import java.awt.image.*;
    import javax.swing.Timer;
    import java.net.*;

    public class jishiqi  extends JFrame
    {
    private GregorianCalendar now=new GregorianCalendar();
    private MyLabel ml=new MyLabel();
    private JLabel Lweek=new JLabel();
    private JLabel Ldate=new JLabel();
    private JLabel Lmark=new JLabel();
    private JLabel Ltime=new JLabel();
    public jishiqi()
    {
       setTitle("Timer");
       setSize(500,480);
       setLocation(100,100);
       setResizable(false);
       ml.setSize(500,440);

       Lmark.setText("ZQIAN");Lmark.setLocation(225,80);Lmark.setSize(100,30);
       Lmark.setFont(new Font("Bookman Old Style",Font.BOLD,15));Lmark.setForeground(Color.darkGray);

       Lweek.setSize(60,20);Lweek.setLocation(315,190);Lweek.setForeground(Color.darkGray);
       Lweek.setFont(new Font("Arial Narrow",Font.BOLD,12));

       if(now.get(Calendar.DAY_OF_WEEK)==1) Lweek.setText("SUNDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==2) Lweek.setText("MONDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==3) Lweek.setText("TUESDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==4) Lweek.setText("WEDNESDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==5) Lweek.setText("THURSDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==6) Lweek.setText("FRIDAY");
       else if(now.get(Calendar.DAY_OF_WEEK)==7) Lweek.setText("SATURDAY");
       Ldate.setSize(20,20);Ldate.setLocation(370,190);Ldate.setForeground(Color.darkGray);
       Ldate.setFont(new Font("Fixedsys",Font.BOLD,12));Ldate.setText(""+now.get(Calendar.DATE));

       Ltime.setSize(500,30);Ltime.setLocation(100,400);
       Ltime.setForeground(new Color(0,64,128));
       Ltime.setFont(new Font("Fixedsys",Font.PLAIN,15));

       Container con=getContentPane();
       con.setBackground(Color.white);
       con.setLayout(null);
       con.add(Lweek);con.add(Ldate);con.add(Lmark);con.add(Ltime);
       con.add(ml);


    }
    public static void main(String []args)
    {
       jishiqi jsq=new jishiqi();
       jsq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jsq.show();
    }

    class MyLabel extends JLabel implements Runnable
    {
       Thread th=null;
       double arch=0;double arcm=0;double arcs=0;
       int a=50;
       boolean flag=false;
       int id=0;
       Image img[]=new Image[5];
       BufferedImage bi=null;
       Graphics gg=null;
       URL url[]=new URL[]{MyLabel.class.getResource("1.jpg"),MyLabel.class.getResource("2.jpg"),
            MyLabel.class.getResource("3.jpg"),MyLabel.class.getResource("4.jpg"),
            MyLabel.class.getResource("5.jpg"),MyLabel.class.getResource("6.jpg")};

       public MyLabel()
       {
        GregorianCalendar  jj=new GregorianCalendar();
        arch=now.get(Calendar.HOUR)*30;arcm=now.get(Calendar.MINUTE)*6;arcs=now.get(Calendar.SECOND)*6;

        Toolkit tk=this.getToolkit();
        img[0]=tk.createImage(url[0]);
        img[1]=tk.createImage(url[1]);
        img[2]=tk.createImage(url[2]);
        img[3]=tk.createImage(url[3]);
        img[4]=tk.createImage(url[4]);
        try
        {
         MediaTracker mt=new MediaTracker(this);
         mt.addImage(img[0],0);
         mt.addImage(img[1],0);
         mt.addImage(img[2],0);
         mt.addImage(img[3],0);
         mt.addImage(img[4],0);
         mt.waitForAll();
        }catch(Exception err){}
        bi=new  BufferedImage(310,310,BufferedImage.TYPE_INT_ARGB);
        gg=bi.createGraphics();
        th=new Thread(this);
        th.start();
       }

       public void paint(Graphics g1)
       {
        Graphics2D g=(Graphics2D)g1;

        for(int i=0;i<360;i=i+6)
        {
         g.setColor(Color.blue);
         g.setStroke(new BasicStroke(2));
         if(i%30==0&&i!=90&&i!=180&&i!=270)
         {
          g.setColor(Color.red);
          g.drawLine(250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*155),
                250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*155));
         }
         if(i%90==0)
         {
          g.setColor(Color.blue);
          g.setStroke(new BasicStroke(5));
          g.drawLine(250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*155),
                    250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*155));
         }
         g.drawLine(250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*153),
              250+(int)(Math.cos(i*Math.PI/180)*150),200+(int)(Math.sin(i*Math.PI/180)*153));
        }

        Line2D.Double lh=new Line2D.Double(250,200,250+Math.cos((arch-90)*Math.PI/180)*50,200+Math.sin((arch-90)*Math.PI/180)*50);
        g.setStroke(new BasicStroke(6));
        g.setColor(new Color(0,128,0));
        g.draw(lh);
        Line2D.Double lm=new Line2D.Double(250,200,250+Math.cos((arcm-90)*Math.PI/180)*80,200+Math.sin((arcm-90)*Math.PI/180)*80);
        g.setStroke(new BasicStroke(3));
        g.setColor(new Color(0,128,192));
        g.draw(lm);
        Line2D.Double ls=new Line2D.Double(250,200,250+Math.cos((arcs-90)*Math.PI/180)*100,200+Math.sin((arcs-90)*Math.PI/180)*100);
        g.setStroke(new BasicStroke(1));
        g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
        g.draw(ls);

        gg.drawImage(img[id],0,0,310,310,0,0,320,320,this);
        for(int j=0;j<310;j++)
        {
         for(int i=0;i<310;i++)
         {
          int pix=bi.getRGB(i,j);
          Color c=new Color(pix);
          int r=c.getRed();
          int gr=c.getGreen();
          int b=c.getBlue();
          int newpix=new Color(r,gr,b,a).getRGB();
          bi.setRGB(i,j,newpix);
         }
        }
        g.clip(new Ellipse2D.Double(95,45,310,310));
        g.setColor(Color.white);
        g.drawImage(bi,95,45,this);

       }

       public void run()
       {
        while(th!=null)
        {
         DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);
         String s=df.format(new Date());
         Ltime.setText(s);
         arcs+=6;arcm+=0.1;arch+=0.1/60;
         if(arcs>=360)
         {
          arcs=0;
         }
         if(arcm>=360)
         {
          arcm=0;
         }
         if(arch>=360)
         {
          arch=0;
         }
         if(flag)
         {
          a+=10;
          if(a==50){flag=!flag;}
         }
         else
         {
          a-=10;
          if(a==0)
          {
           flag=!flag;
           id++;
           if(id==4) id=0;
          }
         }
         repaint();
         try
         {
          Thread.currentThread().sleep(1000);
         }catch(Exception err){}
        }
       }
    }

    }

      

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/1/025038672.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-28 07:10 , Processed in 0.410959 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表