CS 117: Using existing classes
Winter 2005

In this lab, you will play with existing Java programs with the goal of learning a few things about Java: (1) how programs are structured, (2) how to draw simple pictures, and (3) how do do arithmetic with integers. To do the lab, open up a terminal window and a text editor (nedit, the yellow N on your taskbar at the bottom of the screen, will do nicely), and follow the instructions. Questions? Ask away.

Have fun.

Handing things in

Down at the bottom of this lab, I have asked you to hand in a program called Drawing.java. To do this, you can use the Homework Submission Program (HSP) to hand in just the Drawing.java file or the entire lab1 directory. For details on how HSP works, follow the link above. Note that HSP is really just a way to use the college-wide Collab system in a way that is convenient from the command line.

Before you hand anything in, make sure your source code includes both your name and your partner's name in a comment at the top of the file.

Drawing pictures using the Canvas class

You'll be needing the mkdir, cd, ls, and cp Unix commands. If you need help remembering how they work, please ask me.

In your terminal window, create a directory called lab1. Then cd to lab1. This is the directory in which you will do today's work.

Next, list the contents of the directory named /Accounts/courses/cs117/ondich/intro/. Copy all of the Java files you find there into your lab1 directory. One of these files is called Canvas.java. The Canvas class, written and modified by several people (take a peek inside it if you want to see who), was developed to simplify many aspects of simple two-dimensional graphics programming so that people new to Java could start drawing pictures right away. We will use Canvas a lot this term.

One of the other files you should now have in lab1 is called Art.java. Open this file in your editor and read through it. Try to predict what you will see when you run the program. For example, where do you think the string "Not quite Picasso" will appear, and where exactly will the purple square show up?

Before running the example program, you will need to compile both Canvas.java and Art.java. A simple command to compile all the .java files at once is:

javac *.java

The * in this command is known as a "wildcard." In this situation, it tells the terminal program to apply the javac command to all files whose names end in .java.

Finally, assuming that javac reports no errors (call for help if it does), run the program, like so:

java Art

Did you see what you expected? Next, try modifying the program to accomplish the following:

Now, take a look at the documentation for the Canvas class. Notice that the page has distinct sections: a general description of the class, a summary of fields and constructors (we'll talk about those later), and a more detailed description of all the methods available to you. Find the descriptions of setInkColor and drawRectangle. Do they seem consistent with the behavior you saw in Art.java? Take a few minutes to skim the other methods and to familiarize yourself with the documentation format. Then try picking out a few methods that look like they might be interesting, and try them out. See if you can draw a circle or a line or a triangle or some text. Have some fun, and ask questions.

A bit of arithmetic

Next, take a look at the Arithmetic.java file. Once again, read through the program and try to predict what it will do. Then compile it and run it.

Some questions to think about:

Back to graphics

Create a Java file named Drawing.java, and create a class called Drawing in it. (Probably the best way to do this is to copy and paste the entire contents of Art.java, and then do some editing). Your program should create a Canvas object and, using the methods in the Canvas class, draw a picture. Spend a little bit of time experimenting with the different methods, and then figure out how to combine these methods to draw more complex shapes or pictures. Use the arithmetic operations above as part of your program.

Some suggestions:

Hand in Drawing.java or your entire lab1 directory by midnight at the end of Monday, Jan 10, 2005.

Be creative, and have fun!


Authored by Amy Csizmar Dalal, Dave Musicant, and Jeff Ondich.