import java.applet.*; import java.awt.*; import java.util.*; public class watch2 extends Applet implements Runnable{ Image fb; Graphics gg; Color c; Thread th=null; public void init(){fb = createImage(200,200);gg = fb.getGraphics();} public void start(){if(th==null){th=new Thread(this);th.start();}} public void run(){ for( ; ; ){ repaint(); try{Thread.sleep(50);} catch(InterruptedException e){} } } public void update(Graphics g){paint(g);} public void stop(){if(th!=null){th.stop();th=null;}} public void paint(Graphics g){ int hour,minute,second; double a,a1,a2,a3; int i,r,x,x1,x2,x3,y,y1,y2,y3; Calendar d=Calendar.getInstance(TimeZone.getTimeZone("JST")); hour = d.get(d.HOUR); minute = d.get(d.MINUTE); second = d.get(d.SECOND); c = new Color(255,255,255); gg.setColor(c); gg.fillRect(0,0,200,200); /******時計のデザイン*******/ c = new Color(220,240,220); gg.setColor(c); gg.fillRect(5,5,190,190); c = new Color(0,0,0); gg.setColor(c); gg.drawOval(24,24,152,152); gg.drawOval(25,25,150,150); gg.drawOval(26,26,148,148); r = 70; for(i=0;i<60;++i){ a = Math.PI*i/30.0 - Math.PI/2.0; x = 100+(int)( r*Math.cos(a) ); y = 100+(int)( r*Math.sin(a) ); gg.fillOval(x-1,y-1,3,3); } r = 70; for(i=0;i<60;++i){ if( i % 5 == 0 ){ a = Math.PI*i/30.0 - Math.PI/2.0; x = 100+(int)( r*Math.cos(a) ); y = 100+(int)( r*Math.sin(a) ); gg.fillOval(x-2,y-2,4,4); } } /********時針を書く********/ r = 40; a1 = Math.PI*hour/6.0 + Math.PI*minute/360.0- Math.PI/2.0; x1 = 100+(int)( r*Math.cos(a1) ); y1 = 100+(int)( r*Math.sin(a1) ); r = 10; a2 = Math.PI*hour/6.0 + Math.PI*minute/360.0- Math.PI/2.0+2*Math.PI/3; x2 = 100+(int)( r*Math.cos(a2) ); y2 = 100+(int)( r*Math.sin(a2) ); a3 = Math.PI*hour/6.0 + Math.PI*minute/360.0- Math.PI/2.0+2*Math.PI*2/3; x3 = 100+(int)( r*Math.cos(a3) ); y3 = 100+(int)( r*Math.sin(a3) ); int xp1[] = {x1,x2,x3}; int yp1[] = {y1,y2,y3}; Polygon pg1 = new Polygon(xp1,yp1,3); c = new Color(0,0,255); gg.setColor(c); gg.fillPolygon(pg1); /********分針を書く********/ r = 50; a1 = Math.PI*minute/30.0 + Math.PI*second/1800.0 - Math.PI/2.0; x1 = 100+(int)( r*Math.cos(a1) ); y1 = 100+(int)( r*Math.sin(a1) ); r = 10; a2 = Math.PI*minute/30.0 + Math.PI*second/1800.0 - Math.PI/2.0+2*Math.PI/3; x2 = 100+(int)( r*Math.cos(a2) ); y2 = 100+(int)( r*Math.sin(a2) ); a3 = Math.PI*minute/30.0 + Math.PI*second/1800.0 - Math.PI/2.0+2*Math.PI*2/3; x3 = 100+(int)( r*Math.cos(a3) ); y3 = 100+(int)( r*Math.sin(a3) ); int xp2[] = {x1,x2,x3}; int yp2[] = {y1,y2,y3}; Polygon pg2 = new Polygon(xp2,yp2,3); c = new Color(0,255,0); gg.setColor(c); gg.fillPolygon(pg2); /********秒針を書く********/ r = 65; a1 = 2*Math.PI*second/60.0 - 2*Math.PI/4.0; x1 = 100+(int)( r*Math.cos(a1) ); y1 = 100+(int)( r*Math.sin(a1) ); r = 10; a2 = Math.PI*second/30.0 - Math.PI/2.0+2*Math.PI/3; x2 = 100+(int)( r*Math.cos(a2) ); y2 = 100+(int)( r*Math.sin(a2) ); a3 = Math.PI*second/30.0 - Math.PI/2.0+2*Math.PI*2/3; x3 = 100+(int)( r*Math.cos(a3) ); y3 = 100+(int)( r*Math.sin(a3) ); int xp3[] = {x1,x2,x3}; int yp3[] = {y1,y2,y3}; Polygon pg3 = new Polygon(xp3,yp3,3); c = new Color(255,0,0); gg.setColor(c); gg.fillPolygon(pg3); //********************************* c = new Color(0,0,0); gg.setColor(c); gg.fillOval(90,90,20,20); gg.drawString(hour+":"+minute+":"+second,80,150); int w=getSize().width; int h=getSize().height; g.drawImage(fb,0,0,w,h,0,0,200,200,this); } }