3 Applet ¸¸µé±â

Java AppletÀÇ °³³ä°ú ¿©·¯°¡Áö ±â¼ú¿¡ ´ëÇؼ­ ¾Ë¾Æº»´Ù.

3.1 The Anatomy of an Applet

¸ðµç appletÀº Applet classÀÇ subclass¸¦ »ý¼ºÇÔÀ¸·Î½á ¼öÇàµÈ´Ù. ´ÙÀ½Àº Applet classÀÇ »ó¼Ó ±¸Á¶¸¦ º¸¿©ÁÖ°í ÀÖ´Ù. ÀÌ°ÍÀº appletÀÌ ¹«¾ùÀ» ¾î¶»°Ô µ¹¾Æ°¡´Â±â ¾Ë¾Æº¸´Âµ¥ µµ¿òÀÌ µÈ´Ù.

java.lang.Object

   |

   +----java.awt.Component

           |

           +----java.awt.Container

                   |

                   +----java.awt.Panel

                           |

                           +----java.applet.Applet

A Simple Applet
´ÙÀ½Àº SimpleÀ̶ó´Â applet source codeÀÌ´Ù. Simple appletÀº appletÀÌ ÀÖ´Â page°¡ ¹æ¹®µÇ¸é, ÀÚ½ÅÀÇ ½ÇÇà°úÁ¤À» È­¸é¿¡ º¸¿©ÁÖ°Ô µÈ´Ù.


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);

    }

}




3.1.1 The Life Cycle of an Applet

´ÙÀ½Àº Simple appletÀÌ´Ù.


Loading the Applet
¾Æ¸¶ À§¿¡ "initializeing... starting..."À̶ó°í ³ª¿Ã°ÍÀÌ´Ù. AppletÀÌ loadµÉ¶§ ´ÙÀ½°ú °°Àº ÀÏÀÌ ¹ú¾îÁø´Ù.



Leaving and Returning to the Applets's Page
»ç¿ëÀÚ°¡ appletÀÌ ÀÖ´Â page¸¦ ¶°³ª¸é, ¿¹¸¦ µé¾î¼­ ´Ù¸¥ page·Î ³Ñ¾î°¡¸é, appletÀº stopÇÑ´Ù. ´Ù½Ã »ç¿ëÀÚ°¡ appletÀÌ ÀÖ´Â ´Ù½Ã startÇÑ´Ù. ¸¸¾à, ÇöÀç page¸¦ ¶°³ª, ´Ù¸¥ °÷¿¡ °¬´Ù ¿À¸é, "stopping..."À̶ó´Â ¹®ÀÚ¿­°ú "starting..."À̶õ ¹®ÀÚ¿­ÀÌ applet¿¡ ÷°¡ µÇ¾î ÀÖÀ» °ÍÀÌ´Ù.

Reloading the Applet's Page
»ç¿ëÀÚ°¡ appletÀÌ ÀÖ´Â page¸¦ reloadÇϸé, appletÀº Áö¿öÁ³´Ù°¡ ´Ù½Ã loadµÈ´Ù. µû¶ó¼­, appletÀÌ °¡Áö°í ÀÖ´ø resource¸¦ ¹Ý³³ÇÏ°í stop, final cleanup »óÅ°¡ µÈ´Ù. ±×·±ÈÄ ´Ù½Ã loadµÇ¾î¼­ initialize, startÇÏ°Ô µÈ´Ù. ¸¸¾à, ÇöÀç page¸¦ reloadÇÏ°Ô µÇ¸é À§ÀÇ Simple appletÀº ¸ðµç°Ô Áö¿öÁö°í ´Ù½Ã "initializeing... starting..."À̶ó°í ³ªÅ¸³¯ °ÍÀÌ´Ù.

Quitting the Browser
»ç¿ëÀÚ°¡ browser¸¦ Á¾·áÇÏ·Á°í Çϸé, appletÀº stopÇÏ°Ô µÇ°í, final cleanup ÇÏ°í browser´Â Á¾·áÇÏ°Ô µÈ´Ù.


