init()
start()
stop()
destroy()
애플릿 적재
- 애플릿 초기화. 처음 시스템에
적재될때 한번만 브라우저에 의해서 자동으로 호출.
애플릿 시작
- Init() 호출 후, 브라우저가
애플릿이 포함되어 잇는 문서를 방문할 때 마다 호출
다른 브라우저로 이동
- 브라우저가 다른 페이저로
이동할 때, destroy()가 호출되기 직전 호출
메모리에서 제거
- 애플릿에 의해 할당된
리소스 환수를 위해 브라우저가 호출
페이지로 되돌아 옴
Introduction
(Cont.)
참고) OOP에서의 method overload
cf) override
Introduction
(Cont.)
Introduction
(Cont.)
Exception 클래스는 대부분의 프로그램들이 처리해야 하는 예외 상황들을 나타내는 예외 클래스의 부모 클래스이다.
명시적으로 - try … catch 구문 - 예외를 처리해야 한다.
애플릿에서 관련된 정보 가져오기
public class AppletBases extends Applet {
public void paint(Graphics g) {
g.drawString("Codebase: " + getCodeBase().toString(), 10, 40);
g.drawString("Documentbase: " + getDocumentBase().toString(), 10, 65);
}
}
애플릿이 들어있는 디렉토리
애플릿이 내장된 HTML 문서가 들어있는 디렉토리와 문서 이름
애플릿에서 이미지 다운로드하기
public class ImageView extends
Applet {
Image theImage;
public void init() {
try {
URL u = new URL(getCodeBase(), getParameter("IMAGE"));
theImage = getImage(u);
} catch (MalformedURLException e) {
System.err.println(e); } }
public void paint(Graphics g) {
g.drawImage(theImage,0,0,this); }}
<APPLET CODE=ImageView WIDTH=500 HEIGHT=500>
<PARAM NAME="IMAGE" VALUE="titleimage.jpg">
</APPLET>
HTML 소스보기
getImage()의 문제점
- 서버에 이미지가 실제로 존재하는지
확인하기도 전에 제어를 넘긴다.
- 이미지는 프로그램 중의 일부가 실제로 그리기
시작하기 전까지, 또는 강제로 이미지가 적재를
시작하도록 할 때까지 적재되지 않는다.
애플릿에서 사운드 다운로드 하기
public class PlaySound extends Applet {
public void init() {
try {
URL u = new URL(getCodeBase(), getParameter("SOUND"));
play(u);
} catch (MalformedURLException e) {
System.err.println(e); } } }
ex) PlaySound.html
<APPLET CODE=RelativeGong WIDTH=500 HEIGHT=500>
<PARAM NAME="SOUND" VALUE="spacemusic.au">
</APPLET>
애플릿에서 사운드 다운로드 하기 (Cont.)
import java.applet.AudioClip;
public class RelativeGong
extends Applet implements Runnable{
AudioClip theGong;
Thread t;
public void init() {
try {
URL u = new URL(getCodeBase(),
getParameter("SOUND"));
theGong = getAudioClip(u);
if (theGong != null) {
t = new Thread(this); t.start();
} } catch (MalformedURLException e) {
System.err.println(e); }}
/* resume() and suspend () are deprecated
public void start() { t.resume(); }
public void stop() { t.suspend(); }
*/
public void run() {
Thread.currentThread()
.setPriority(Thread.MIN_PRIORITY);
while(true) {
theGong.play();
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
} } }
}
참고
: Thread
모든 프로세스는 main 쓰레드
- main()이나 init() - 에서 시작
System.exit()를 호출하지 않는다면
main()이나 init()가 종료될 때
main 쓰레드 종료
스레드를 만든다면,
run()을 시작점과 종료점으로 가짐
Main 스레드를 비롯한 모든 스레드가 종료해야만
종료
프로세스 시작
프로세스 종료
참고
: Thread (Cont.)
Thread 만들기
(1)
class MyFirstThread extends Thread {
public void run() {
…
}
}
class ThreadTest {
ThreadTest() {
Thread thread = new MyFirstThread();
thread.start();
…
}
}
Thread 만들기
(2)
class ThreadTest implements Runnable{
ThreadTest() {
Thread thread = new Thread(this);
thread.start();
…
}
public void run() {
…
}
}
ImageObserver 인터페이스
public class ShowImage extends Applet {
Image thePicture;
public void init() {
thePicture = getImage(getCodeBase(), "titleimage.jpg");
}
public void paint(Graphics g) {
if (!g.drawImage(thePicture,0,0,this))
g.drawString("Loading Picture. Please hang on", 25,50);
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) {
repaint();
return false; // 이미지가 완성되었고, 더 이상의 수정이 필요없으므로 false 반환
}
else return
true; } } // 이미지가
아직도 수정되어야 한다면 true를 반환
public abstract boolean drawImage(Image img, int x,
int y, ImageObserver observer);
ImageObserver 인터페이스 (Cont.)
MediaTracker 클래스
public class trackImage extends Applet implements Runnable{
Thread thePlay;
Image thePicture;
MediaTracker theTracker;
public void init() {
// Image 객체 thePicture를 생성하요 이미지를 담을 준비를 한다
thePicture = getImage(getCodeBase(),
"titleimage.jpg");
// MediaTracker 생성자. 보통 인자는 this
theTracker = new MediaTracker(this);
// MediaTracker에 이미지 추가하기.
// 실제 적재는 checkID, checkALL, 또는 세개의 wait() 메쏘드들 중의 하나를 호출해야 한다.
theTracker.addImage(thePicture,1);
thePlay = new Thread(this);
thePlay.start();
}}
MediaTracker 클래스 (Cont.)
public void run() {
try {
// 1번 ID를 가진 모든 매체가 적재를 시작하도록 강제.
// 완료, 포기, 에러 발생 때까지 blocking
theTracker.waitForID(1);
repaint();
} catch (InterruptedException ie) {}
}
public void paint(Graphics g) {
// 1번 ID를 가진 모든 이미지가 모두 적재를 마쳤으면 true, 그렇지 않으면 false
if (theTracker.checkID(1,true)) {
g.drawImage(thePicture,0,0,this);
//thePlay.stop(); // deprecated
}
else g.drawString("Loading Picture. Please hand on", 25,50);
}
}
AppletContext 인터페이스의 네트워크 메쏘드들
public class Redirector extends Applet{
public void init() {
AppletContext ac = getAppletContext();
URL oldURL = getDocumentBase();
try {
URL newURL = new URL(getParameter("NEWHOST") + oldURL.getFile());
// 사용자를 새로운 웹 사이트로 보내주는 이미지 맵 애플릿에서 유용하게 사용
ac.showDocument(newURL);
} catch (MalformedURLException e) {}
}
}
AppletContext 인터페이스의 네트워크 메쏘드들
(Cont.)
<APPLET CODE=Redirector WIDTH=500 HEIGHT=500>
<PARAM NAME="NEWHOST" VALUE="http://ailab.sogang.ac.kr/">
</APPLET>
HTML 소스보기
URL oldURL = getDocumentBase();
//oldURL = “file:/D:/My Program
Sources/JAVA/index.html”
URL newURL = new URL(getParameter("NEWHOST") + oldURL.getFile());
//newURL= “http://ailab.sogang.ac.kr/index.html"
이 경우는 경로를 포함함으로
약간의 수정이 필요?
유용한 프로그램들
유용한 프로그램들 (Cont.)
public void init() {
...
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
/*
if (running) { play.suspend(); running = false; }
else { play.resume(); running = true; }
*/
synchronized (Animator.this) {
running = !running;
if (!running)
Animator.this.notify(); } }});
…}
public void run() {
while (true) {
for (thisCell = 0; thisCell < cells.size(); thisCell++){
try {
theTracker.waitForID(thisCell);
repaint();
play.sleep(90); //delay
synchronized(this) { while (!running) wait(); }
}
catch (InterruptedException ie) {}
}
thisCell = 0; }}
유용한 프로그램들 (Cont.)
유용한 프로그램들 (Cont.)
new URL(oldURL.getProtocol() + “://”
+ oldURL.getHost() + “/” + oldURL.getHost()