站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个经典的小时钟

一个经典的小时钟

摘要: 一个经典的小时钟

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="737" border="0"> <tr> <td width="731"> </td> </tr> </table>

程序运行图:



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

} </td> </tr> <tr>


↑返回目录
前一篇: 判断闰年与日、月、年是否有效的函数
后一篇: 一个用DES来加密、解密的类