CS 2121 Introduction to Java:
Programming Assignment 1
Due February 5, 20 points
Overview
In this assignment you will make modifications to the program that you
wrote in Lab Exercise 2.
The modifications will add a text font button (Steps 1-4) and a background
color control button (Steps 5-8) to the applet.
Each of these modifications follows the same basic outline:
-
Add a method with supporting variables to the TextPanel class (Steps 1 and
5).
The added method (changeTextFont() or
changeBackgroundColor()) is added to allow the TextApplet class to
control the way that its text panel displays text.
-
Add a button in the TextApplet class, with an action listener that invokes
the method that is added to the TextPanel class (Steps 3 and 7).
Additional steps specify good places for compilation and testing.
If appropriate, you can do more frequent compilation to make it easier to
catch and correct errors.
For most of the changes described in the steps below, you should refer to
analagous code that you wrote in the lab exercise and use it as a model.
The steps below point the important differences.
Step 1 - Add Text Font Handling to the TextPanel Class
Text font handling is mostly the same as the text color handling
that you implemented in the lab exercise.
The most important difference is that the Font class does not have a
collection of ready-made fonts like the colors Color.red and
Color.blue.
Instead, you need to invoke the Font class constructor (new
Font(...)) to get the different fonts.
You should first make the folowing changes in your TextPanel.java
file.
-
Add a Font array instance variable named textFonts.
This variable provides the font choices in the same way that the
textColors variable provides the text color choices.
The array should contain three bold 18 point fonts: "Helvetica",
"TimesRoman", and "Courier".
The three fonts should be constructed using the Font class constructor.
For example, the following expression creates a bold 18 point "Helvetica"
font.
new Font("Helvetica", Font.BOLD, 18)
Inside the braces that contain the array initial values, you should have
three expressions like this separated by commas.
They will differ only in the font family ("Helvetica", "TimesRoman", or
"Courier").
-
Add an int instance variable named textFontCode.
This variable controls the font in the same way that the
textColorCode variable controls the text color.
-
Add a changeTextFont() method to change the font of the displayed
text.
The code for this method is identical to the changeTextColor()
method except that it uses the textFonts and textFontCode
variables instead of textColors and textColorCode.
-
Add the following line of code to the paintComponent() method just
before the drawString() message.
g.setFont(textFonts[textFontCode]);
This line tells the graphics object (g) to use the font selected
from the textFonts array when it draws a text string.
Step 2 - Compile the TextApplet Class
This compilation will catch most of the errors that you might make.
If there are any errors, you should carefully check the code that you have
added, make corrections, and compile again.
Step 3 - Add Text Font Handling to the TextApplet Class
You should make the folowing changes in your TextApplet.java
file.
The changes should be made in the init() method before the text
panel is added to the content pane.
-
Add a JButton local variable named textFontButton with
label "Text Font".
-
Add an action listener to the text font button.
The actionPerformed() method of the action listener should just
send a changeTextFont() message to the text panel.
-
Add the text font button to the content pane of the applet.
Step 4 - Compile and Run the Revised Applet
You should see a second button in the applet labeled "Text Font".
When you click on this button, the font style of the message should
cycle through "Helvetica", "Courier", and "TimesRoman".
If you follow the link below, you will see what your applet should look
like.
Demonstration Applet
If you get errors during compilation or do not get the expected behavior
when the applet runs, then you will need to locate your errors and re-edit
the TextApplet.java file or the TextApplet.java file.
Then you will need to recompile the changed files again and run the
program again.
Step 5 - Add Background Color Handling to the TextPanel Class
Background color handling is mostly the same as the text color handling
that you implemented in the lab exercise.
The most important difference is that background color is a property of the
text panel itself, rather than its graphics object.
To change the background color of the text panel, you have it send a
setBackground() message to itself in two places:
- in the constructor
This determines the initial background color.
- in the changeBackgroundColor() method
This method is added to the TextPanel class to allow the text applet to
change the background color of its text panel.
You should first make the folowing changes in your TextPanel.java
file.
-
Add a Color array instance variable named backgroundColors.
This variable provides the background color choices in the same way that
the textColors variable provides the text color choices.
The array should contain three colors: Color.yellow,
Color.orange, and Color.green.
-
Add an int instance variable named backgroundColorCode.
This variable controls the background color in the same way that the
textColorCode variable controls the text color.
-
Add the following line of code at the end of the text panel constructor.
setBackground(backgroundColors[backgroundColorCode]);
This line tells the text panel (g) to use the color selected
from the backgroundColors array when it is initially painted.
-
Add a changeBackgroundColor() method to change the background
color of the text panel.
The code for this method is mostly identical to the
changeTextColor() method except that it uses the
backgroundColors and backgroundColorCode variables
instead of textColors and textColorCode.
Since the background color is a property of the text panel itself, you will
need to send a setBackground() message just before the
repaint() message.
The code for this message is identical to the code added to the
constructor.
setBackground(backgroundColors[backgroundColorCode]);
This line tells the text panel (g) to use the color selected
from the backgroundColors array when it is repainted.
Step 6 - Compile the TextApplet Class
This compilation will catch most of the errors that you might make.
If there are any errors, you should carefully check the code that you have
added, make corrections, and compile again.
Step 7 - Add Background Color Handling to the TextApplet Class
You should make the folowing changes in your TextApplet.java
file.
The changes should be made in the init() method before the text
panel is added to the content pane.
-
Add a JButton local variable named backgroundColorButton
with label "Background Color".
-
Add an action listener to the background color button.
The actionPerformed() method of the action listener should just
send a changeBackgroundColor() message to the text panel.
-
Add the background color button to the content pane of the applet.
Step 8 - Compile and Run the Revised Applet
You should see a third button in the applet labeled "Background Color".
When you click on this button, the background color of the text panel
should cycle through the colors yellow, orange, and green.
If you follow the link below, you will see what your applet should look
like.
Demonstration Applet
If you get errors during compilation or do not get the expected behavior
when the applet runs, then you will need to locate your errors and re-edit
the TextApplet.java file or the TextApplet.java file.
Then you will need to recompile the changed files again and run the
program again.
Step 9 - Have Your Applet Validated and Turn in Your Code Files
After the TA has validated your applet, you should turn in a copy of
TextApplet.java and a copy of TextPanel.java.
Be sure your name and the course number appear in each file.
Step 10 - Review This Web Page
This will help solidify the understanding that you have acquired by doing
this assignment.