Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Assignment 2 - Using Objects

Your first Java applet uses Labels, Fonts, Colors and Events to work some magic.

It's magic! Run your mouse over the applet that appears here, and see what happens. What happens when you hold down the mouse button as you move the mouse?

In this homework, you'll modify an existing applet, adding new objects and sending them messages. The purpose of this homework is to:

  • make sure you know how to use the Java Development Kit to build Java applets. You'll type in an applet, compile it, write an HTML page which references your applet, and make sure everything works.
  • learn how to create objects. You'll add several different objects to the basic applet you create in Step 1.
  • learn how to send messages to objects. To this end, you'll tell your new objects to perform a couple of interesting tricks.
Before you tackle this homework, make sure you know how to:
  • Use a text editor to create a file.
  • Create an HTML file with hyperlinks and a variety of HTML tags.
  • Use FTP to post your HTML page on your Web site.
  • Use FTP to send additional files, such as images, to your Web site.
These skills were covered in homework assignment. Here are some things you should remember from the lesson and your text to help you through this assignment.
Back to Top

Environment, Edit, Compile, Run

Before you can create your Java program, you have to prepare your environment. In Windows, the easiest way to do that is to create a shortcut. You must have completed Lesson 1.6, "Your Development Environment" before starting your homework assignment. 

Make sure you can:

  • Edit: Use the SciTE editor or the JCreator editor to create Java source code.
  • Compile: Turn the Java source code into byte-code.
  • Run: Use the Applet Viewer and your Web browser to run a Java applet.

Creating New Objects

To create a new object, you have to do the following:
  • Tell the compiler what kind of thing you want to create, using its class name. If you want to create a new Chevy object, for instance, your object declaration would start with the word Chevy.
  • Give your object a name. For this, you have to follow the object naming conventions and rules. You can look in your text if you've forgotten them. You could call your Chevy appleGate, for instance. (If Steve King could name a Plymouth Christine, then you can certainly name your Chevy appleGate, don't you think?)
  • Use the object's constructor to actually create the new object. Think of the constructor as a factory that uses the class definition to build an object for you, right on demand. The name of the constructor will always be the same name as the class, but classes may have several different constructors, each taking different numbers or types of arguments.
  • For instance, the Chevy class definition could have one constructor that takes no arguments, returning a plain white Nova, for instance. Another constructor could allow you to specify the model and the color. You'll have to read the class documentation to find out which arguments you can pass.
  • Here is the line you'd write to create the Chevy object we discussed above:
     
      Chevy appleGate = new Chevy();

Talking To Your Objects

Once you've created a new object, you can send it messages to get it to perform the tasks you want. The syntax for sending a message is:
    object.message(arguments);
    appleGate.startEngine(250);
The first line shows the general case, the second example shows how you'd send the startEngine() message to the appleGate object, passing 250 as the argument. The 250 could stand for the number of seconds before the engine started, for instance. You can only send the startEngine() message to the appleGate object if its class (the Chevy class, in this case), has a startEngine() method. If it does, you'll have to look at the documentation for startEngine() to see what kinds of arguments you can pass.
Back to Top

Step-By-Step Instructions

Step 1

Write a starter program, modeled on the GraffitiThree applet in your text, named Magic.

  • Set up your environment to use the development environment of your choice. [In other words, make sure you have completed Lesson 1.6, "Your Development Environment", before you get to this step.]
  • Make sure you enter your personal information to the top of your source code. Add your name, your class, your Java ID number [1036-100], and a brief description of the program. Every program you write should include this information.
  • Remember that your source file will have to be called Magic.java, and it will have to contain one public class named Magic.
Step 2

Define two Label objects in the attribute section of your applet.

  • The name of the first Label object should be myName, and its text should contain your name [just as the text in the Label shown in the example applet contains my name].
  • The name of the second Label object should be myID and should display your ID for this class. [Don't use your student ID, please.]
  • Make sure you remove the existing Label object definition for senselessScrawl.
Step 3

Define two Font objects. You'll use one Font object to change the Label myName, and the other Font object to change the appearance of the Label myID. Remove the Font definition for realBigFont.

  • Refer to your textbook for additional help on the arguments required by the Font constructor. Be very careful that you don't accidentally reverse the last two arguments. This is very common and does not result in a syntax error; it does, however, lead to Font objects that are too small to be seen.
  • You can name your Font objects anything you like, but make sure you use meaningful names. I usually name my user-interface fields with a trailing identifier that identifies the kind of object it is. In this case, for instance, I'd name the Font intended for the myName field, myNameFnt, where Fnt stands for Font. If I had a large Font and a small Font, I might name them largeFnt and smallFnt respectively.
Step 4

Define two Color objects. One of these objects will be used to color the text of myName, the other will be used to color the text of myID.

  • Using the JavaDoc online documentation, look up the Color class. You'll find it in the java.awt package, just like the Label class. If you need help, refer to the online class lesson, "Using the JavaDocs".
  • Locate the Constructors section for the Color class. Notice that there are several constructors, each of which allows you to specify the characteristics of your Color object in a different way. Use a different constructor for each of your two Color objects. [One of the constructors requires float values. To specify a float value, write a decimal number with an f suffix, like this: 0.25f ]
Step 5

Complete the init() method. Inside the init() method you will change the font and color for each of your Label objects, change the background color for your applet, and add both Label objects to the surface of your applet. 

Inside the init() method:

  • Send your applet a setBackground() message, passing the constant Color.white as an argument. You can omit the message receiver, if you like, or use the keyword this.
  • Send each of your Label objects the setFont() message, passing the appropriate Font object as an argument. You can see an example of how this works in your text. Or, you may search online.
  • Send each of your Label objects the setForeground() message, passing the appropriate Color object as an argument.
  • Add both of your Label objects to the applet, just as the program previously did with the Label named senselessScrawl . Remove any references to the Label senselessScrawl from the init() method.
Step 6

Specify mouse-move actions. When the user moves the mouse over your applet [without pressing the mouse button], the mouseMoved() method will be called. You should write the statements necessary to make both of the Labels invisible whenever this occurs.

Here are the steps required to do that:

  • Remove the existing statement from the mouseMoved() method. Since you have eliminated the senselessScrawl field, you can no longer send messages to it.
  • Inside the mouseMoved() method, add a statement that sends a setVisible() message to the Label named myName. Pass the constant false as an argument. This will make the Label invisible when the mouseMoved() method is invoked.
  • Add another statement that sends the same message to the object named myID.
Step 7

Specify mouse-drag actions. When the user drags the mouse over your applet [moving with the mouse button pressed], the mouseDragged() method will be called. You should write the statements necessary to make both of the Labels visible whenever this occurs.

To accomplish this, add the same two statements you added to the mouseMoved() method to the mouseDragged() method. These statements must go between the two braces that mark the body of the mouseDragged() method. Initially, there are no statements there.

After you have added the two statements, replace the argument false in each statement with the constant value true. This causes each of the Labels to make themselves visible when they receive the message.

Step 8

Compile and test.  Use the Java compiler (javac or jikes) to compile your source code. If any errors appear, correct them one-by-one, until your program compiles cleanly.

Test your program by running it inside the appletviewer program. Modify your Assignment 2 Web page by adding an applet tag, and then use appletviewer to display the Web page. [Remember that appletviewer will not display any of the HTML in your Web page, only the applet.]

Back to Top

Finishing Up

Post your applet. Once you've tested your applet locally, add it to the Assignment 2 HTML file on your Web site.

 

Back to Top