나는 시계 애플릿을 만들고 있으며, 거의 끝났지 만 여전히해야 할 일이 있습니다. 초침이 움직일 때마다 시계를 "똑딱"하게 만들고 싶지만 * 진드기 * 소리에 대한 코드를 어디에 넣을 지 알 수 없습니다. 여기에 애플릿의 코드입니다애플릿에 사운드를 생성하는 코드를 삽입하려면 어떻게해야합니까?
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.URL;
import java.text.*;
public class ClockApplet extends Applet implements Runnable {
Ellipse2D line1 = new Ellipse2D.Float(100, 150, 200, 200);
int width, height;
Thread t = null;
boolean threadSuspended;
int hours = 0, minutes = 0, seconds = 0;
String timeString = "";
public void init() {
width = getSize().width;
height = getSize().height;
setBackground(Color.white);
}
public void start() {
if (t == null) {
t = new Thread(this);
t.setPriority(Thread.MIN_PRIORITY);
threadSuspended = false;
t.start();
} else {
if (threadSuspended) {
threadSuspended = false;
synchronized (this) {
notify();
}
}
}
}
public void stop() {
threadSuspended = true;
}
public void run() {
try {
while (true) {
// Here's where the thread does some work:
Calendar cal = Calendar.getInstance();
hours = cal.get(Calendar.HOUR_OF_DAY);
if (hours > 12)
hours -= 12;
minutes = cal.get(Calendar.MINUTE);
seconds = cal.get(Calendar.SECOND);
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss",
Locale.getDefault());
Date date = cal.getTime();
timeString = formatter.format(date);
// Now the thread checks to see if it should suspend itself
if (threadSuspended) {
synchronized (this) {
while (threadSuspended) {
wait();
}
}
}
repaint();
t.sleep(1000); // interval given in milliseconds
}
} catch (InterruptedException e) {
}
}
void drawHand(double angle, int radius, Graphics g) {
angle -= 0.5 * Math.PI;
int x = (int) (radius * Math.cos(angle));
int y = (int) (radius * Math.sin(angle));
g.drawLine(width/2, height/2, width/2 + x, height/2 + y);
}
void drawWedge(double angle, int radius, Graphics g) {
angle -= 0.5 * Math.PI;
int x = (int) (radius * Math.cos(angle));
int y = (int) (radius * Math.sin(angle));
angle += 2 * Math.PI/3;
int x2 = (int) (5 * Math.cos(angle));
int y2 = (int) (5 * Math.sin(angle));
angle += 2 * Math.PI/3;
int x3 = (int) (5 * Math.cos(angle));
int y3 = (int) (5 * Math.sin(angle));
g.drawLine(width/2 + x2, height/2 + y2, width/2 + x, height/2
+ y);
g.drawLine(width/2 + x3, height/2 + y3, width/2 + x, height/2
+ y);
g.drawLine(width/2 + x2, height/2 + y2, width/2 + x3, height/2
+ y3);
}
void drawCircle(Graphics g) {
g.drawOval(0, 0, 200, 200);
}
public void paint(Graphics g) {
g.setColor(Color.black);
drawWedge(2 * Math.PI * hours/12, width/5, g);
drawWedge(2 * Math.PI * minutes/60, width/3, g);
g.setColor(Color.red);
drawHand(2 * Math.PI * seconds/60, width/2, g);
g.setColor(Color.black);
g.drawString(timeString + " ET", 10, height - 10);
g.setFont(new Font("Arial", Font.PLAIN, 30));
g.drawString("12", 85, 30);
g.drawString("1", 140, 40);
g.drawString("2", 170, 70);
g.drawString("3", 180, 110);
g.drawString("4", 170, 150);
g.drawString("5", 140, 180);
g.drawString("6", 92, 195);
g.drawString("7", 46, 180);
g.drawString("8", 16, 150);
g.drawString("9", 5, 110);
g.drawString("10", 16, 70);
g.drawString("11", 46, 40);
drawCircle(g);
}
}
는 그리고 여기 * 틱 코드입니다 * 사운드 :
try {
Clip tick = AudioSystem.getClip();
URL clipURL = new URL("file://C:/users/owner/desktop/Tick.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(clipURL);
tick.open(ais);
tick.start();
} catch (Exception e) {
System.out.println("Error playing sound!");
}
내가 어디 애플릿 코드로 사운드 코드를 삽입하는 것입니다 알아야 할 모든 . 나는 다양한 장소를 시도했지만 아무도 작동하지 않는 것 같습니다.
* "나는 다양한 곳을 시도했지만 아무 것도 작동하지 않는 것 같습니다."* 프로그래밍은 마술이 아닙니다. 비트 (t.sleep (1000);)가 문제를 복잡하게 만듭니다. 하지만 먼저 다른 문제들 .. –
1) 애플릿을 코딩하는 이유는 무엇입니까? 사양으로 인해 예정된 경우. 선생님이 [왜 CS 교사가 Java 애플릿 교육을 중단해야하는지] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)를 참조하십시오. 2) Swing이 아닌 AWT를 사용해야하는 이유 AWT 구성 요소를 사용하여 포기해야하는 여러 가지 이유 때문에 [AWT를 통한 스윙 엑스트라] (http://stackoverflow.com/a/6255978/418556)에서이 대답을 참조하십시오. 이전 AWT 기반 API를 지원해야하는 경우 [중량 및 중량 구성 요소 혼합] (http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html)을 참조하십시오. –
실제로 내가 한 일은 다른 코드에서 대부분의 코드를 가져 왔지만 그 코드는 24 시간 내내 돌아 다니는 코드 일뿐입니다. 이제 시계에는 숫자가 표시되고 초침은 빨간색으로 표시되며 시계 주위처럼 원이 그려져 있습니다. 나는 그것을 진드기로 만드는 법을 알 필요가있다. – livefree75