애플릿에서의 버튼 이벤트..
갤2
아래 애플릿은
아날로그 시계입니다
run()에 보시면 sleep타임이 1000이고 즉 1000마다 새로 repaint()를 시행하게 되는 애플릿인데요
애플릿내부에 버튼을 추가하여 [ 시작. 중지 ]
해당 버튼을 누를경우 애플릿이 재시작 되거나 중지[즉 wait상태]로 바꿀방법이 없을까요?
애플릿에 button을 넣으려고 시도하는것 조차 안되는군요 어디에 button 생성자를 넣어야 애플릿에 버튼이 나오는거죠?
import java.awt.*;
import java.applet.*;
import java.util.*;
public class ActiveClock extends Applet implements Runnable {
Thread clock;
double radius1, radius2;
int pointX[] = new int[4];
int pointY[] = new int[4];
MediaTracker tracker;
Image imageBuffer;
int oldHour = -1, oldMinute = -1, oldSecond = -1;
public void init() {
tracker = new MediaTracker (this);
}
public void start() {
if (imageBuffer == null) {
imageBuffer = createImage (0, 0);
}
if (clock == null) {
clock = new Thread (this);
clock.start(); // 스레드 시작
}
}
public void stop() {
if (clock != null) {
clock = null;
imageBuffer = null;
}
}
public void run() {
try {
tracker.waitForAll(); // 로딩이 완료될 때까지 기다린다.
} catch (InterruptedException e) {
return;
}
while (Thread.currentThread() == clock) {
repaint();
try {
clock.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
} // end of run()
public void update(Graphics g) {
// 현재 시각을 구한다.
String now = new java.util.Date().toString();
int hour = Integer.parseInt(now.substring(11,13));
int minute = Integer.parseInt(now.substring(14,16));
int second = Integer.parseInt(now.substring(17,19));
// 시침, 분침, 시침의 위치를 계산한 후 그린다.
if ((hour!=oldHour) || (minute!=oldMinute) || (second!=oldSecond)) {
Graphics graphBuffer = imageBuffer.getGraphics();
drawClockShape(graphBuffer);
// 초침의 위치를 결정 : 초침은 단독으로 작동한다.
double posSecond = Math.PI * second/30.0;
 sp; radius1 = 150 * 0.9;
int endX = (int)Math.round(150 + radius1 * Math.sin(posSecond));
int endY = (int)Math.round(150 - radius1 * Math.cos(posSecond));
graphBuffer.setColor(Color.red);
graphBuffer.drawLine(150, 150, endX,endY);
// 분침의 위치를 계산 : 분침은 초침에 따라서 움직인다.
double posMinute = Math.PI*(minute/30.0 + second/1800.0);
radius1 = 150 * 0.8;
radius2 = 150 * 0.04;
// 분침의 모양을 구하고 그린다.
pointX[0]=(int)Math.round(150 - 2*radius2*Math.sin(posMinute))-1;
pointY[0]=(int)Math.round(150 + 2*radius2*Math.cos(posMinute))-1;
pointX[1]=(int)Math.round(150 - radius2 * Math.cos(posMinute));
pointY[1]=(int)Math.round(150 - radius2 * Math.sin(posMinute));
pointX[2]=(int)Math.round(150 + radius1 * Math.sin(posMinute))+1;
pointY[2]=(int)Math.round(150 - radius1 * Math.cos(posMinute))+1;
pointX[3]=(int)Math.round(150 + radius2 * Math.cos(posMinute));
pointY[3]=(int)Math.round(150 + radius2 * Math.sin(posMinute));
graphBuffer.setColor(Color.black);
graphBuffer.fillPolygon(pointX, pointY, 4);
// 시침의 위치를 결정 : 시침은 분침에 따라서 움직인다.
double posHour = Math.PI*(hour/6.0 + minute/360.0);
radius1 = 150 * 0.7;
radius2 = 150 * 0.04;
// 시침의 모양을 구하고 그린다.
pointX[0]=(int)Math.round(150 - 2*radius2*Math.sin(posHour))-1;
pointY[0]=(int)Math.round(150 + 2*radius2*Math.cos(posHour))-1;
pointX[1]=(int)Math.round(150 - radius2 * Math.cos(posHour));
pointY[1]=(int)Math.round(150 - radius2 * Math.sin(posHour));
pointX[2]=(int)Math.round(150 + radius1 * Math.sin(posHour))+1;
pointY[2]=(int)Math.round(150 - radius1 * Math.cos(posHour))+1;
pointX[3]=(int)Math.round(150 + radius2 * Math.cos(posHour));
pointY[3]=(int)Math.round(150 + radius2 * Math.sin(posHour));
graphBuffer.setColor(Color.black);
graphBuffer.fillPolygon(pointX, pointY, 4);
oldHour = hour;
oldMinute = minute;
oldSecond = second;
}
g.drawImage(imageBuffer, 0, 0, null);
}
public void paint(Graphics g) {
drawClockShape(g);
}
private void drawClockShape(Graphics g) {
double bulletPosition;
double radius = 150 * 0.9;
g.setColor(Color.white);
g.fillRect(0, 0, 300, 300);
for (int i = 1; i = 12; i++) {
bulletPosition = Math.PI*(0.5 - i / 6.0);
int X = (int)Math.floor(150 + radius * Math.cos(bulletPosition));
int Y = (int)Math.floor(150 - radius * Math.sin(bulletPosition));
g.setColor(Color.blue);
g.fill3DRect(X - 2, Y - 2, 4, 4, true);
}
for (int i = 1; i = 60; i++) {
if ((i % 5) != 0) {
bulletPosition = Math.PI * i / 30.0;
int X = (int)Math.floor(150 + radius * Math.cos(bulletPosition));
int Y = (int)Math.floor(150 - radius * Math.sin(bulletPosition));
g.setColor(Color.black);
g.fill3DRect( X - 2, Y - 2, 2, 2, false);
}
}
// 시계 숫자 표시
g.setColor(Color.black);
g.drawString(3, 150+140, 150+3);
 sp; g.drawString(6, 150-3, 150+150);
g.drawString(9, 150-150, 150+3);
g.drawString(12, 150-5, 150-140);
// 현재 시각 표시
String today_now = new java.util.Date().toString();
String sub = today_now.substring(0,20);
g.drawString(sub, 150-50, 250);
}
}
-
늘솔길
음.. 먼저 소스를 어디서 가져오셨나봐요. 현재 소스는 Applet에다가 그림 (시계) 을 그리고 있는거 잖아요. 쉽게 말하면, 그림판 같은걸로 쓰고 있죠.. 근데, 그림판으로 쓰고 있다면 그위에 버튼을 올릴수는 없어요. 님께서 원하시는 버튼을 추가 하고 싶다면, Applet에 바로 그림을 그릴게 아니라, Canvas 같은 클래스를 사용해서, 그곳에 그리구요. Applet에 Canvas와 Button을 더하는 방식으로 전체적은 구조를 변경하셔야 할것 같
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2696792 | Doctype 문의 | 떠나간그녀 | 2025-06-02 |
2696683 | 컴파일된 클립 질문 드립니다. | 흰추위 | 2025-06-01 |
2696656 | C질문요 (4) | 블랙캣 | 2025-05-31 |
2696504 | 플래시 위에 div 올리기 (5) | 큰꽃늘 | 2025-05-30 |
2696458 | 제가 만든 소스 한번 봐주시고 수정 할 꺼 있으면 말해주세요. (실행은 되지만 깜빡거리네요) | 이플 | 2025-05-29 |
2696434 | 퍼센트 레이아웃 질문인데요.. | 나츠 | 2025-05-29 |
2696372 | %=open_main%, %=open_sub% 가 뭘까요? (9) | 행복녀 | 2025-05-29 |
2696347 | 콘솔 프로그램 질문 | 상큼한캔디 | 2025-05-28 |
2696320 | c언어 scanf 함수를 이요해 문자열 입력 받을 시 질문 있습니다. | 슬아라 | 2025-05-28 |
2696292 | 익스플로러9이상에서만 이상한 보더가 보이는데 삭제할수 있나요? | 망고 | 2025-05-28 |
2696263 | 프로그래밍 공부시작 질문 (6) | 진이 | 2025-05-28 |
2696206 | SK2의 플래시를 밴치마킹하려고하는데요.. (1) | 비내리던날 | 2025-05-27 |
2696179 | ie7에서 사라지지가 않네요. (2) | 빛길 | 2025-05-27 |
2696150 | div에 스크롤 생기게 하려면... (2) | 에드가 | 2025-05-27 |
2696123 | 자료구조론 공부중인데 | 김자영 | 2025-05-26 |
2696094 | exe 파일 | 제철 | 2025-05-26 |
2696043 | 제이쿼리 .scroll() 관련 질문드립니다 | 이거이름임 | 2025-05-26 |
2695984 | 마크업상으로 하단에 있으나 우선적으로 이미지파일을 다운로드받는 방법 (1) | 들꿈 | 2025-05-25 |
2695934 | tr 속성값 (9) | 새 | 2025-05-25 |
2695905 | ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) | 슬아라 | 2025-05-24 |