/*java 写的小时钟 Email:shun_shun@21cn.com 三叶 */ import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Calendar; public class Watch extends Frame { /** * @param args */ final int ScreenSize=400; double h,m,s; int year,month,day,week; String str; Calendar cal; Font f=new Font("楷体_GB2312",1,30); Image img=Toolkit.getDefaultToolkit().getImage("b.gif"); Image offScreenImage=null; final double corner=Math.PI/30; //g.fillOval(15, 40, ScreenSize-20, ScreenSize-20); final int centerx=15+(ScreenSize-20)/2; final int centery=40+(ScreenSize-20)/2; final int hlength=100,mlength=125,slength=140; public void setFrame(){ this.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub System.exit(0); } }); this.setSize(ScreenSize+10,ScreenSize+40); this.setLocation(200, 200); this.setResizable(false); this.setTitle("钟表"); this.setVisible(true); Thread t=new Thread(new SetDate()); t.start(); } public void paint(Graphics g){ Color c=g.getColor(); Font fb=g.getFont(); h=cal.get(Calendar.HOUR); m=cal.get(Calendar.MINUTE); s=cal.get(Calendar.SECOND); year=cal.get(Calendar.YEAR); month=cal.get(Calendar.MONTH)+1; day=cal.get(Calendar.DAY_OF_MONTH); week=cal.get(Calendar.DAY_OF_WEEK); g.drawString(year+"-"+month+"-"+day+" "+week, 100, 100); g.setFont(f); str=Double.valueOf(h).intValue()+" : "+Double.valueOf(m).intValue()+" : "+Double.valueOf(s).intValue(); g.setFont(fb); g.fillOval(5,30, ScreenSize, ScreenSize); g.setColor(Color.white); g.fillOval(15, 40, ScreenSize-20, ScreenSize-20); g.setColor(c); g.setFont(f); g.setFont(fb); g.setColor(Color.red); g.drawString(str, 10, 50); g.setColor(c); int x1,y1; int j=0; g.setFont(f); for(int i=0;i<60;){ x1=centerx+Double.valueOf(175*Math.sin(i*corner)).intValue()-12; y1=centery-Double.valueOf(175*Math.cos(i*corner)).intValue()+10; if(j!=0)g.drawString(String.valueOf(j), x1, y1);else{g.drawString("12", x1, y1);} j++; i+=5; } g.setFont(fb); for(int i=0;i<60;i++){ x1=centerx+Double.valueOf(155*Math.sin(i*corner)).intValue(); y1=centery-Double.valueOf(155*Math.cos(i*corner)).intValue(); if(i%5!=0)g.fillOval(x1, y1, 5, 5); else{ g.setColor(Color.red); g.fillOval(x1-1, y1-1, 8, 8); g.setColor(c); }; } Graphics2D g2d=(Graphics2D)g; BasicStroke bs=(BasicStroke)g2d.getStroke(); g2d.setStroke(new BasicStroke(2.0f)); x1=centerx+Double.valueOf(slength*Math.sin(s*corner)).intValue(); y1=centery-Double.valueOf(slength*Math.cos(s*corner)).intValue(); g.drawLine(x1, y1, centerx, centery); g2d.setStroke(new BasicStroke(4.0f)); x1=centerx+Double.valueOf(mlength*Math.sin((m+s/60)*corner)).intValue(); y1=centery-Double.valueOf(mlength*Math.cos((m+s/60)*corner)).intValue(); g.drawLine(x1, y1, centerx, centery); g2d.setStroke(new BasicStroke(8.0f)); x1=centerx+Double.valueOf(hlength*Math.sin(5*(h+m/60)*corner)).intValue(); y1=centery-Double.valueOf(hlength*Math.cos(5*(h+m/60)*corner)).intValue(); g.drawLine(x1, y1, centerx, centery); g2d.setStroke(bs); g.setColor(Color.black); g.fillOval(centerx-6, centery-6, 12, 12); g.setColor(Color.red); g.fillOval(centerx-2, centery-2, 4, 4); g.setColor(c); g.setFont(new Font("楷体_GB2312",1,20)); g.drawString(year+"年"+month+"月"+day+"日", 135, 300); g.drawString("星期"+week, 170, 330); g.setFont(fb); //g.drawLine(15+(ScreenSize-20)/2, 0, 15+(ScreenSize-20)/2, this.getHeight()); //g.drawLine(0, 40+(ScreenSize-20)/2, this.getWidth(), 40+(ScreenSize-20)/2); } public void paintfinger(int s,Graphics g){ int x1=centerx+Double.valueOf(slength*Math.sin(s*corner)).intValue(); int y1=centery-Double.valueOf(slength*Math.cos(s*corner)).intValue(); g.drawLine(x1, y1, centerx, centery); } public void update(Graphics g){ if(offScreenImage==null){ offScreenImage=this.createImage(ScreenSize+40,ScreenSize+40); } Graphics goffScreenImage=offScreenImage.getGraphics(); Color c=g.getColor(); goffScreenImage.setColor(Color.green); goffScreenImage.fillRect(0, 0, 500,500); goffScreenImage.setColor(c); this.paint(goffScreenImage); g.drawImage( offScreenImage,0,0, null); } public static void main(String[] args) { Watch f=new Watch(); f.setFrame(); } class SetDate implements Runnable{ public void run() { while(true) { repaint(); cal=Calendar.getInstance(); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } } } }