Lab 7
Page 6
Writing your own Java Applet
Your assignment
We have provided you with an applet that should do various things. However
the java code that we have given you is not finished.
We provide code to:
- Start up the applet and place all user interface items on the screen
in their proper place.
- Handle the events
You must make modifications to the event handler so that the applet does the following:
- When the add button is pressed, the numbers in the op1 and op2 fields are added, and the
answer is placed in the ans field
This code has already been provided for you to use as an example.
- When the Black radio button is pressed, the text box labeled "Type in
selected color" is cleared. Any further text you type in that field will
be black.
- When the Red radio button is pressed, the text box labeled "Type in
selected color" is cleared. Any further text you type in that field will
be red.
- When the Green radio button is pressed, the text box labeled "Type in
selected color" is cleared. Any further text you type in that field will
be green.
- When the change color to red button is pressed, the same things happen as if the Red radio button
was pressed, including lighting up the Red radio button.
- When the add to List button is pressed, the text in the add to list field is added to the list
- When something is selected from the list, its text is placed in the selected in list field
- When the count button is pressed the bottom text field is filled with a list of numbers
between the from field and to field (inclusive)
- When the GCD button is pressed, the Greatest Common Divisor of op1 and op2 is computed, and the
answer is placed in the ans field. (Use Euclid's famous algorithm.)
In order to help you with this assignment and to explain and provide the necessary syntax, a
table is available with all the components and methods you will need
along with some examples.
This table is essential for the assignment.
Since this is your first time at Java you are liable to make mistakes. This is not as bad as
it sounds - if you are methodical, finding these mistakes (or debugging) will not be too
hard. A good idea is to write small sections at a time, and then compile. This way, it is
easier to find what section of your code has the error. Another tip is to make sure you
have semicolons at the end of each statement (not the end of each line). Also, check to see if
your parentheses match - it's quite easy to make a mistake with them. Good luck!
Download the file
Download this file by clicking with the
right mouse button, and pick the "save link as" option. As before, save
onto the Desktop.
Now you can edit it (with Textpad) so that your
applet does all the specifed operations. Compile and run the applet after
each modification to do one of the tasks itemized above
so that you know it works before going on to the next.
(Refer back to the instructions for
editing, compiling and running java code.) Be sure your file is
called "CS111Applet.java", and be sure you save the file after each modification and before compiling again.
To get you started, we have filled in the code for the add button.
Before doing anything else, compile this intial version of the applet
without making any changes, just to get a feel for the process.
What your Finished Product will look like
Applets do not print, so if this is a hardcopy this space should be blank.
Helpful Information
Comments
Comments are a part of the code not looked at by the compiler when generating the .class file.
Comments are used to make the code more readable for other people that may have to work on it, or
to remind you of what you were thinking when you did a particular line of code.
In Java
comments start with // and go until the end of a line or are inside a
/* ... */ pair. We have already put in the comments to describe
most of what you will have to do. These comments we have put in beginning with
//??
to indicate you will have to
write at least one line of code that does what this comment says to do. It may be easiest if
after you write that line of code and are fairly sure that it is right you
remove the ??'s. This
will allow you to search the code for ??'s and be finished when there are not any.
Components names
Each component of the user interface has a name that it is called inside the java program.
The picture below gives you the names of all the components that you need to worry about
for this lab in red.
Component
|
Examples
|
Methods
|
Labels - are the simplest component, they are used to label other components.
|
The words "op1", "op2", "ans", "Type in selected color", "Add to list", "Selected in list", "From", and "to"
|
You will not need to call any methods on labels
|
Buttons - Generate an event when they are pressed, this event is sent to an event handler.
Used to present users with commonly exectued options.
|
Badd, Bgcd, BcolorButton, BaddToList, Bcount
|
You will not need to call any methods on buttons |
Radio Buttons - Like buttons but one radio button in a group is always pressed in, used for
presenting users with choices that cannot happen together (the color is either red or black, it cannot be
both.)
|
CBblack, CBred, CBgreen
|
- setState(true) - "Presses" the radio button, and unselects any other that may be selected
|
Text Fields - allow users to enter a text string into a box. Used to input data from the user.
|
TFop1, TFop2, TFans, TFcolor, TFaddToList, TFselectedFromList, TFfrom, TFto
|
- getText() - returns the text inside the text field
- setText(X) - sets the text inside the text field to be X
- setText("") sets the text to be an empty string, thus clearing the field
- appendText(X) - appends the text X to the end of the text field
- setEditable(true or false) - sets if the user can type in the box
- setForeground(Color.red) - sets the text color to red
- setForeground(Color.black) - " black
- setForeground(Color.green) - " green
|
Text Areas - are much like text fields only they are more than one line and contain scroll bars.
Text areas are used in word processing applications where the user enters large amounts of text data.
|
TAcount
|
Same as Text Field
|
Choice Lists - Allow Users to select items from a list of Choices
|
Clist
|
- getSelectedItem() - Returns the text of the currently selected item
- addItem(X) - Adds X to the list
|
Working with Text in your program
You have already learned a few ways that the computer can store numbers, in plain binary form or as a series of ASCII characters. Programmers often need to convert between these
two formats and most programming languages provide
an easy way to do so. In this lab we provide you with textToNumber and numberToText.
For an example lets use the "Add" button in this assignment.
It has to take the two numbers in TFop1 and TFop2 and add them together, storing the answer in TFans.
The numbers in TFop1 and TFop2 are in Text Fields and are stored in text format (ASCII).
In order to add the numbers they must be in Number format (two's complement).
Anything written in TFans must be in text format. So we must read the text from TFop1 and TFop2,
convert the text into numbers, add the numbers, convert the answer number into text and store it
in TFans. The Java code we gave you does just this:
.
.
.
if(e.target == Badd) {
// declare 2 integer variables, x and y
int x,y;
// Set x to the number value of what Text Field op1's text is using textToNumber
x = textToNumber(TFop1.getText());
// Set y to the integer value of what Text Field op2's text is
y = textToNumber(TFop2.getText());
// Tell Text Field ans to set its text as the text representation of x + y HINT use numberToText(x+y)
TFans.setText(numberToText(x+y));
.
.
.
Other text tidbits
- Inside your program text values must appear inside of quotes (e.g. "Text Value")
- The + sign is used to attach 2 different texts together to form a new one. (e.g. "Hi " + "There" is the same as "Hi There")
- "\n" is a special text value that means new line (it is equivalent to pressing return on the keyboard)
Extra Credit
- When figuring out the sum, gcd, or listing numbers, what happens
when one of the fields give is not a number? Fix this problem by
displaying ERROR in
the answer box or the count text area.
- The program currently lets you add blank
entries to the list; make it so that
blank entries are ignored when the add to list button is pressed.
- Add other buttons (e.g., multiply, clear all fields, anything you want!)
- The count text area, answer, and
selected list item fields currently let you type in them.
There is no reason for a user ever to be typing
stuff in these areas, so make it so they can't.