7월20일 JAVA(Thread),ARM(
Applet의 주기
애플릿의 주기 --> 웹브라우저에 탑재된 자바가상머신이 각상황에 따라 자동 호출한다.
까먹고 있었던 applet의 주기의 당연히 entry point인 init()부분과 paint()부분 destory() 파괴1. public void init()
애플릿이 처음 로딩될 때 호출되는 메소드이다.
이 메서드는 애플릿이 웹브라우저에 로딩될때 한번만 호출되므로 애플릿을 초기화 시킬때 사용된다. 초기화 작업으로는 필요한 객체의 생성, 초기 상태 설정, 이미지 또는 폰트 등의 로딩 등이다.
2. public void start()
init() 메서드가 호출된 다음이나, 다른 HTML 페이지로 갔다가 현재 애플릿이 포함된 HTML 페이지로 다시 되돌아 왔을 때 호출되는 메서드이다.
이 메소드는 일반적으로 stop 메소드와 함께 사용될 수 있으며, 애플릿에 선언되어 있는 스레드의 시작 또는 재시작을 주로 담당한다.
3. public void stop()
애플릿이 정지될때 호출되는 메소드이다.
사용자가 애플릿이 포함된 HTML 페이지를 보다가 다른 페이지로 넘어갔을때 호출된다. 또한 애플릿이 포함된 HTML 페이지의 웹 브라우저를 닫을 때 호출되는 dsestroy 메소드가 호출되기 전에 먼저 호출된다.
4. public void destroy()
애플릿이 종료될때 호출되는 메서드이다.
현재 애플릿이 포함되어 있는 HTML 페이지를 보고 있는 웹 브라우저를 닫을 때 호출된다.
5. public void paint(Graphics g)
현재 애플릿의 영역에 그림을 그리거나 글자를 출력하는 작업시 호출되는 메서드이다.
이 메서드는 화면이 사라졌다가 다시 화면에 나타났을때 자동 호출되며 개발자가 임의로 호출할 수도 있다. paint() 메서드는 Graphics 클래스가 객체가 인자로 넘어오기 때문, java.awt.Graphics 클래스를 반드시 import 해야한다.
그리고 start() stop()부분으로 기억해둬야한다.
=============================
import java.awt.*;
import java.awt.event.*;
public class DowningLine extends Frame implements Runnable,ActionListener
{
Button startButton = new Button("START");
Button stopButton = new Button("STOP");
Button quitButton = new Button("QUIT");
Panel drawingPanel = new Panel();
Thread lineThread = null;
int xStart, xEnd, yStart, yEnd,y;
boolean isStart;
public static void main(String[] args)
{
DowningLine theApplication = new DowningLine();
}
public DowningLine()
{
Panel buttonPanel = new Panel();
startButton.addActionListener(this);
stopButton.addActionListener(this);
quitButton.addActionListener(this);
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
buttonPanel.add(quitButton);
add(buttonPanel,"North");
add(drawingPanel,"Center");
setSize(200,200);
int width = getSize().width/2;
int height = getSize().height/2;
xStart = width/2;
xEnd = width+xStart;
yStart = height/2;
yEnd = height+yStart;
y = yStart;
isStart = false;
setVisible(true);
}
public void start()
{
if(null==lineThread )
{
if(isStart==false)
{
isStart=true;
}else if(isStart==true)
{
isStart=false;
}
lineThread = new Thread(this);
lineThread.start();
}
}
public void stop()
{
if(isStart==false)
{
isStart=true;
}else if(isStart==true)
{
isStart=false;
}
if(null!=lineThread && lineThread.isAlive())
{
lineThread.stop();
}
lineThread=null;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(100);
}catch(InterruptedException exp) {}
repaint();
}
}
public void paint(Graphics g)
{
g = drawingPanel.getGraphics();
g.setXORMode(Color.orange);
if(isStart==false)
{
g.drawLine(xStart,y,xEnd,y);
isStart=true;
}
g.drawLine(xStart,y,xEnd,y);
if(y>=yEnd)
{
y=yStart;
}
else
{
y++;
}
g.drawLine(xStart,y,xEnd,y);
}
public void actionPerformed(ActionEvent theEvent)
{
if(theEvent.getSource() == startButton)start();
else if(theEvent.getSource() ==stopButton)stop();
else if(theEvent.getSource() == quitButton)
{
setVisible(false);
dispose();
System.exit(0);
}
}
}