Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto,
2002.
Lab 3: Building Applets - Graphical representation
An applet is a small program that is intended not to be run on its own, but
rather to be embedded inside another application. The Applet class must be the
superclass of any applet that is to be embedded in a Web page or viewed by the
Java Applet Viewer. The Applet class provides a standard interface between applets
and their environment. Since an Applet is a subClass of java.awt.Component,
by default it operates on a graphic object.
Important methods in Applet Class:
For more information about Applet Class, please consult the
Java API Documentation.
An Applet is different from an application. An application is a static method within a class definition, thus may not instantiate the class itself, but an Applet is an object and will be treated as an object when it is loaded into an application.
Applet Class inherits Panel Class. A Panel Class is a Container Component that can contain other components (such as Button, TextArea, etc.). By default, a Panel Class has a Layout Manager called Flow Layout. The function of this Layout Manager is to manage how the components in the Container is displayed in the corresponding Graphics context. A Flow Layout Manager will arrange components in a left-to-right flow, much like lines of text in a paragraph. Flow layouts are typically used to arrange buttons in a panel. It will arrange buttons left to right until no more buttons fit on the same line. Each line is centered.
In this Lab, we will add some components to the Applet Panel as well as customizing the paint(Graphics) method to draw our MobileNetwork topology and the MobileStation too.
First of all we should define a Class that inherits Applet Class.
import java.applet.Applet; import java.awt.*; public class VisualMobileSystem extends Applet
Then, we define all the Class fields and the init() method to initialize all the fields and add some components to the Applet Panel.
private MobileSystem vodaPhone; private MobileStation ms; private TextArea textarea;public void init() { vodaPhone = new MobileSystem("VodaPhone Systems"); ms = new MobileStation("Siemes 45i",100,100); Label title = new Label(vodaPhone.getName()); textarea=new TextArea(4,80); add(title); add(textarea); }
Finally, we should customize the paint(Graphics) method to draw the MobileNetwork
topology and the MobileStation into the Applet Panel.
Make sure that the code executes the super.paint(Graphics) method before any
others.
public void paint(Graphics g) { super.paint(g); vodaPhone.draw(g); ms.draw(g); }
The complete code in Java is shown in the followings.
import java.applet.Applet; import java.awt.*; public class VisualMobileSystem extends Applet { // instance variables private MobileSystem vodaPhone; private MobileStation ms; private TextArea textarea; public void init() { // provide any initialisation necessary for your Applet vodaPhone = new MobileSystem("VodaPhone Systems"); ms = new MobileStation("Siemes 45i",100,100); Label title = new Label(vodaPhone.getName()); textarea=new TextArea(4,80); add(title); add(textarea); } public void paint(Graphics g) { super.paint(g); // to allow painting any contained components vodaPhone.draw(g); ms.draw(g); } public String getAppletInfo() { // replace this with your own info return "Title: Mobile System Applet \n" + "Author: CPwJ\n" + "A simple applet that draws the mobile system topology."; } } |
By now, your BlueJ Project has grown into a little jungle of Classes. Check whether you have all of them.
When your applet compilation is successful, try to run the applet by right clicking the VisualMobileSystem Applet and select Run Applet.
You should then asked to fill in a form that will define how the Applet is executed. Set the Height and Width of the Applet as 300 and 600 consecutively.
What BlueJ actually did with this process is to define an HTML file called VisualMobileSystem.html that will be refered by either appletviewer application or web browser application to load the VisualMobileSystem.class and run the Applet. Here is the contents of the html file.
<HTML>
<!-- This file automatically generated by BlueJ Java Development -->
<!-- Environment. It is regenerated automatically each time the -->
<!-- applet is run. Any manual changes made to file will be lost -->
<!-- when the applet is next run inside BlueJ. Save into a -->
<!-- directory outside of the package directory if you want to -->
<!-- preserve this file. -->
<HEAD>
<TITLE>VisualMobileSystem Applet</TITLE>
</HEAD>
<BODY>
<H1>VisualMobileSystem Applet</H1>
<HR>
<APPLET CODE="VisualMobileSystem.class"
WIDTH=600
HEIGHT=300
CODEBASE=".">
ALT="Your browser understands the <APPLET> tag but isn't running the applet, for some reason."
Your browser is ignoring the <APPLET> tag!
</APPLET>
<HR>
</BODY>
</HTML>
The result of the applet execution is the display similar to what is shown below.