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!
The Canvas class
In your home directory, create a directory called graphicsIntro
to hold your work for this assignment. Change to that directory in the
terminal window.
Copy the file /Accounts/courses/cs117/dmusican/Canvas.java
(also available here as Canvas.java)
to the graphicsLab 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
/Accounts/courses/cs117/dmusican/MyArtwork.java (also
available here as MyArtwork.java)
to your 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:
- Make the graphics window tall and skinny. What happens to the
square when you run your new version? Why?
- Change the background color. Can you make it white?
- Make the square green.
The documentation for the Canvas class is at this
web page (a link is also now on the Moodle home 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. (Note that the polygon methods require ideas we haven't covered
yet, so you might want to stay away from them unless you feel
particularly daring.)
Your Own Design
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 that you learned in the last
assignment as part of your program.
Some suggestions:
- Use arithmetic to create a "scalable" shape. For example, you
might ask the user to enter how long each edge of a square should be,
and the program should draw a square accordingly.
- Combine simple shapes in more complex ways. For example, combine triangles to form a star, or circles to generate different patterns, or rectangles to create a tic-tac-toe board.
- Draw a picture, and then use the
wait() and erase() methods to erase part of the picture. (If you're really feeling ambitious, you might want to figure out how you could use these methods to make it appear as if something is moving across your canvas.)
Be creative, and have fun!
What to turn in
Turn in your entire directory with hsp. 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.
Written by Amy Csizmar Dalal, Dave Musicant, and Jeff Ondich.
|