아날로그 시계 만드는건데요....모르는 부분이 있어서요..
4차원
어떤 님이 올려주신 소스인데요....
이해가 잘 안가는 부분이 있어서요....
자세히 좀 가르쳐 주시면 좋겠습니다...
몰라서요...ㅠ.ㅠ
ㅎㅎ
아시겠지만, 애플릿입니다.
구조도 잘 모르겠고...ㅎㅎ
시계바늘 부분에서
double second = 2 * Math.PI * (totalSec - 15) / 60;
double minute = 2 * Math.PI * (totalSec - (15 * 60)) / (60 * 60);
double hour = 2 * Math.PI * (totalSec - (3 * 60 * 60)) / (12 * 60 * 60);15를 왜 빼주는거죠?
==========================소 스 부 분==========================
import java.awt.*;
import java.util.*;
import java.awt.Graphics;
public class AnalogWatch extends java.applet.Applet implements Runnable{
Thread runner;
Calendar rightNow;
int watchWidth = 300;
int watchHeight = 300;
int x = 50; // 시계의 시작점(x좌표)
int y = 50; // 시계의 시작점(y좌표)
int center = (watchHeight / 2) + x; // 시계의 중심
int totalSec = 0; // 시 * 60 * 60 + 분 * 60 + 초
public void init() {
setBackground(Color.LIGHT_GRAY); // 바탕색은 밝은회색
setSize(400, 400); // 크기지정
}
// runner 객체가 null 값을 가지면 쓰레드를 작동시켜라
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
// runner 객체가 null 값이 아니면 스레드를 중지시킨다
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
// 시계의 모형을 제작
public void boardSketch(Graphics g) {
String numberOfWatch = ;
String logo = asdf; // 로고작성
String profile = Made by sgw; // 시계제작자
String grade = 991096;
Font f = new Font(TimesRoman, Font.ROMAN_BASELINE, 20);
Font f1 = new Font(Georgia, Font.ITALIC, 25);
Font f2 = new Font(Arial, Font.ITALIC, 8);
// 단순한 원형시계가 아닌 사각안의 원형을 위해 필요
g.setColor(new Color(255,193,255));
g.fillRect(x-10, y-10, watchWidth + 20, watchHeight + 20);
g.setColor(new Color(0,35,109));
g.fillOval(x,y, watchWidth, watchHeight);
// 로고작성
g.setFont(f1);
g.setColor(new Color(200,200,200));
g.drawString(logo, center - 25, center - 65);
// 시계제작자 작성
g.setFont(f2);
g.setColor(new Color(200,200,200));
g.drawString(profile, center - 20, center + 70);
g.drawString(grade, center - 12, center + 80);
// 시계의 시 부분을 알 수 있도록 1~12까지의 숫자 작성
g.setFont(f);
g.setColor(Color.yellow);
for (int i = 1; i = 12; i++) {
numberOfWatch = i + ;
g.drawString(numberOfWatch, (int)(center - 6 + 130*Math.sin(2*Math.PI * i / 12)), (int)(center + 6 - 130*Math.cos(2*Math.PI * i / 12)));
}
}
// 시계바늘제작
public void timeStructure(Graphics g) {
double beforeSecond = 0.0;
double beforeMinute = 0.0;
double beforeHour = 0.0;
// 3시가 0도 이므로 12시 부분이 0도가 되게끔 수정
double second = 2 * Math.PI * (totalSec - 15) / 60;
double minute = 2 * Math.PI * (totalSec - (15 * 60)) / (60 * 60);
double hour = 2 * Math.PI * (totalSec - (3 * 60 * 60)) / (12 * 60 * 60);
// 계속 repaint() 되어야 하므로 값이 연속적으로 바뀌어야 한다.
beforeSecond = second;
beforeMinute = minute;
beforeHour = hour;
g.setColor(new Color(31, 255,255));
g.drawLine(center, center, center + (int)(130*Math.cos(second)), center + (int)(130*Math.sin(second))); // 초침
g.drawLine(center, center, center + (int)(105*Math.cos(minute)), center + (int)(105*Math.sin(minute))); // 분침
g.drawLine(center, center, center + (int)(70*Math.cos(hour)), center + (int)(70*Math.sin(hour))); // 시침
}
// 시계바늘과 시계 몸통의 결합
public void paint(Graphics g) {
boardSketch(g);
timeStructure(g);
}
// 쓰레드가 실행되는 동안
public void run() {
while(true) {
rightNow = Calendar.getInstance(); // 현재시간을 입력받는다 이 때 Calendar 를 사용할 수 있다
// 전체 초를 계산한다. 이는 시 *3600 + 분*60 + 초 로 계산이 가능하다 0도가 3시 방향이므로 빼주기 위해 반드시 필요하다.
totalSec = rightNow.get(Calendar.HOUR)*3600+ rightNow.get(Calendar.MINUTE)*60 + rightNow.get(Calendar.SECOND);
repaint();
try { // 예외처리 구문
Thread.sleep(1000);
}
catch(InterruptedException e) {
}
}
}
}