Ms Gold
2/13
Introduction to Object Oriented Programming
Lesson Plan 18 - Java Application vs Java
Applet
Mass Framework:
Standard 1:
Standard 2:
Standard from Technology/Engineering Standards for a Full First-Year Course in Grades 9 or 10
Learning Objective: To try to grapple with the issues surrounding the future of digital media brought to light by the recent storm of WikiLeaks activities.
Learning Experience:
Swing provides a special subclass of the Applet class called javax.swing.JApplet. The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs).
The browser's Java Plug-in software manages the lifecycle of an applet.
* It can initialize itself. must have an init() method, a start() method,
a stop() method and destroy() method
* It can start running.
* It can stop running.
* It can perform a final cleanup, in preparation for being unloaded.
The Java API Applet class provides what you need to design the appearance and manage the behavior of an applet. This class provides a graphical user interface (GUI) component called a Panel and a number of methods. To create an applet, you extend (or subclass) the Applet class and implement the appearance and behavior you want.
The applet's appearance is created by drawing onto the Panel or by attaching other GUI components such as push buttons, scrollbars, or text areas to the Panel. The applet's behavior is defined by implementing the methods.
Extending a Class
Most classes of any complexity extend other classes. To extend another class means to write a new class that can use the fields and methods defined in the class being extended. The class being extended is the parent class, and the class doing the extending is the child class. Another way to say this is the child class inherits the fields and methods of its parent or chain of parents. Child classes either call or override inherited methods. This is called single inheritance.
The SimpleApplet class extends Applet class, which extends the Panel class, which extends the Container class. The Container class extends Object, which is the parent of all Java API classes.
The Applet class provides the init, start, stop, destroy, and paint methods you saw in the example applet. The SimpleApplet class overrides these methods to do what the SimpleApplet class needs them to do. The Applet class provides no functionality for these methods.
However, the Applet class does provide functionality for the setBackground method,which is called in the init method. The call to setBackground is an example of calling a method inherited from a parent class in contrast to overriding a method inherited from a parent class.

The start Method: The start method is called when the applet is visited such as when the end user goes to a web page with an applet on it. The example prints a string to the console to tell you the applet is starting. In a more complex applet, the start method would do things required at the start of the applet such as begin animation or play sounds.
After the start method executes, the event thread calls the paint method to draw to the applet's Panel. A thread is a single sequential flow of control within the applet, and every applet can run in multiple threads. Applet drawing methods are always called from a dedicated drawing and event-handling thread.
The stop and destroy Methods: The stop method stops the applet when the applet is no longer on the screen such as when the end user goes to another web page. The example prints a string to the console to tell you the applet is stopping. In a more complex applet, this method should do things like stop animation or sounds.
The destroy method is called when the browser exits. Your applet should implement this method to do final cleanup such as stop live threads.
Appearance
The Panel provided in the Applet class inherits a paint method from its parent Container class. To draw something onto the Applet's Panel, you implement the paint method to do the drawing.The Graphics object passed to the paint method defines a graphics context for drawing on the Panel. The Graphics object has methods for graphical operations such as setting drawing colors, and drawing graphics, images, and text.The paint method for the SimpleApplet draws the I'm a simple applet string in red inside a blue rectangle.
public void paint(Graphics g){
System.out.println("Paint");
//Set drawing color to blue
g.setColor(Color.blue);
//Specify the x, y, width and height for a rectangle
g.drawRect(0, 0,
getSize().width -1,
getSize().height -1);
//Set drawing color to red
g.setColor(Color.red);
//Draw the text string at the (15, 25) x-y location
g.drawString(text, 15, 25);
}