最近在学习Java线程,照着书上例子写的钟表图形程序,对线程有一定的理解,但是发觉对图形编程的把握不够很多函数还不能把握~~
以下是钟表例子的代码
package hello;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
public class T extends Applet implements Runnable{
Thread tHour = null,tMinute = null,tSecond = null;//表示时针,分针和秒针的线程
int hour_a,hour_b,minute_a,minute_b,second_a,second_b;//表示时针,分针,秒针端点的整型变量
int hour = 0,minute = 0,second = 0;//获取当前时间的整型变量
//绘制时针,分针和秒针的Graphics对象
Graphics g_second = null,g_minute = null,g_hour =null;
//存放表盘刻度的数组,供指针走动时使用
double point_x[] = new double[61],point_y[] = new double[61];
//存放表盘刻度的数组,供绘制表盘使用
double scaled_x[] = new double[61],scaled_y[] = new double[61];
//判定小程序是否重新开始的变量
int start_count = 0;
public void init(){
g_hour = this.getGraphics();
g_hour.setColor(Color.CYAN);
g_second = this.getGraphics();
g_second.setColor(Color.RED);
g_minute = this.getGraphics();
g_minute.setColor(Color.blue);
g_second.translate(200,200);//进行坐标系统变换,将新坐标系原点设在(200,200)处
g_minute.translate(200,200);
g_hour.translate(200,200);
point_x[0] = 0; point_y[0] = -120;//各个时针12点处的位置坐标(按新坐标系的坐标)
scaled_x[0] = 0;scaled_y[0] = -140;//12点处的刻度位置坐标(按新坐标系的坐标)
double jiaodu = 6*Math.PI/180;
//表盘分割成60分,将分割点的坐标存放在数组中
for(int i = 0; i < 60; i++){
point_x[i+1] = point_x[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*point_y[i];
point_y[i+1] = point_y[i]*Math.cos(jiaodu) + point_x[i]*Math.sin(jiaodu);
}
point_x[60] = 0; point_y[60] = -120;
for(int i = 0; i < 60; i++){
scaled_x[i+1] = scaled_x[i]*Math.cos(jiaodu)-Math.sin(jiaodu)*scaled_y[i];
scaled_y[i+1] = scaled_y[i]*Math.cos(jiaodu) + Math.sin(jiaodu)*scaled_x[i];
}
scaled_x[60]= 0;
scaled_y[60] = -140;
}
public void start(){
//每当小程序重新开始时,首先消灭线程,然后重新开始创建线程
if(start_count >= 1){
tSecond.interrupt();
tMinute.interrupt();
tHour.interrupt();
}
tSecond = new Thread(this);
tMinute = new Thread(this);