|
90-726
Java in Electronic Community Development
|
Assignment 1: Introduction to Applets
Summary
Create an applet that simplifies web page design by drawing bar graphs.
The web page author includes the parameters of the graph in the HTML code,
such as size, labels, and data values. The java applet is responsible for
displaying the data in an accurate and easy-to-understand manner.
Sample HTML excerpt
<applet
code=BarGraph.class
align=top
width=300
height=200 >
<param name=numItems value=3>
<param name=foreground value=000000>
<param name=background value=ffffff>
<param name=barWidth value=25>
<param name=gapWidth value=15>
<param name=dataMin value=1.25>
<param name=dataMax value=2.0>
<param name=numTicks value=6>
<param name=hpad value=40>
<param name=vpad value=20>
<param name=color1 value=ff0000>
<param name=height1 value=1.5>
<param name=label1 value="1993">
<param name=color2 value=808000>
<param name=height2 value=1.67>
<param name=label2 value="1994">
<param name=color3 value=00ff00>
<param name=height3 value=1.75>
<param name=label3 value="1995">
</applet>
Parameters
The parameters in the above HTML file affect the graph in the following
ways.
- numItems (int): the number of bars in the graph. No default,
this parameter is required.
- barWidth (int): the width of each bar, in pixels. Default=25.
- gapWidth (int): the width of the space between bars, in pixels.
Default=barWidth.
- background (RRGGBB hexadecimal int): the background color
of the applet. Default=ffffff (white).
- foreground (RRGGBB hexadecimal int): the color used for axes,
labels, and some bars. Default=000000 (black).
- dataMin, dataMax (float): minimum and maximum values on the
vertical. Defaults=0.0 and 1.0.
- numTicks (int): number of subdivisions on the vertical axis.
Default=4.
- hpad, vpad (ints): horizontal and vertical padding (in pixels),
to provide space for labels. Defaults=40 and 20
- label1, label2, etc (Strings): Short text labels for each
bar. Default="".
- color1, color2, etc (RRGGBB hexadecimal int): the color to
draw the bars in. Default=foreground.
- height1, height2, etc (floats): data values of each bar, in
the same units as dataMin and dataMax. No default, these
parameters are required.
Notes
Normally, an applet should perform some basic error handling. For instance,
the Integer.parseInt method (see below) throws a NumberFormatException
which should be caught. However, as this is the first assignment, and since
this applet is not interactive, you do not need to use any try,
catch, finally syntax in your code. See Nutshell pp.
40-45 if you are interested. (You will need to know this later.)
Tips
- To get the width and height of the applet, use
Rectangle rect = this.bounds();
applet_height = rect.height;
applet_width = rect.width;
- To get a parameter, use s = getParameter("parametername");
where s is a string.
- To translate a string to an int, use i = Integer.parseInt(s);
- To translate a string to a float, use f = Float.valueOf(s).floatValue();
- To handle the color parameters, see Nutshell pp. 92-93.
- To write an int onto the screen, use g.drawString(String.valueOf(i),
x, y);
- Java has a very convenient string syntax. To create the string "colorn",
simply use "color" + n, where n is an int.
- Try to implement one thing at a time.
Extras
The above assignment represents the minimal work necessary to pass.
For full credit, implement one or two of the following enhancements, or
invent your own. (The following are only suggestions.) Make whatever design
decisions necessary, including new parameter names and reasonable defaults.
Be sure that the enhanced applet still supports the basic parameters. Decide
whether to extend the original applet, or create a new one or ones. Describe
the enhancements, and create HTML files that show them off.
- Add axis labels on the vertical and horizontal axes. Try to print the
vertical label vertically.
- Add support for different fonts. Try to make the font support be platform
independent.
- Improve the axis labelling, so that it does center justification on
the horizontal and right justification on the vertical.
- When dataMin is negative and dataMax is positive, allow the bars to
be drawn relative to 0.
- Allow the data to be shown as a pie chart instead of a bar graph.
- For large datasets, include an alternate parameter interface that
is more compact.
Questions
In addition to writing code, briefly answer the following questions in a
separate file, about 1 paragraph each (see the "Submissions" page).
- How is Java different from traditional compiled languages such as C? How
does this affect its current uses? How is Java expected to be useful in the
future?
- Why is a client/server model useful? What are its advantages and
disadvantages over mainframe systems and LANs?