3.1.2 Methods for Milestones

Applet class´Â ±âº»ÀûÀÎ applet½ÇÇà¿¡ µû¸¥ method Çü½ÄÀ» Á¤ÀÇÇÏ°í Àֱ⠶§¹®¿¡ Á¦°øÇÏ´Â ¿©·¯°¡Áö ´Ù¾çÇÑ method¸¦ overrideÇÏ¿© applet subclass¸¦ ÀÛ¼ºÇÑ´Ù.


public class Simple extends java.applet.Applet {

    . . .

    public void init() { . . . }

    public void start() { . . . }

    public void stop() { . . . }

    public void destroy() { . . . }

    . . .

}


Simple appletÀº Applet classÀÇ subclass·Î½á À§¿Í °°Àº ±¸Á¶·Î µÇ¾îÀÖ´Ù. À§ÀÇ Simple appletÀº ´ëÇ¥ÀûÀÎ event°¡ ¹ß»ýÇÒ ¶§¸¶´Ù ¼öÇàµÇ´Â Applet method¸¦ overrideÇÑ´Ù.

init()
to initialize the applet each time it's loaded (or reloaded)
start()
to start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet
stop()
to stop the applet's execution, such as when the user leaves the applet's page or quits the browser
destroy()
to perform a final cleanup in preparation for unloading

¸ðµç appletÀÌ ÀÌ ¸ðµç method¸¦ overrideÇÒ ÇÊ¿ä´Â ¾ø´Ù. ¿¹¸¦ µé¾î¼­, óÀ½¿¡ ¼³¸íÇÑ "Hello World" appletÀº ÃʱâÈ­°¡ µÈÈÄ¿¡´Â ¾Æ¹«Àϵµ ÇÏÁö ¾Ê±â ¶§¹®¿¡ init() method¸¸À» °¡Áö°í ÀÖ°í start(), stop(), destroy() method´Â °¡Áö°í ÀÖÁö ¾Ê´Ù.

ÃʱâÈ­°¡ ³¡³ª°í¾î¶²ÀÏÀ» ¼öÇà ÇÏ°íÀÚ ÇÏ´Â ¸ðµç appletÀº, start() method¸¦ overrideÇÏ¿© È­¸éÀÇ ³»¿ëÀ» ¹Ù²Û´ÙµçÁö, »ç¿ëÀÚÀÇ ÀÔ·ÂÀ» ±â´Ù¸°´ÙµçÁö ÇÏ´Â ÀÏÀ» ¼öÇàÇÑ´Ù.

start()¸¦ overrideÇÏ´Â ´ëºÎºÐÀÇ appletÀº stop() methodµµ overrideÇÑ´Ù. stop() method´Â appletÀÌ ÁߴܵǾúÀ»¶§ ½ÇÇà µÈ´Ù. µû¶ó¼­, »ç¿ëÀÚ°¡ appletÀÌ ÀÖ´Â page¸¦ º¸°í ÀÖÁö ¾ÊÀ¸¸é system resource¸¦ °¡Áö°í ÀÖÁö ¾Êµµ·Ï ÇؾßÇÑ´Ù. ¿¹¸¦ µé¸é, animationÀ» ¼öÇàÇÏ´Â appletÀº »ç¿ëÀÚ°¡ º¸°í ÀÖÁö ¾ÊÀ¸¸é, stopµÇ¾î¼­ È­¸é°»½ÅÀ» ±×¸¸ µÎ¾î¾ßÇÑ´Ù.

