Applet Class

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

Topic Notes
The browser's part in a Java applet
  • The JVM (Java Virtual Machine) is built into the browser.  Therefore the browser has complete control of the running applet.
  • The JVM reads the byte code found in the Java .class files and executes the instructions to make the applet run.
  • The web browser controls the applet security regarding reading & writing of files, accessing system configuration parameters, etc...
  • The web browser reads the HTML command <applet> and its parameters, then starts running the Java class file.
  • The Browser via the JVM creates a Thread to run the applet in.  Then the JVM creates a Frame, sets the Size of the Frame via the Width & Height HTML values, sets the Layout Manager to FlowLayout, sets the frame to visible,  creates an instance of the Applet, calls init(), then starts the Thread by start(). 
  • Note:  main() in your applet is not run, unless you run the applet from the command line as you would an application.  See my example Hello_App2.html
Applet Methods - Start up sequence
  • init() - called once!
  • Then start()  is called.
  • Then paint() is called.
init()
  • Called once when the applet is load or re-loaded.
  • Create the user interface.  Use this method to parse parameters, load images & fonts, and create components to show to the screen like Button, Label, TextField etc...
  • Create new Threads here.
paint()
  • Takes an instance of Graphics class.
  • Called whenever you resize or minimize then maximize a component.  (note: update() is not called)
  • Called when you call repaint() in your code.
repaint()
  • Call this method in your applet whenever you make a change to an object and want to display the change to the screen.
  • calls update()  in Component class, which clears the screen with the background color of the applet, then calls paint(). 
  • The clearing of the screen will cause flickering.  Override the update() method to solve this problem.
  • repaint() schedules a call to update().  Therefore, update() may not be called immediately and there maybe one call to update() for several repaint() calls.
update()
  • Clears the screen with the background color of the applet, then calls paint().
  • To solve screen flickering problems due to the clearing of the screen, override the update() method. The code you write should only clear portions of the screen that requires clearing.  Use the Graphics methods clearRect() to clear a certain rectangular area.   (See the example below.  No clearing is used.)
    ex:  public void update( Graphics g)
           { paint( g ); }
start() & stop()
  • start():  Called after init() when the web page with the applet is about to appear.
  • start() & stop(): May be called as the web browsers display is scrolled in and out of focus.
  • stop(): Called when the web page is about to be replaced.
  • Pause and resume Threads, animations, etc...
destroy()
  • stop() will always be called before destroy()
  • Called when the web page is removed from the browser's memory.  Refresh/Reload does this.
  • Use this method to remove any Threads that your applet has created.
  • If you override destroy() make sure you make a call to super.destroy() as the last call in the method.
main()
  • If you put a main() method in the applet the browser will not call it!
From the Browser:
Refresh/Reload or Back then Forward
  • stop() is called.
  • Then destroy() is called.
  • Next your applet is instantiated again and init() is called.
  • Next paint() is called.
From the Browser:
Scrolling or switch Windows
  • Applet screen is cleared.
  • Then paint() is called.
  • Noticed that the update() method is not called.
HTML Codes
  • Mandatory codes:  code, height, width (see example below)
  • <applet code="Hello_Applet.class" width="275" height="55"> </applet>
Gettting the Size of the Applet
  • this.getSize().width - width of the applet.
  • this.getSize().height - height of the applet.
  • getSize() is located in the Component class and returns a Dimension object.  The variables "width" or "height" come from the Dimension class.
  • setSize() may not work because the applet may not allow sizing.
Parameter
  • getParameter("[param name]")
  • If a parameter is not passed then the value returned by getParameter() is null.