Assignment 10 - Using Inheritance
For Assignment 10, you'll get a chance to create a useful RollOver label class, using the technique of inheritance. A RollOver label is a specialized version of the Label class that changes colors, or text, when your mouse moves over the label.
You can see the RollOver label class at work in this applet which uses several RollOver objects to redirect you to a specific Web site. You can look at and modify if you like the source code for the ROMenu class by clicking here.
The ROMenu class uses HTML PARAM tags to specify a list of menu captions, and to associate those captions with specific URLs.
The class also uses PARAM tags to specify the foreground and background colors, specified as a 6-digit hexadecimal, integer--are passed to the RollOver label which creates the rollover effect.
For your assignment, you will complete the RollOver label class, and a test program like the one at the end of this page. You won't use the ROMenu class, shown above, as part of your assignment.
To complete the homework, you'll have to understand:
- How to subclass the Label class to create a specialized kind of Label.
- How to write constructors for your class.
- How to use the this() and super() methods.
- How to use the this object to initialize a field with the same name as a parameter.
- How to handle the MouseListener() interface.
Information on the Label class can also be found in the text. Information about the MouseListener interface, along with the rest of the event interfaces is also presented in the text. You may also want to examine the Sun JavaDoc documentation for the classes, which can be found in the java.awt package and the java.awt.eventpackage.
You may also search the internet for additional information on the above.
Step-By-Step Instructions
Step 1
Define your component's attributes. Start by creating a new class named RollOver. Your new class will extend the Label class, instead of extending the Applet class. You'll need to implement the MouseListener interface, as well, which is as simple as adding the line:
implements MouseListener
to the end of the class header. Remember to import the java.awt and java.awt.event packages. You don't need to import. java.applet.
Once you've set up the initial skeleton for your RollOver, add the following six fields:
- Add two private String fields named onMsg and offMsg. The onMsg will be displayed when the mouse is moving over your RollOver, and your offMsg will be displayed when the mouse is moved off of your RollOver. If you only supply a single String when constructing your RollOver, it will be used for both messages. You don't have to initialize either field, because you'll do that in the constructor.
- Add four private Color fields, named onFg, onBg, offFg, and offBg. You don't have to initialize these fields either, because they will be initialized in the contructor as well. The onFg and onBg colors will be used when the mouse is hovering over your RollOver; the other two will be used when the mouse is not over your label.
Before you leave, add the following "dummy" methods to the bottom of your RollOver class. Later on, we'll come back and fill in two of these [mouseEntered() and mouseExited()], but in the meantime, the dummy methods will let you make sure that your code compiles.
// -------------------------------------------------------
// Dummy methods for MouseListener interface
// -------------------------------------------------------
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { } |
After you've added these methods, make sure your code compiles before you continue.
Step 2
Write a working constructor. Your working construct will take 7 arguments:
- a String named offMsg,
- an int named align,
- a String named onMsg,
- a Color named offFg,
- a Color named offBg,
- a Color named onFg,
- a Color named onBg
There are three tasks to complete in this step. You may want to compile after each task, just to make sure you're on the right track. After you've finished writing the constructor, compile one last time before going on.
1. Construct your "parent"
Your working constructor needs to pass the first two arguments it receives, offMsg and align, on to the Label constructor. Do this by using the super() method.
2. Initialize each of your fields
Each of the arguments passed to the working constructor corresponds to one of the fields defined in the RollOver class, with the exception of the align argument which is passed on to the Label constructor.
Assign each of the remaining arguments to the appropriate field. Because the arguments and fields have the same name, you'll have to use the object qualifier named this when refering to field names.
Since the user may wish to supply only a single String, check to see if the third argument, onMsg, consists of the empty String (""). If it does, assign the argument offMsg to this field as well as to the first field. [The same String will then be used in both cases.]
3. Update the Interface
Now that your fields are initialized, you need to hook up the event system and set the initial colors for your RollOver. To set the colors do this:
- Check to see if offBg is null. If it is, don't do anything. If it is not, then set the background color for your RollOver object, using offBg as the color.
- Check to see if offFg is null. If it is, don't do anything. If it is not, then set the foreground color for your RollOver object, using offFg as the color.
To hook up the event system, call the addMouseListener() method, passing this as an argument. At this point, your constructor is finished.
Compile, and clean up the syntax errors. Then, download the file TestRO1.java, compile it and run it in appletviewer to see if your RollOver label works.
// TestRO1.java
// Test the RollOver class after construction
import java.awt.*;
import java.applet.*;
public class TestRO1 extends Applet
{
public void init()
{
RollOver one =
new RollOver("One", Label.CENTER, "Two",
Color.red, Color.blue,
Color.green, Color.yellow);
one.setFont(new Font("Helvetica", Font.BOLD, 36));
add(one);
}
} |
Create an appropriate HTML file and make sure you place your TestRO1.class and RollOver.class in the same directory. Note that you can create a RollOver object and then send it the same messages you would send to the built-in Label object [add(), setFont()]. At this point, the RollOver class doesn't do any "rolling over"; all you can see is the original text, and the red and blue colors you selected.
Click here to see the TestRO1 applet at work.
Step 3
Add the following methods to the RollOver class. These are the basic methods that make the RollOver labels "work". Whenever the user's mouse cursor enters the space claimed by a RollOver component, or whenever the user's mouse cursor leaves the space clamed by a RollOver component, a MouseEvent is generated.
When entering the component's space, the mouseEntered() method is called; when leaving the space, the mouseExited() method is called. You've already added both of these methods in the last step. All you have to do here is to fill in the code inside the methods.
Here are the three tasks to carry out inside the mouseEntered() method:
- Set the background color of your component using the field onBg.
- Set the foreground color of your component using the field onFg.
- Set the text of your component using the field onMsg.
Here are the three tasks to carry out inside the mouseExited() method:
- Set the background color of your component using the field offBg.
- Set the foreground color of your component using the field offFg.
- Set the text of your component using the field offMsg.
Compile your code and clean up any errors. Then, compile TestRO1.java once again.
This time, your program should look like this when it runs. Note that the Label now changes both color and text when the mouse is run over it.
Step 4
Complete the interface. Even though your RollOver works, it's not very full featured. Fix that by adding the following four methods and constructors:
1. Overloaded Constructors
Most users want to create RollOvers as easily as they can create Label objects; they won't necessarily want to pass seven arguments each time they need to create a RollOver object. You can accomodate such users by adding a few new constructors that uses the method this() to call the working constructor you alread wrote. Here's how:
- Add a constructor that accepts a single String argument. Inside the constructor, call this(), passing along the String as the first argument, the value Label.LEFT as the second argument, the empty String "" as the third argument, and the value null for each of the remaining arguments.
- Add a constructor that accepts a String and an int alignment parameter. Follow the same steps you did for the previous constructor. Instead of passing Label.LEFT for the second argument to this(), however, pass the alignment parameter received from the user.
- In the same vein, add a third overloaded constructor that takes a String and a Color argument. Using this() once again, pass the String as the first argument (offMsg), and the Color as the fourth argument (offFg). For the remaining arguments, pass the same values as you used for the first of the overloaded constructors.
2. The addNotify() Method
If a user creates a RollOver object, but doesn't specify foreground and background colors, you can set the foreground and background colors by using the defaults provided by the Label class. You can set offFg to the value returned by calling getForeground(), and offBg to the value returned by calling getBackground(). For the "rollover" colors, you can simply reverse the process, so when a user rolls the mouse over a default RollOver object, the background and foreground will switch places.
The only problem with this scheme is that if you try it, you'll find that getForeground() and getBackground() don't work if you try to call them in the constructor. That's because Labels, like most other components, inherit their default colors from the container that they are added to. What you need is to be notified when your RollOver object is added to a container, so you can set the default foreground and background colors if they are not already set.
Fortunately, that's what the addNotify() method is for. Here's how you write it:
- Add a void method that takes no arguments named addNotify().
- On the first line of the method, call the superclass addNotify() method.
- Check each of your color fields, offFg, offBg, onFg, onBg.
- If a field is null, set its value, as specified above, by calling getForeground() or getBackground(). Remember that onFg must be the reverse of the offFg color.
Test your RollOver class by creating your own test applet that uses each of the constructors you just created. Post the finished test applet on your Web site. Here is a very simple example with four RollOver labels added to an applet that has a black background and yellow foreground:
Finishing Up
Post your applet. Once you've run your test applet locally, add it to the Assignment 10 HTML file on your Web site. Remember that you must send the .class file for both the RollOver class and for your test applet class, as well as an updated HTML file. When you use FTP to send up your .class files, remember that they must be transferred using binary mode, not ASCII.
|