CS 210 Intro
I object!
Assigned Tuesday, January 21
Lab summary
This lab allows you to explore the basic graphics package we will be using at the
start of this course, as well as some of the simplest forms of object-oriented
programming. The main thing I would like you to focus on is how working in Java
is very different than in imperative languages like C.
Background
In object-oriented programming, some of the most basic types of instructions
we can give to the computer are to create sets of objects that accomplish
various purposes.
Somebody else has already done the job of defining how these objects work,
and what they do (you will be learning that soon!). For now, we are just
going to tell the computer to create some individual objects that we can use
to form a picture.
The objects themselves have already been defined in a graphics package called
"objectdraw". You will first run a simple program that allows you to create
graphical objects and change them in various ways. Then you will use a Java
system to write instructions for the computer to build the same picture.
The activities you will engage in
Locate and run the "NotPhotoshop" program
- Use the instructions you have been given to access the CS210 Folder
- Open the Course Materials folder.
- Locate the file NotPhotoshop.jar
- Drag NotPhotoshop.jar onto your desktop, so you can have
a local copy of the program
- Double-click on the copy you made, start the program
- This should pop up a window with a a number of items in it, including
two rows of buttons at the bottom, and a drawing area in the upper right
Explore the program
- You can use the buttons in this program to send instructions to the
computer to create and manipulate various kinds of objects
- The program window offers the following five different areas:
- The drawing window is the large rectangle appearing on the
upper portion of the right side of the window. When you create objects, they
will appear here.
- The mouse tracker consists of the x and y coordinates below
the drawing window. These tell you where the computer thinks the mouse is,
when it is inside the drawing window. Move the mouse around to see how these
values change.
- The object constructor is the shaded area just below the
mouse tracker. This area contains entries for you to fill in information
describing the kinds of objects you want to create. The entries are set up to
show you what kinds of commands you would have to write in Java in order to
accomplish in a Java program the kinds of things you are going to do by hand
here.
- The method invocation buttons allow you to send messages to
objects you have already created, in order to modify them or obtain
information from them. You can send a message to an object by clicking on it
in the drawing window and then clicking the button for the message you want to
send.
- The output area will print information that you get back
if you send a message to an object that requests some kind of information.
This area will also display error messages if you try to do something the
program doesn't like when creating an object or sending a message to one.
- Learn about the coordinate system that Java expects you to use:
- Try moving the mouse to the exact position x:30 y:50
- Find out where the position x:0 y:0 is in the drawing window
- If you move the mouse right, what happens to the x and y values?
- If you move the mouse down, what happens to the x and y values?
- What are the x and y coordinates of the four corners of the drawing
window?
Create your first objects
There are six different types of objects provided by the objectdraw package:
FramedRect, FilledRect, FramedOval, FilledOval, Line, Text. They each require
different types of information when they are created, and they can each be
sent different types of messages.
- Create a FramedRect object
- Make sure that FramedRect is selected in the new
box in the object constructor area.
- Fill in blanks to tell the system you want a rectangle that has a
width of 100, has a height of 100, and is located 70 pixels from the top of
the drawing area, and roughly half way between the left and right edges of the
drawing area.
- After you have filled in the values you want, click on
Construct to create the object (it should appear on the drawing
window).
- There are two ways to make an object disappear
- Tell the object to "hide" itself (the object is still there, it is
just "invisible"...tell the object to "show" itself again)
- Wipe the object's existence from the computer's memory (you can't do
that with this program, but you can do it when you start writing Java
programs)
- Create a FilledOval object
- Make sure you tell the program that the type of object you want to
construct is a FilledOval
- Fill in blanks to create an oval that is really a circle (the width
and height are the same) of radius 60, roughly in the center of the drawing window
- Click the Construct button to create the object
- Notice you can select either the FilledOval or the FramedRect in the
drawing window by clicking on either one. You have to select an object in
order to send it messages.
- Move the FilledOval 100 pixels to the left, by clicking on the
move(...) button, entering an appropriate x and y offset, and then
clicking OK
- Figure out on your own how to change the FilledOval's color to
orange.
- Find out what the FilledOval's new location is by sending the
getLocation() message. Look for the result in the output area.
Draw a picture
Quit the NotPhotoshop program and start it again, so you can start with a
clean slate.
Now use your skills and ingenuity (and help, if you would like some) to draw the
following picture:
When you are done, send messages to the various objects to determine their
exact positions and sizes. We do not care what the exact values are, as long
as the picture looks about right.
Write this information down, because you will need
it for the next part of the lab.
Start the Java environment
In this part, you will draw your "No clicking" sign by giving Java
instructions to the computer, rather than clicking buttons.
First you must start up the "Java compiler"; the computer program that
understands instructions in the Java language.
- Locate the DrJava program in the Course Materials folder. Drag
it to the desktop to make a local copy.
- Click the DrJava icon. This should pop up a window that
represents that Java compiling program
- For now, we are only going to pay attention to the area at the
bottom that is titled "Interactions". This area should have a "Welcome to
DrJava" message in it
- In this "Interactions" area, you can type Java commands and watch them
execute. Before you can start creating objects, though, you have to type in a
couple of special commands.
- First you must click your mouse in the interactions area, so it
will accept things that you type
- Then type in:
import objectdraw.*;
(then press Enter). This allows the Java program to have access to the special graphics library we
are using to draw pictures.
- Next type in:
canvas = new FrameCanvas();
(then press Enter). This should cause a drawing window to appear on your
screen. This is just like the drawing portion of the NotPhotoshop program.
You will learn more later about what you just did.
- At this point, you should be able to start drawing pictures by typing
in Java commands
- You can use the constructor are of NotPhotoshop to give you clues
about how you have to type the commands. Remember that computers are picky
when you program them...if you make a typographical error, the computer will
not do what you want.
- As an example, if you want to create a FramedRect at x=100,
y=75, with width=50 and height=80, you could type in the command:
new FramedRect(100,75,50,80,canvas);
- Use this example to figure out which Java instructions you need to
type to create your "no clicking" sign
- Notice that we have not yet told you how to set the colors of the
objects in Java. You will learn that next week. For now, just
draw all the objects in black, and do not worry about the colors.
- Once you have figured out which commands you need to create the
sign, and you have successfully drawn the sign in the window, write the
commands down. You will use these for our first real lab tomorrow.
- Get my attention and show me what you have created.
What you would hand in if this were to be a graded lab.
- A written sheet of the Java instructions you came up with for
drawing the "no clicking" sign
- A short (1 or 2 pages) writeup describing what you learned from
this project, any problems you ran into, and how you overcame them.
- You do not need to turn in anything electronic for this assignment
Grading (again, standards that would be used if this were graded)
- 10 points will be divided as follows:
- 4 points for your Java "program"
- 5 points for your report
- 1 point to reward especially impressive
efforts on this project
- You will be graded on the quality of writing in your report,
as well as the quality of the insights, information, and
analysis you provide. Bland reports that shallowly summarize
what you did are boring and don't often receive full points.