import java.applet.*; import java.awt.*; import java.util.Calendar; //★★★ 試験問題のJavaへの移植を試みる。 ★★★ // 例題 // 平成12年度春期 第2種情報処理技術者試験 // 午後 選択-問7(C言語) // 時計を描画するプログラム // 移植:城江 左京 //●「displayClock」関数が受け取る引数用の構造体(クラス) class ACLOCK { boolean ready; int hour, minute, second; int cx, cy; int radius; int plateX[]=new int[360]; int plateY[]=new int[360]; } //●本体である「displayClock」関数を含むクラス //※コンストラクタと本関数以外は、別に定義する。 class DrawClock extends Support_DrawClock { public DrawClock( Graphics gr ) { super( gr ); } public void displayClock( ACLOCK pt ) { final double rad = 3.14159265359 / 180.0; final int largeMark = 6; final int smallMark = 2; final int hourHand = 7; final int minuteHand = 3; final int secondHand = 1; int angle, size=0, hx, hy, w, x, y; if(pt.ready == false) { for( w=-90,x=y=0; w<270; w++,x++,y++ ) { pt.plateX[x] = pt.cx + (int)(pt.radius*Math.cos(w*rad)); pt.plateY[y] = pt.cy + (int)(pt.radius*Math.sin(w*rad)); } pt.ready = true; } for( angle=0; angle<360; angle+=6 ) { if( angle%30 == 0 )size=largeMark; else size=smallMark; drawCircle( pt.plateX[angle], pt.plateY[angle], size ); } angle = pt.second*6; drawLine( pt.cx, pt.cy, pt.plateX[angle], pt.plateY[angle], secondHand); angle = pt.minute*6 + pt.second/10; drawLine( pt.cx, pt.cy, pt.plateX[angle], pt.plateY[angle], minuteHand); if( pt.hour>12 )pt.hour -= 12; angle = pt.hour*30 + pt.minute/2; hx=( pt.cx + pt.plateX[angle] )/2; hx=( hx + pt.plateX[angle] )/2; hy=( pt.cy + pt.plateY[angle] )/2; hy=( hy + pt.plateY[angle] )/2; drawLine( pt.cx, pt.cy, hx, hy, hourHand ); drawCircle(pt.cx, pt.cy, pt.radius/10 ); } } //●「displayClock」関数をテストするためのドライバ //  描画するためのイメージバッファを用意し、 // 約1秒間に一回、ACLOCK構造体に値を代入して // displayClock関数を呼び出す。 public class Control_displayClock extends Applet implements Runnable { private DrawClock clock; private ACLOCK a; private Image image; private Graphics image_gr,applet_gr; private Thread thread; private Dimension size; public void init() { size = getSize(); image = createImage( size.width, size.height ); image_gr = image.getGraphics(); applet_gr = getGraphics(); a = new ACLOCK(); clock = new DrawClock( image_gr ); a.cx = size.width / 2; a.cy = size.height / 2; a.radius = ( a.cx