Applet Demonstration Program

 

1 SOURCE LISTING

1.1 Demo.java - A Simple Interactive Graphic Applet

1 import java.awt.*; //import Abstract Windowing Toolkit
2 import java.applet.*; //import applet class
3 import java.math.*; //import math class

4 public class Demo extends Applet { //define Demo class extends Applet class
5 Panel title,rhPanel; //declare panel variable
6 Scrollbar AmpScr; /declare scrollbar variable
7 Graphics g; //declare graphics variable
8 Label amplabel; //declare label variable
9 float amp=10; //declare floating point variable

10 public void init() { //initialize applet presentation
11 setLayout(new BorderLayout(5,5)); //window panel presentation
12 setBackground(Color.lightGray); //applet background color
13 title = new Panel(); //initiate new layout panel
14 title.add(new Label("SIMPLE DEMO APPLET")); //top panel title label
15 add("North", title); //confirm top panel
16 rhPanel=new Panel(); //start right-hand panel
17 rhPanel.setLayout(new GridLayout(16,1,1,1)); //panel has 1 column with 16 rows
18 rhPanel.add(amplabel = new Label("Amplitude = "+ (int)10)); //add scrollbar with labels
19 AmpScr = new Scrollbar(Scrollbar.HORIZONTAL,10,1,0,101); //set up scrollbar limit values
20 rhPanel.add(AmpScr); //add scrollbar to right-hand panel
21 add("East",rhPanel); //add right-hand panel
22 } //end init()

23 public Insets insets() { //inset border to applet
24 return new Insets(5,5,5,5); //set border 5 pixels in from 4 edges
25 } //end insets

26 public boolean handleEvent(Event e) { // check for user changes
27 if (e.target instanceof Scrollbar){ //test for scrollbar movement
if (e.target == AmpScr){ //check scrollbar moved
28 amplabel.setText("Amplitude = " + (int)RA.getValue()); //write new scrollbar value
29 amp =(float) AmpScr.getValue(); //pass value to amplitude variable
30 repaint(); //rewrite applet with new values
31 } //end AmpScr if
32 } // end instanceof if
33 return false; //return status
34 } // end handleEvent

35 public void paint(Graphics g) { //draw/update graphics
36 Dimension d = size(); //applet size function (taken from .htm file- 500x300 pixels)
37 g.drawRect(0,0, d.width – 1, d.height – 1); //draw outside box
38 g.setColor(Color.blue); //set paint color blue
40 g.drawLine(50,150,350,150); // draw x-axis
41 g.drawRect(50,50, 300, 200); //draw plot area rectangle
42 int xcur, ycur, lastx=50, lasty=50; //initialize axis variables
43 for(xcur=50;xcur<350;xcur++){ // x-axis pixel range
44 ycur=(int)(amp*(–Math.sin((float)(xcur–50)/10))+150); //y-axis plot - sine function
45 if (xcur != 50) { //don’t paint the first point
46 g.drawLine(lastx, lasty, xcur, ycur); //draw line between points
47 } //end if
48 lastx = xcur; //store previous x point
49 lasty = ycur; //store previous y value
50 } //end for
51 } //end paint

52 } //end Demo

 

1.2 Source Listing Description

Lines 1 to 3 Program Java library imports.
Line 4 starts the program as an applet.
Lines 5 to 9 declare attributes and assign variable names.
Line 10 applet layout initializing method: init().
Line 11 defines the applet window BorderLayout. This lays out a window container, arranging and resizing its components to fit in five regions: North, South, East, West, and Center. The function parameters (5,5) define 5-pixel gaps between the regions.
Line 12 sets the applet background color.
Lines 13 to15 define the top panel, and insert the applet title.
Lines 16 to 22 describe the right-hand panel. Line 17 splits this panel using GridLayout into one column with 16 rows. Only the top row is filled with a sensibly sized scrollbar, the rest are empty to fill the panel space.
Lines 23 to 25 call the Insets class that sets a border 5 pixels in from each edge. Without this the applet enclosing box may be incomplete.
Lines 26 to 32 check the user interface for user interface changes and events and updates input parameters and readouts accordingly. In this case, only a scrollbar is tested. After user modifying input data and the program handling of in/out scrollbar data, the applet is repainted by calling the function repaint() in Line 30.
Lines 35 to 51 define the graphics plotting using the paint method.
Line 36 derives the applet size from the HTML calling program and assigns the pixel values to variables d.width and d.height, respectively.
Lines 37 to 41 use the graphics library definitions to draw the applet and plot outline rectangles, set drawing colors and draw the plot x-axis.
Lines 43 to 49 plots the sine math function using a for loop to increment pixel values along the x-axis and to draw a line between the function values until the end of the plot window.
Line 44 defines the math sine function with amplitude amp obtained from the scrollbar setting. The offset is 150 pixels from the applet edge.
Lines 50 to 52 terminate the program subfunctions.

 

2 APPLET LAYOUT

The BorderLayout class developed for Java 1 is structured as a central plotting area surrounded by four panels, North, South, East and West.
Figure 1 shows the window plan for the demonstrator applet using the central display area, and just two panels, North and East.
The panels can be subdivided by the GridLayout class into equal areas in column/row format. Within these areas, Buttons, Scrollbars,
Text areas, Drop-down menus, and Radio selection buttons can be placed for user interaction.

The central panel is usually reserved for plotting or drawing graphical data.
The size of the applet, X,Y in pixels, is determined by the settings in the .htm calling program.
The plotted data needs to be constrained within the program set plot window by suitably addressing its pixel coordinates.
Pixel address coordinates are referenced to the applet top left-hand corner as indicated in Figure 1.

 

Figure 1 Applet window plan.

3 HTML APPLET CALLING PROGRAM

Applets are invoked in browsers by the code within the <APPLET…….> </APPLET> instructions of an HTML browser calling program file.
An example program is listed in Section 3.1.
The Demo.class file is the compiled executable code derived from the Demo.java source file.
The applet class file is called up from the first code = line. The applet size in displayed pixels is constrained within the width = and height = settings.
Various other parametric settings/initializing data to be passed to the applet on start-up can also be specified in this region.

3.1 HTML Source Listing

<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER>
<APPLET
code = "Demo.class"
width = "500"
height = "300"
>
</APPLET>
</CENTER>
</BODY>
</HTML>