¸¹Àº appletÀº destory() method¸¦ overrideÇÒ ÇÊ¿ä°¡ ¾ø´Âµ¥, ±× ÀÌÀ¯´Â stop() method(°¡²ûÀº destory()¶ó°í ºÒ·ÁÁö±âµµ ÇÑ´Ù.)ÀÌ applet ½ÇÇàÀ» ±×¸¸µÎ´Âµ¥ ÇÊ¿äÇÑ ¸ðµç ÀÛ¾÷À» ó¸®Çϱ⠶§¹®ÀÌ´Ù. ±×·¯³ª, destory()´Â ±×¹ÛÀÇ appletÀÇ resource¸¦ ¹Ý³³Çϴµ¥ ÇÊ¿äÇÒ¶§ ¾²ÀδÙ.


3.1.3 Methods for Drawing and Event Handling

AppletÀº AWT(Advanced Windowing Toolkit; appletÀ̳ª application¿¡¼­ user interface¸¦ À§ÇÑ packageÀÌ´Ù.) Component class¿¡¼­ drawing°ú event handling method¸¦ »ó¼Ó ¹Þ´Â´Ù. DrawingÀº appletÀÇ È­¸é¿¡ image¿Í button°°Àº user interface¸¦ À§ÇÑ °ÍÀ» ¸»Çϴ°ÍÀÌ°í, Event handlingÀº mouse³ª keyÀÔ·Â °°Àº »ç¿ëÀÚ ÀÔ·ÂÀ̳ª, windowÀÇ iconÈ­, file ÀúÀå°ú °°Àº abstract event¸¦ ¸»ÇÏ´Â °ÍÀÌ´Ù.


class Simple extends Applet {

    . . .

    public void paint(Graphics g) { . . . }

    . . .

}


À§ÀÇ Simple appletÀº, paint() method¸¦ »ó¿ëÇÏ°í ÀÖ´Ù. paint() method´Â Applet classÀÇ È­¸éÃâ·ÂÀ» À§ÇÑ 2°³ÀÇ methodÁßÀÇ ÇϳªÀÌ´Ù.

paint()
the basic display method; most applets implement this method to draw the applet's representation within a browser page
update()
a method you can use along with paint() to make improvements in drawing performance

¶ÇÇÑ, Applet class´Â event handlingÀ» À§ÇÑ method¸¦ Á¦°øÇÑ´Ù.
keyDown()
notifies the applet that the user pressed a key on the keyboard
mouseDown(), mouseUp()
notify the applet that the user pressed/released a mouse button
mouseEnter(), mouseExit()
notify the applet that the cursor entered/exited the applet's display area
mouseDrag()
notifies the applet that the user moved the mouse while pressing a mouse button
mouseMove()
notifies the applet that the mouse moved but no mouse button was pressed

À§ÀÇ method¸¦ overrideÇÔÀ¸·Î½á appletÀº event¿¡ ´ëÀÀÇÒ ¼ö ÀÖ´Ù. ¿¹¸¦ µé¾î¼­ Simple applet¿¡ ´ÙÀ½°ú °°Àº mouDown() method¸¦ ÷°¡Çϸé, mouse°¡ clickµÇ¸é ÀÔ·ÂµÈ À§Ä¡¿¡ "click!..."À̶ó°í Ãâ·ÂÀÌ µÈ´Ù.


public void mouseDown(int x, int y) {

    addItem("click!... ");

}


´ÙÀ½¾Æ·§ ºÎºÐÀº mouseÀÔ·ÂÀ» ¹Þ´Â Simple appletÀÌ´Ù. (alpha - HotJava¿ë)




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()
Adds the specified Component to this Container.
remove()
Removes the specified Component from this Container.
getComponents()
Gets all the Components in this Container.
locate()
Locates the Component at the specified x,y position.
setLayout()
Sets the layout manager for this Container.
preferredSize()
Returns the preferred size of this Container.


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À» ½ÇÇàÇÑ °á°úÀÌ´Ù.


Your browser doesn't understand the APPLET tag, and there's no Alpha version of this applet. The reason: With the Alpha 3 and earlier APIs, it's difficult to write an applet that uses the AWT.



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