Due date: Monday, February 20, by the beginning of your lecture section.
Bring to lab: A hand-written plan for drawing the "no-clicking" sign.
Exercises: 1.9.3, 2.7.2, 2.7.6, 3.11.2 a, b (turn in with your HW1 on Monday).
This lab will introduce you to the tools with which you will be working this semester. Your task is fairly simple. You will construct a Java program that draws an image resembling a roadside warning sign, but with a message more appropriate for a computer screen, as shown below.
First you will need to determine the Java commands required to draw such a picture. Then you will write a Java program that draws and modifies the picture in simple ways in response to actions the user performs with the mouse.
To start, you must log in to one of the Macintosh computers in our lab. Each student in the course will be given an account on our computer science cluster of machines. This is separate from your general campus account for reading email. You will receive a user name and initial password. After you have logged in for the first time, you should change the password to something that only you will know and that will be easier for you to remember. Don't, of course, make your new password too easy to guess. In particular, your password should be at least 8 characters long and should be a mix of letters (preferably both upper and lower case) and numerical digits.
To change your password you should:
Now, log out and log in again with your new password. To log out simply select the last item in the apple menu.
The files you create for our course laboratory projects will not actually be stored in the computer on which you are working. Instead, they will be stored on our department's file server, benjerry. By entering the account information for your account, you have instructed the computer you are using to access your files on benjerry. You can access your files on benjerry from any computer in our lab by simply logging in to that computer.
To start working on the lab, click on this link to download the intro starter.
This will download a file archive called "hw01_introStarter.tar.gz" to your Desktop. Double-click on this archive to extract the files to a folder on your Desktop called "hw01_intro". Before continuing, you will want to make a folder in your "public_html" folder for your work in CS101. To do this, double-click on the "Macintosh HD" icon, open your home directory by selecting your username in the left column, and double-click on the "public_html" folder. Now select "New Folder" from the File menu at the top of the screen. Name your new folder "cs101" (don't include a space in the name). Finally, move the "hw01_intro" folder from the Desktop to the "cs101" folder. If you have problems, please ask for help in lab. Now, go to your "cs101" folder and open your copy of the "hw01_intro" folder to see its contents.
To help you learn the primitives for creating graphical objects from within a program before dealing with the details of writing a program, we have constructed an environment in which you can experiment interactively with the graphics primitives. This environment is a program that lets you create objects by clicking buttons and filling in fields that describe the locations and dimensions of the objects. If you think of this program as a drawing program (like Adobe Illustrator) you will quickly conclude it is the worst drawing program in the world. Its interface is designed specifically to use mechanisms similar to those you will use from within your programs, and certainly not for usability as a drawing program. Hence its name: NotPhotoShop.
This part of the lab has you experiment a bit with NotPhotoShop in order to familiarize yourself with the commands you will need to draw your own rendition of our "no clicking" sign.
In the folder you just opened, you will see a file named "NotPhotoShop" and a folder named "NoClicking". Double-click on the "NotPhotoShop.jar" icon in your "hw01_intro" folder. Our drawing program will start and will display a window on your screen that should look like this:
The components in the NotPhotoShop window are divided into five main units:
To see how all these components work, let's add an object to the display.
You can begin by specifying the object type. The type of object to be created is controlled by the pop-up menu located just to the right of the word "new" in the constructor area. By default, the program starts with the "FramedRect" item selected in the menu. If you depress the mouse on the box labeled "FramedRect", a list of the other choices will appear. Six options are available:
Using the mouse, select "Line" instead of "FramedRect". As you do this, notice that the labels on the text entry boxes to the right of the menu change to reflect the kinds of information you need to enter to specify the appearance of a line rather than a rectangle. For a framed rectangle you need to enter the coordinates of its upper left corner and its width and height. For a line you need to enter the coordinates of two points -- its end points. Use the menu to select other options to see what information they require. To display a piece of text you enter the text to be displayed (although the box for entering the text is rather small) and the coordinates of the upper left corner of the rectangle in which the text should be placed. Ovals require the same input as rectangles, and the rectangle specified determines the bounds the oval to be drawn.
To continue, make sure that "FramedRect" is the selected menu item. Fill in the boxes to the right of the menu with values to create a 100x100 rectangle at a location 70 pixels from the top of the drawing area and roughly centered between the left and right boundaries of the drawing area. To determine the actual x value to enter for this rectangle, position the mouse cursor where you estimate the left edge of the rectangle should be and use the "mouse meter" to determine the x coordinate of that point. Enter this value in the box labeled "x" immediately to the right of the menu. Then enter 70 as the "y" coordinate and 100 for both the width and height.
Once all these values have been entered, press the "Construct" button and the rectangle will appear.
Experiment with constructing lines, rectangles, ovals, etc., so that you can see how to create the necessary components of the no-clicking sign shown at the beginning of this document. The interior of the sign should be colored yellow like a good warning sign. To produce such a sign you will use a "FilledRect" for the sign's background and a "FramedRect" for the edge. Use the plan you developed on paper to try create the various components of the sign.
Now, you will construct a Java program to draw the warning sign. You will do this using a program named BlueJ, which provides everything you need to type in and run Java programs.
The construction of a single Java program may involve the use of several files. One or more files will contain Java code, the actual text of your program. If your program is designed so it can be run in a web page, another file is required to tell the Java system run-time details like the size of the window your program should be given. This file is written in HTML, the notation used to describe web pages, and these Java programs are called applets. In addition, access to many of the facilities of Java and our graphics system are provided through two other files named "classes.zip" and "objectdraw.jar". The files associated with a specific project are stored in a folder with the project's name.
Start the BlueJ program by clicking on the blue jay bird icon in the dock.
You should now see a window containing your Java project that looks something like the image shown below.
This window has three main regions. In the upper left, it displays buttons for common operations. In the upper center and right, it displays separate icons for each file that makes up the project. In our case, the first icon corresponds to a text file in which you can add information about the project for other humans to read. The second icon corresponds to the only Java program file, "NoClicking.java", in this project. In the bottom is the object bench, which will hold icons representing objects that you can issue commands to.
For now, double-click on the NoClicking icon to show the contents of the "NoClicking.java" file in which you will place your code.
BlueJ should now open a new window that looks something like the image shown below.
Here is the text of the program as it should appear initially:
import objectdraw.*;
import java.awt.*;
// CS 101 Laboratory 1
// Enter your name, lab section and the date here.
public class NoClicking extends FrameWindowController{
public void onMouseClick(Location point)
{
// commands placed here are executed after the user clicks the mouse
}
public void begin()
{
// commands placed here are executed when the program begins to execute
}
public void onMouseEnter(Location point)
{
// commands placed here are executed when the mouse enters the program window
}
public void onMouseExit(Location point)
{
// commands placed here are executed when the mouse leaves the program window
}
public void onMousePress(Location point)
{
// commands placed here are executed when the mouse button is depressed
}
public void onMouseRelease(Location point)
{
// commands placed here are executed when the mouse button is released
}
}
The text we have placed in "NoClicking.java" is the skeleton of a
complete Java program. It includes the header for the definition of a
class NoClicking that extends FrameWindowController
and within the class, headers for the event handling methods you will
use. We have not, however, included any Java commands within the
bodies of these methods, only a Java comment that reminds you when the
Java system will follow any instructions you might add to the method
body. (By the way, particularly observant students may have noticed
that the textbook refers to WindowController. For the time
being, you can assume that WindowController and
FrameWindowController are the same.)
You should follow our lead and begin writing your program by typing comments rather than actual Java commands. Near the top of the file we have included a temporary comment telling you to enter your name, lab section time and the current date. Such identifying comments are always good practice, and in a class like this they make the grader's job much easier. BlueJ provides the familiar capabilities of a simple word processor or text editor. You can position the cursor using the mouse or arrow keys. You can enter text and/or cut and paste text using items in the edit menu. Use these features to replace our comment with your name, etc.
Next, add Java instructions to the body of the onMouseClick method that will draw a "no clicking" warning sign. This is the first method skeleton we have included in "NoClicking.java".
As a first step, let's add the single instruction needed to draw the rectangle that frames the contents of the sign. The form of the command you will need to enter is:
new FramedRect( ..., ..., ..., ..., canvas);where all the ...'s need to be replaced by the numbers describing the position and size of the rectangle. This line should be placed immediately after the comment line in onMouseClick (i.e., just before the line containing the "}" that ends the method body).
Your rectangle constructor should now be below the comment in the onMouseClick method. Now that it does something, it would be a good idea to update the comment in the onMouseClick method to say what it does so far. Replace our comment with something more appropriate, like "draws a rectangle when the mouse is clicked". You should get in the habit of updating comments to keep them as accurate as possible as you add instructions to your programs. Not only does this mean you will not have to go back and add comments when you're done, it will help you to remember what everything does as you are writing your programs.
Now you are ready to run your program and see if it works. To do this:
When BlueJ encounters an error in your code, you should correct it and recompile the program. You may have to correct and recompile numerous times if you have multiple errors in your code.
Once you have corrected all typing mistakes, BlueJ will compile your program and indicate that it was compiled without syntax errors. To run your program, right-click on the NoClicking icon in the project window and select "new NoClicking()" from the menu that appears. This will open up a "Create Object" dialog window in which BlueJ will suggest a name. Go ahead and click on the "Ok" button. An icon will appear in the object bench (don't worry about this for now), and BlueJ will run your program in a new window titled "panel" followed by some number. This window may appear behind your BlueJ windows so you may have to move some windows around to find it. A better way is to use the command-tab key combination (hold down the command key -- the one with an apple icon on it -- and then hit the tab key) repeatedly to cycle through all your running programs, including the NoClicking program you just started.
The new window should appear blank at first. Move the mouse into the window and click, and a rectangle should appear because Java invokes your onMouseClick method, which includes the instruction to construct a rectangle. If this happens, smile. If not, select "quit" from the File menu inside your program window. Go back to BlueJ by clicking on one of its windows or using command-tab. Examine the instructions you typed carefully to see if you can find any mistakes. If so, correct, recompile (this will clear the object bench), and try running your program again. If you can't find the problem, ask your instructor or a TA for assistance.
Once your rectangle program is working, you should add more instructions to it to turn it into a complete warning sign drawing program. Back in BlueJ, immediately beneath the line you added to draw the rectangle, add additional lines to create new Text(...), a new FramedOval(...) and all the other components you will need. In the NotPhotoShop version of the drawing, we told you to have a yellow background for the sign. Leave that part out of the version your program draws for now; we will add it later. As you work, it is a good idea to compile and run your program every time you add a line or two to ensure that you catch mistakes early. Also, make sure your comment line gets updated by the time you are finished. When you are done, your program should draw a sign when you click in the window, and then do nothing until you tell it to quit. In reality, the program draws another copy of the sign each time you click, but you can't tell, as each new sign is drawn exactly on top of the previous one.
Now, to explore event handling methods a bit more, revise your program so that it reacts to the mouse in more interesting ways. In the revised version, the sign will be drawn as soon as the program begins to run. The sign will change, however, as the program runs. In particular, when the user moves the mouse into the program window or presses the mouse, your revised code will alter the appearance of the sign.
Making the program draw the sign when it first starts is easy. You initially placed the code to draw the sign in the onMouseClick method. There is a skeleton for a method named begin immediately after the onMouseClick method definition in the "NoClicking.java" file. As you might guess, the instructions in begin are followed when your program first begins to run. Use cut and paste to move all the drawing instructions from the onMouseClick method into the begin method. Don't forget to update your comments. Run your program again. It should now display the sign even if you don't click the mouse.
Notice that it makes more sense logically to have the begin method occur before the onMouseClick method. We arranged them in the opposite order so that you would be able to find onMouseClick more easily. The point is that the computer does not care which order you write them in your program. Only the names are important (though we urge you to put them in a logical order so that it will be easier for all of us to read!). The order of instructions within a method are important, but the order in which you define the methods within the program is not.
Making the sign change in response to the mouse is a bit trickier. Suppose, for example, that you want to emphasize the sign's warning by changing the color of the word "CLICKING" from black to red when the user moves the mouse into the program's window. As you saw in NotPhotoShop, there is a setColor operation that you can use to change the color of the text. There is also an obvious place to tell Java to make this change. The onMouseEnter method is executed by the FrameWindowController whenever the mouse moves into your program window. Placing an appropriate setColor in that method would do the trick.
The problem is that you can't simply say setColor in the onMouseEnter method. If that was all you said, Java would have no way of knowing which of the several objects' color to change. It could change the rectangle, the oval, the text, all of them, etc. Your code has to be more specific and identify the object that should change.
To be able to specify the text object as the object whose color we wish to set, we will have to give it a name. We will use the name message, but we could use any name that seems appropriate.
Associating a name with an object requires two steps. First, we have to include a line that declares or "introduces" the name. This line informs Java that we plan to use a particular name within the program. At this point, we don't need to tell Java what object it should be associated with, but we do need to specify what sort of object it will eventually be associated with. We plan to associate the name message with an object created as new Text, so we have to tell Java that the name will be associated with a Text object. The form of a Java declaration is simply the name being declared preceded by the keyword private and by the type of object with which it will be associated. So, the form for our declaration is:
private Text message;You should add a line containing this declaration to your program immediately before the heading of the onMouseClick method.
Now you have to tell Java which object to associate with the name message. Your program currently contains a construction of the form:
new Text("CLICKING", ..., canvas);
that creates the text on the screen. To associate the name
message with the text object this line creates, revise this line
so that it looks like:
message = new Text("CLICKING", ..., canvas);
This is an example of a construct called an assignment
statement. It tells Java that in addition to creating the new
object, it should associate a name with it. Shortly, you will convert
some of your other constructors into assignments.
Now that the text has a name, we can use the name to change its color. Within the body of the onMouseEnter method add the line:
message.setColor(Color.RED);Then, run your program (correcting any errors as needed).
The program isn't quite complete. It draws the sign immediately and makes the text turn red when the mouse enters the window, but it doesn't make the text black again when the mouse is moved back out of the window. You should be able to figure out what to add to make it black when the mouse exits. Give it a try.
To get more practice using names and other event handling methods, we would like you to modify your program a bit more. First, change the program so that while the user is depressing the mouse button, the circle with the diagonal line through the text disappears. (Of course, it should reappear when the mouse is released.) This will require that you declare names for the circle and the line and associate them with the correct objects. These declarations should appear right after the declaration of message. Your code to make the objects disappear and reappear goes in the onMousePress and onMouseRelease methods. You can use the hide and show methods to handle the disappearing and reappearing.
Finally, add the yellow background to the sign. We didn't have you do this earlier because you had not yet seen how to associate names with objects. Associate a name with the background rectangle when you create it and then use the name to set its color to yellow.
|
Value | Feature |
| 4 pts. | Drawing the sign correctly when the program starts |
| 4 pts. | Changing the color of the text correctly |
| 4 pts. | Making the slash and circle disappear and reappear correctly |
| 2 pt. | Meaningful names used in declarations |
| 2 pt. | Informative comments |
| 2 pt. | Good and consistent formatting |
| 2 pt. | Good choice of Java commands |
The last point is worth additional discussion. Often there are several Java commands that can be used to accomplish the same result. It is important to pick the most appropriate commands. For example, in this lab you could make your circle and slash disappear by sending them behind the background, but this would not be the best choice as it is not obvious what your intent is and there is a more direct way of accomplishing the same result.
Congratulations, you have written a Java program! We will grade your assignment both by running your actual program over the web and by reading a printout of your Java source code. Homework is due by the beginning of your lecture section. Here is the procedure for submitting your work:
For this and all other assignments, you should not modify the code after the due date and time (we can easily check this). However, we encourage you to continue experimenting with your programs by working on a separate copy. You can do so by making a copy of the entire project folder and working on the copy. On the Mac, you can copy a folder by holding down the option key, dragging the folder little bit in any direction, and then letting go,
Back to Computer
Science 101 Home
Department of Computer
Science