java.lang.Object | +----java.awt.Component | +----java.awt.Container | +----java.awt.Panel | +----java.applet.AppletA Simple Applet
import java.awt.Graphics; public class Simple extends java.applet.Applet { StringBuffer buffer = new StringBuffer(); public void init() { resize(500, 20); addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); } public void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); } public void paint(Graphics g) { g.drawRect(0, 0, size().width - 1, size().height - 1); g.drawString(buffer.toString(), 5, 15); } }
Loading the Applet
¾Æ¸¶ À§¿¡ "initializeing... starting..."À̶ó°í ³ª¿Ã°ÍÀÌ´Ù. AppletÀÌ
loadµÉ¶§ ´ÙÀ½°ú °°Àº ÀÏÀÌ ¹ú¾îÁø´Ù.
public class Simple extends java.applet.Applet { . . . public void init() { . . . } public void start() { . . . } public void stop() { . . . } public void destroy() { . . . } . . . }
init()
start()
stop()
destroy()
class Simple extends Applet { . . . public void paint(Graphics g) { . . . } . . . }
paint()
update()
paint()
to make improvements in drawing performance
keyDown()
mouseDown()
,
mouseUp()
mouseEnter()
,
mouseExit()
mouseDrag()
mouseMove()
public void mouseDown(int x, int y) { addItem("click!... "); }
3.1.4 Using UI Components
AppletÀº AWT Container class·Î ºÎÅÍ »ó¼Ó¹Þ¾Ò´Ù. ±×·¯¹Ç·Î, button, label,
pop-up list, scrollbar¿Í °°Àº user interface object¸¦ ¸ðµÎ »ç¿ë ÇÒ ¼ö ÀÖ´Ù.
´Ù¸¥ Container¿Í ¸¶Âù°¡Áö·Î, appletÀº ComponentÀÇ À§Ä¡¸¦ contorlÇÒ ¼ö ÀÖ´Â
layout manager¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
public class Simple extends java.applet.Applet {
. . .
public void paint(Graphics g) {
g.clearRect(0, 0, size().width - 1, size().height - 1);
g.drawRect(0, 0, size().width - 1, size().height - 1);
g.drawString(buffer.toString(), 5, 15);
}
}
À§ÀÇ Simplet AppletÀº display¿¡ ¹®Á¦°¡ ÀÖ´Ù.; scrollÀ» ÇÒ ¼ö ¾ø´Ù.
¸¸¾à text°¡ È¸é ³¡¿¡ µµ´ÞÇÏ°Ô µÇ¸é, ±×´ÙÀ½ÀÇ »õ·Î¿î text´Â º¼ ¼ö ¾ø°Ô µÈ´Ù.
ÀÌ ¹®Á¦¸¦ ÇØ°áÇϱâ À§Çؼ pre-made UI component¸¦ »ó¿äÇÏ¿© Á¤È®ÇÏ°Ô ÇØ°áÇÑ´Ù.
Pre-Made UI Componets
AWT´Â ´ÙÀ½ÀÇ UI componets¸¦ Á¦°øÇÑ´Ù.
Methods for Using UI Components in Applets
Applet class´Â AWT Container class¿¡¼ »ó¼Ó¹Þ¾ÒÀ¸¹Ç·Î, Applet¿¡ Components¸¦
÷°¡ÇÏ´Â °ÍÀº ½¬¿î ÀÏÀÌ´Ù. °¢°¢ÀÇ AppletÀº Componetns¸¦ ȸ鿡 ¾î¶»°Ô À§Ä¡ÇÒ
°ÍÀÎÁö °ü¸®ÇØÁÖ´Â layout manager°¡ ÀÖ´Ù. ´ÙÀ½Àº applet¿¡¼ ComponentÀÇ À§Ä¡¸¦
°áÁ¤Çϴµ¥ »ç¿ë ÇÒ ¼ö ÀÖ´Â Container class¿¡¼ Á¦°øÇÏ´Â methodµéÀÌ´Ù.
add()
remove()
getComponents()
locate()
setLayout()
preferredSize()
Adding a Non-Editable Text Field to the Simple Applet
Simple appletÀ» scrollÇϱâ À§Çؼ, TextField class¸¦ »ç¿ëÇÑ non-editable
text fieldÀÌ´Ù.
TextField field = new TextField(80);
//We used to use a StringBuffer.
public void init() {
//Add the TextField, and then display it.
field.setEditable(false);
add(field);
resize(field.preferredSize());
show();
addItem("initializing... ");
}
public void addItem(String newWord) {
//This used to append the string to the StringBuffer;
//now it appends it to the TextField.
String t = field.getText();
System.out.println(newWord);
field.setText(t + newWord);
repaint();
}
//The paint() method is no longer necessary,
//since the TextField repaints itself automatically.
´ÙÀ½Àº À§ÀÇ Simple appletÀ» ½ÇÇàÇÑ °á°úÀÌ´Ù.
3.1.5 Threads in Applets
°£´ÜÇÑ appletÀÏÁö¶óµµ Á÷Á¢ÀûÀ¸·Î º¸ÀÌÁö ¾ÊÁö¸¸ ¿©·¯°³ÀÇ thread°¡ ½ÇÇàµÉ ¼ö
ÀÖ´Ù. ¸¹Àº appletÀº ´Ù¸¥ applet¿¡ ¿µÇâÀ» ÁÖÁö ¾Ê°í ÀÚ½ÅÀÇ thread¸¦ »ý¼ºÇÏ¿©
»ç¿ëÇÑ´Ù.
¿¹¸¦ µé¾î¼ HotJava browser´Â Á¦ÀϸÕÀú applet¿¡ ÀÖ´Â document¸¦ º¸¿©ÁØ´Ù.
À̶§ browserÀÇ DocumentSwitcher thread´Â appletÀÇ init() method¸¦ ½ÇÇà
½ÃŲ´Ù. ±×¸®°í, À̶§ »ç¿ëÀÚ°¡ document¸¦ scroll½ÃÅ°¸é, AWT WServer thread´Â
appletÀÇ update() method¸¦ ½ÇÇà ½ÃŲ´Ù.
±×·¯¸é, ¾î¶»°Ô appletÀº ÇÊ¿äÇÑ ÀڽŸ¸ÀÇ thread¸¦ »ý¼ºÇÏ°í, »ç¿ëÇÒ °ÍÀΰ¡?
Thread°¡ init()À» È£ÃâÇÏ´Â µ¿¾ÈÀº ¾Æ¹«°Íµµ ÇÏÁö ¸øÇÑ´Ù. ¿¹¸¦ µé¾î¼
HotJava browserµµ ÀÚ½ÅÀÇ appletÀ» ÃʱâÈ Çϱâ ÀÌÀü¿¡´Â ¾Æ¹«°Íµµ ÇÒ ¼ö ¾ø´Ù.
±×·¯¸é, appletÀÌ ÃʱâÈ µÇ±â ÀÌÀü¿¡´Â ȸ鿡 ¾Æ¹«°Íµµ ³ªÅ¸³ªÁö ¾ÊÀ» °ÍÀÌ´Ù.
ÀÌ·¯ÇÑ ¹®Á¦¸¦ ÇØ°áÇϱâ À§Çؼ appletÀº thread¸¦ »ý¼ºÇÏ°í init() methodÀÇ
ÃʱâÈ¿¡ µû¸¥ code¸¦ thread body¿¡¼ ½ÇÇà ½ÃŲ´Ù. ´ÙÀ½Àº ±× ¿¹ÀÌ´Ù.
3.1.5.1 Example
´ÙÀ½ÀÇ Good, Bad appletÀº, loop¸¦ µ¹¸é¼ °è»êÀ» ÇÏ°í °¡²û¾¿ »óŸ¦ Ãâ·ÂÇØÁØ´Ù.
µÑ´Ù °°Àº ÀÏÀ» ¼öÇàÇÏÁö¸¸, ¼·Î Bad´Â init()¿¡¼ ¸ðµçÀÏÀ» ó¸®Çϱ⠶§¹®¿¡
´À¸®´Ù.
´ÙÀ½Àº Good appletÀÇ source codeÀÌ´Ù.
import java.awt.Graphics;
public class Good extends java.applet.Applet implements Runnable {
static final int NUMLOOPS = 2000000;
int loop = 0;
boolean doneInitializing = false;
String message = null;
Thread loopThread = null;
public void init() {
resize(500, 20);
}
public void start() {
if (loopThread == null) {
loopThread = new Thread(this, "Good thread");
loopThread.start();
}
}
public void stop() {
loopThread.stop();
loopThread = null;
}
public void run() {
while (loop < NUMLOOPS) {
if ((++loop%50000)==0) {
message = "Good: Initialization loop #"
+ loop + " of " + NUMLOOPS;
getAppletContext().showStatus(message);
repaint();
}
}
doneInitializing = true;
repaint();
}
/* The paint() method can't be called until init() has exited. */
public void paint(Graphics g) {
g.drawRect(0, 0, size().width - 1, size().height - 1);
if (message == null)
g.drawString("Good: ", 5, 15);
else if (!doneInitializing)
g.drawString(message, 5, 15);
else
g.drawString("Good: Done initializing.", 5, 15);
}
}
´ÙÀ½Àº Bad appletÀÇ source codeÀÌ´Ù.
import java.awt.Graphics;
public class Bad extends java.applet.Applet {
static final int NUMLOOPS = 2000000;
int loop = 0;
boolean doneInitializing = false;
String message = null;
Thread loopThread = null;
public void init() {
resize(500, 20);
while (loop < NUMLOOPS) {
if ((++loop%50000)==0) {
message = "Bad: Initialization loop #"
+ loop + " of " + NUMLOOPS;
getAppletContext().showStatus(message);
repaint();
}
}
doneInitializing = true;
repaint();
}
/* The paint() method can't be called until init() has exited. */
public void paint(Graphics g) {
g.drawRect(0, 0, size().width - 1, size().height - 1);
if (message == null)
g.drawString("Bad: ", 5, 15);
else if (!doneInitializing)
g.drawString(message, 5, 15);
else
g.drawString("Bad: Done initializing.", 5, 15);
}
}
´ÙÀ½Àº Good applet°ú Bad appletÀÇ ½ÇÇà ȸéÀÌ´Ù.
GoodÀº »¡¸® ³ªÅ¸³ªÁö¸¸, Bad´Â ¾ÆÁÖ ´Ê°Ô ÃʱâÈµÇ¾î ³ªÅ¸³´Ù.
Good Applet
Bad Applet
3.1.6 Saving, Compiling, and Running an Application