관리 메뉴

nkdk의 세상

자바 Language 29차 예제파일들 애플릿 본문

My Programing/JAVA

자바 Language 29차 예제파일들 애플릿

nkdk 2008. 3. 8. 01:24
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class Applet4 extends JApplet implements ActionListener{
private Image img1, img2;
private AudioClip ac;
private JButton btnStart = new JButton("시작");
private JButton btnStop = new JButton("중지");
private boolean bool;

public void init() { // 웹페이지 로딩시 사용된다.
String pic1 = "pic1.jpg";
String pic2 = "pic2.jpg";
String sound = "town.mid";

img1 = this.getImage(this.getDocumentBase(), pic1);
img2 = this.getImage(this.getDocumentBase(), pic2);
ac = this.getAudioClip(this.getDocumentBase(), sound);
// getDocumentBase() -> 현재 애플릿을 포함한 url을 리턴한다.

this.setLayout(new BorderLayout());
Panel pn = new Panel(new FlowLayout());
pn.add(btnStart);
pn.add(btnStop);

this.add("South", pn);
}

public void start() { // 다른 웹 페이지 수행 후 돌아 올 때
btnStart.addActionListener(this);
btnStop.addActionListener(this);
}

public void paint(Graphics g) { // 애플릿 화면이 그려질 때
if(bool) {
g.drawImage(img1, 20, 20, 250, 150, this);
}else{
g.drawImage(img2, 20, 20, 250, 150, this);
}
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnStart) {
ac.loop(); // 무한 반복하라.
bool = true;
this.repaint();
}else if(e.getSource() == btnStop) {
ac.stop();
bool = false;
this.repaint();
}
}

}


// 애플릿 추가

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>애플릿 연습4</h1>
<applet code="Applet4.class" width"300" height="200"></applet>
</body>
</html>