// 時計を表示 [Clock.java] import java.applet.*; import java.awt.*; import java.util.*; // Dateを使用するため public class Clock extends Applet implements Runnable{ Thread th=null; // スレッドオブジェクトの変数 int x,y,old_X,old_Y, r,x0,y0,w,h,ang; int sdo,mdo,hdo,old_M,old_H; TimeZone tz =TimeZone.getTimeZone("JST"); // JDK 1.1 final double RAD=Math.PI/180.0; //===================== initメソッド ===================== public void init() { setBackground(new Color(0,0,192)); // Background old_X=-1; // 秒の初期値 } //end init public void start() { if (th == null) { th=new Thread(this); // スレッドの作成 th.start(); // スレッドの開始 } } //end start public void run() { while( true ) { try { repaint(); th.sleep(1000); // sleepする時間(ミリ秒) } catch (InterruptedException e) { } // 例外処理なし } } //end run public void stop() { if (th != null) { th.stop(); // スレッドの停止 th=null; } } //end stop //==================== updateメソッド ==================== public void update( Graphics g ) { paint(g); // 画面をクリアしないでpaintを呼び出す } //end update //================= グラフィックスの表示 ================= public void paint( Graphics g ) { int hh,mm,ss; String st; h=getSize().height; // 縦の大きさ g.setColor(Color.white); g.drawOval(30,30,h-60,h-60); //(a) 円(内円) g.drawOval(32,32,h-64,h-64); //(a) 円(外円) r=h/2-30; // 円の半径 x0=30+r-5; //(b) 文字の開始位置をずらす y0=30+r-5; //(b) 文字の開始位置をずらす ang=60; // 数字の位置の角度 for (int i=1; i<=12; i++) { x=(int)((r+10)*Math.cos(RAD*ang)+x0); //(c) y=(int)((r+10)*Math.sin(RAD*ang)+y0); //(c) g.drawString(""+i,x,h-y); // 文字盤 ang-=30; //(c) 30度ずらす } x0=30+r; y0=30+r; // 円の中心 Calendar now=Calendar.getInstance(tz); //(d) 時間の取得 hh=now.get(Calendar.HOUR_OF_DAY); //(d) 時の取得 JDK 1.1 mm=now.get(Calendar.MINUTE); //(d) 分の取得 JDK 1.1 ss=now.get(Calendar.SECOND); //(d) 秒の取得 JDK 1.1 g.setColor(Color.pink); g.fillRect(0,0,60,18); // pinkの長方形で塗り潰す g.setColor(Color.blue); if (hh < 10) st="0"+hh; else st=""+hh; if (mm < 10) st=st+":0"+mm; else st=st+":"+mm; if (ss < 10) st=st+":0"+ss; else st=st+":"+ss; g.drawString(st,0,15); // 時間の表示 // sdo=90-ss*6; //(e) 秒針の角度の計算 mdo=90-mm*6; //(e) 長針 360÷60=6 hdo=90-hh*30-mm/2; //(e) 短針 360÷12=30 if (old_X > 0) { // 最初か? g.setColor(getBackground()); // 背景色の取り出し g.drawLine(x0,y0,old_X,(h-old_Y)); // 秒針消去 } else { old_M=mdo; //(f) 長針の角度の記録 old_H=hdo; //(f) 短針の角度の記録 } g.setColor(Color.yellow); x=(int)((r-8)*Math.cos(RAD*sdo)+x0); y=(int)((r-8)*Math.sin(RAD*sdo)+y0); g.drawLine(x0,y0,x,(h-y)); // 秒針を描く old_X=x; // 短針の角度の記録 old_Y=y; // 短針の角度の記録 if (mdo != old_M) { //(f) 長針がずれたか? line(g,old_M,(int)(r*0.7),getBackground()); // 長針消去 old_M=mdo; // 長針の角度の記録 } if (hdo != old_H) { //(f) 短針がずれたか? line(g,old_H,(int)(r*0.5),getBackground()); // 短針消去 old_H=hdo; // 短針の角度の記録 } line(g,mdo,(int)(r*0.7),Color.green); //(g) 長針の表示 line(g,hdo,(int)(r*0.5),Color.red); //(g) 短針の表示 } // end paint //===== t:角度 n:長さ c:カラー (短針または、長針を描く) ===== public void line(Graphics g, int t, int n, Color c) { int [] xp = new int[4]; // 針を描くための多角形の配列 int [] yp = new int[4]; xp[0]=x0; // 中心座標 yp[0]=y0; // 中心座標 xp[1]= (int)((n-10)*Math.cos(RAD*(t-4))+x0); yp[1]=h-(int)((n-10)*Math.sin(RAD*(t-4))+y0); xp[2]= (int)( n *Math.cos(RAD* t )+x0); yp[2]=h-(int)( n *Math.sin(RAD* t )+y0); xp[3]= (int)((n-10)*Math.cos(RAD*(t+4))+x0); yp[3]=h-(int)((n-10)*Math.sin(RAD*(t+4))+y0); g.setColor(c); g.fillPolygon(xp,yp,4); // 塗り潰し多角形 } // end line } //end Clock