CIS 121 March 16, 2000
For today's laboratory meeting, try the following exercises. These
exercises will give you practice on some of the nine pieces of the programming
assignment.
Recall the nine pieces of the programming assignment.
- Partitioning the screen into an area in which components are placed
and an area in which figures are displayed.
- Creating buttons and having actions take place based on button
clicks.
- Creating textboxes in which information will be placed and from which
information will be retrieved.
- Creating checkboxes and performing actions based on which box(es) are
checked.
- Drawing a figure on the screen for a random amount of time.
- Setting up a timer.
- Capturing a mouse click event and using the coordinates to aim an
imaginary gun.
- Detecting whether the figure was hit.
- Handle any exceptions which might occur.
Exercises
For each of these exercises, I include a solution. Remember that is only
one solution. There are many ways to solve a problem. But this will at
least give you one solution. In order to benefit from these exercise, try
them out before looking at the source code. You may also look at a
demonstration to see what the applet is supposed to look like and do.
- Partitioning the screen into an area in which components are placed
and an area in which figures are displayed.
Create an applet which is divided into four equal pieces each of which
has a different color.
- Creating buttons and having actions take place based on button
clicks.
- Creating textboxes in which information will be placed and from which
information will be retrieved.
Create an applet which is broken up into two pieces. In one piece, place
10 buttons each one labeled with a different digit (0-9), one button
labeled with the symbol plus, one button labeled with the symbol
=, and one button labeled Clear. In the other piece, place a text field with the initial value 0. When the user clicks
one of the buttons, the digit on that button is added to the end of whatever
number appears in the text field (similar to what happens when you click
a button on a hand-held calculator). When the user clicks the + button,
the number in the text field is stored in a variable, and the text field
is reset to 0. The user can then click on the digits again to place another number
into the text field. When the user clicks the = button, the number in
the text field is stored in another variable, added to the first number,
and the result displayed in the text field. When the user clicks on the
Clear button, the text field is reset to 0.
- Creating checkboxes and performing actions based on which box(es) are
checked.
Create an applet which is broken up into two pieces. In one piece, place
five checkboxes each of which is labeled with the name of a different
color other than white. When exactly one of the boxes is checked, the second piece of the
applet is set to that color. If more than one box is checked, then the
second piece is set to white. If no box is checked, then the second peice
is set to white. The second piece is initially set to white.
There is a method associated with checkboxes called getState(). It returns
a 1 if the checkbox is checked. It returns a 0 if the checkbox is unchecked.
You may use this method if you want to, but it is not necessary.
- Drawing a figure on the screen for a random amount of time.
Create an applet which is broken up into two pieces. In one piece place
a button labeled Draw Figure. Also place a text field in this piece.
When the user clicks on the button a circle is generated for a random
amount of seconds at some point in the first piece of the applet. The
amount of time that the figure will be displayed should be place into
the text field.
In the Graphics class there is a method called fillOval which can be
used to draw a circle. This method has four parameters, the x and y
coordinates of the center of the oval, and the major and minor axes of the
oval. Since we are drawing a circle, we send the same value for the
major and minor axes.
- Setting up a timer.
Create an applet in which a textfield is placed initially set to "301".
Create a Thread which will count this field down, that is this field
represents an amount of time, and we want to count this time down. One
idea you might use is to establish a base time. That is, when the thread
starts, capture the time and use that as a base. For other times, you
can compare against the base and see how many seconds have elapsed since
that time and set the text field accordingly.
- Capturing a mouse click event and using the coordinates to aim an
imaginary gun.
Create an applet of size 500 by 300. Consider the point (250,300) as the
base of a gun. When the user clicks in the applet the barrel of a gun
should be displayed pointing in the general direction of the mouse click a distance
of 30 pixels. That is even if the user clicks at the top of the applet
only 30 pixels of the gun barrel will be displayed.
Hint: If you remember the concept of similar triangles from high school
geometry, it will help you in this exercise.
- Detecting whether the figure was hit.
To detect whether or not a figure is hit, recall there are two different
criteria.
- The gun is aimed in the direction of the figure.
- There is enough time for the bullet to get to the figure.
- Handle any exceptions which might occur.
Create an applet which is broken up into two pieces. In one piece, have
two checkboxes, one labeled On and the other labeled Off. In the other
piece have a text field. When the applet begins, the user may check one
or the other but not both. Use exception handling to handle the event of
a user checking both boxes simultaneously. If this happens, print an
error message in the text field, and set both boxes unchecked. You can
use the method setState to check or uncheck a box. After the user clicks
makes a legitimate choice again, the text field is cleared.