자바 아날로그시계만드는법질문
연꽃
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.calendar;
interface timed {
public void tick(timer t);
}class timer extends thread {
private timed target;
private int interval;
public timer(timed t, int i) {
target = t; interval = i;
setdaemon(true);
}
public void run() {
while (true) {
try {
sleep(interval);
target.tick(this);
}
catch(interruptedexception e) {}
finally{
}
}
}
}
public class timertest extends frame {
public timertest() {
setlayout(new gridlayout(2, 3));
add(new clockcanvas(seoul, 16));
add(new clockcanvas(chicago, 1));
add(new clockcanvas(london, 7));
add(new clockcanvas(moscow, 10));
add(new clockcanvas(paris, 8));
add(new clockcanvas(beijing, 15));
}
public static void main(string[] args) {
frame f = new timertest();
f.setsize(450, 300);
f.setbackground(color.gray);
windowdestroyer listener = new windowdestroyer();
f.addwindowlistener(listener);
f.setvisible(true);
}
}class clockcanvas extends canvas implements timed {
public int basex = 10;
public int basey = 10;
public int clockw = 100;
public int clockh = 100;
public int center = basex + clockh/2;
private int seconds = 0;
private string city;
private int offset;
private final int local = 16;public clockcanvas(string c, int off) {
city = c; offset = off;
new timer(this, 1000).start();
setsize(125, 125);
}
public void paint(graphics g) {
g.setcolor(color.white);
g.filloval(basex,basey, clockw, clockh);
double hourangle = 2 * math.pi * (seconds - 3 * 60 * 60) / (12 * 60 * 60);
double minuteangle = 2 * math.pi * (seconds - 15 * 60) / (60 * 60);
double secondangle = 2 * math.pi * (seconds - 15) / 60;
system.out.println(hourangle : + hourangle);
system.out.println(minuteangel : + minuteangle);
system.out.println(secondangle : + secondangle);
g.setcolor(color.black);
g.drawline(center, center, center + (int)(30 * math.cos(hourangle)),
center + (int)(30 * math.sin(hourangle)));
g.drawline(center, center, center + (int)(40 * math.cos(minuteangle)),
center + (int)(40 * math.sin(minuteangle)));
g.setcolor(color.blue);
g.drawline(center, center, center + (int)(45 * math.cos(secondangle)),
center + (int)(45 * math.sin(secondangle)));
g.setcolor(color.black);
g.drawstring(city, basex, basey+clockh+10);
}
public void tick(timer t) {
calendar cal = calendar.getinstance();
seconds = ((cal.get(calendar.hour) - local + offset)* 60 * 60) +
(cal.get(calendar.minute) * 60) + cal.get(calendar.second);
system.out.println(tick 메소드입니다.seconds : + seconds);
system.out.println(offset : + offset);
system.out.println(local : + local);
system.out.println(hour : + cal.get(calendar.hour_of_day ));
system.out.println(calendar.minute * 60 : + (cal.get(calendar.minute) * 60));
system.out.println(calendar.second : + cal.get(calendar.second));
//system.out.println(gettime : + calendar.gettime() );
repaint();
}
}
class windowdestroyer extends windowadapter {
public void windowclosing(windowevent e) {
system.exit(0);
}
}
자바 책을보며 아날로그시계만들기를했습니다.
근데 thread,callserially,runnable 이세가지를 각각이용해서 아날로그시계를만들수있다고하더라구요...
어떻게만드는지 알고싶습니다. 자바책에는안나오더군요