CS 117: Using Classes Written by Others


The objectives of today's lab:

  1. Do some graphics programming
  2. Write programs that utilize pre-existing Java classes
  3. Use arithmetic to adjust graphics images
  4. Have fun!

Please work with your assigned partner (if you have one) for this lab. Remember that all work is to be done with both teammates working together at the computer. If you find that you absolutely can't schedule time to work together, split up for this assignment and turn in your own work. As always, feel free to ask lots of questions!

Background

One of the neat things about programming in Java is that it contains a large number of classes that can be used to program graphics. It turns out, though, that the details of doing so can be be somewhat tedious in Java. This is especially true for simple programs, such as drawing pictures in a window. Luckily, someone else recognized that problem as well and wrote a class to handle simple drawing tasks. We'll be using this class in today's lab.

The Canvas class

In your home directory, create a directory called lab3 to hold your work for this assignment.

In your terminal window, cd to the directory /Accounts/courses/cs117/dmusican/. There, you will find a file named Canvas.java. Copy this file into the lab3 directory that you just created..

You can open up the file and look at it if you'd like. However, we won't be paying much attention for a while to what's in this class. Instead, we're going to use this class in our own programs.

Now copy the file MyArtwork.java from the Accounts/courses/cs117/dmusican/ directory and open it up. Read through the code and see if you can figure out what it will do.

To compile your program, you need to compile both Java programs. Type:

javac *.java

in the terminal window. The * is a "wildcard." In other words, *.java refers to all files that end in .java.

Finally, assuming that you don't get any errors (call for help if you do), run your program. Type:

java MyArtwork

What do you see? Try modifying the program to accomplish the following:

The documentation for the Canvas class is at this web page. 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. Take a few minutes to skim the 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. Modify your program to use other methods from this documentation page.

A Bit of Arithmetic

We're going to shift away from graphics for a short while, but don't worry, we'll be back!

Copy the program Arithmetic.java from the /Accounts/courses/cs117/dmusican/. Look through the program, to get a sense for what it might do, and run it. Answer the following questions in a file called lab3.txt.

Question #1:What does the operation % do? How about /? (If you're having trouble guessing what % does, try these examples: 50 % 5, 51 % 5, 52 % 5, 53 % 5, 54 % 5, 55 % 5, 56 % 5. Try other cases with a similar pattern and see if that helps.)

Now change the types of a and b from int to double. Recompile and rerun.

Question #2:How does the behavior of / change?

Question #3:The type double is badly named. What does it mean in this context?

Back to Graphics

Write a Java application named MyDrawing.java. 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:

Be creative, and have fun!

What to turn in

Turn in your entire directory with hsp your answers to the questions as well as your graphics demos. Creativity is highly valued! Again, only one member of a team needs to submit the work, but make sure that both team members names are in comments in the program.


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