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: 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.

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.

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:

You should first make the folowing changes in your TextPanel.java file.

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.

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.