CS10, Fall 2009, lab06

Instructor: Phill Conrad
TAs: Esra Kucukoguz, Yiming Li, Murali Yeleswarapu

Primary TA for this lab: Esra Kucukoguz

(The lab06 preview is still available, but you no longer need to look at it—this file has everything you need.)

What you'll be doing in this lab:

Step 1: Converting hard coded drawings into classes

Start by working with your pair on one of your two lab03 submissions. Open up this submission in BlueJ, or at the command line—however you prefer to work.

Create a ~/cs10/lab06 directory, and save your lab03 work as lab06Step1Project in this new dircectory.

Now, make a a specific class that implements the "Shape" interface, and represents your drawing.

For example, if you drew an skateboard, you want to make a class:

public class Skateboard implements Shape
{

private GeneralPath skateboard;

public Skateboard(double x, double y, double width, double height)
{ etc ...
}

etc ...

}

This is instead of having code in your DrawSkateboardComponent that has hard coded calls to Rectangle, Ellipse, Line2D, GeneralPath, etc. that draw the skateboard.

If you used the GeneralPath technique to make your drawing, you can see an example online here of how to convert hard-coded drawings into a class that implements Shape

What to look for in this code:

The main idea is to draw the points the same way as you did before, but include a few key lines of code:

You'll need to copy the ShapeTransforms class into your project so that you can scale, translate, and rotate the shapes you draw—this comes from the CoffeeCup example mentioned above.

In your new class, you also need to implement all the methods that implement Shape—this is easy. You can see that the underlying GeneralPath object already has all these methods, so we just provide a wrapper around those, as we went over in Lecture on Wednesday May 13. You'll only need to replace the variable cup with whatever GeneralPath instance variable you use to represent your object (e.g. skateboard, ipod, beachScene, etc.)

 /**
 Tests if the specified coordinates are inside the boundary of the
 Shape.
 */
 
 public boolean contains(double x, double y)  { return cup.contains(x,y); }
 
 /**       Tests if the interior of the Shape entirely contains the
 *       specified rectangular area.
 */
 public boolean contains(double x, double y, double w, double h) { return cup.contains(x,y,w,h); }

etc...

Work with your pair first to try to understand the code in the CoffeeCup class. You may need to consult the Javadocs for the methods you see there—be sure you know what kind of object the method is being invoked on (e.g. append() is being invoked on cup, which is an example of a GeneralPath object, so look up the javadoc for GeneralPath to know how append() works.)

If you used things other than GeneralPath in your drawing, you can still use this technique. You can use the append method of General Path to simply append anything that implements the Shape interface directly to a GeneralPath object. This means you can use the code above, and just use append() on your Rectantgles, Circles, Ellipses, Line2D.Doubles, etc. just like I used it on GeneralPath objects. Here are some examples:

What if I used fills?

(Note: If you didn't use fills, you can skip down to the part that says: "What you should have when you are finished with Step 1")

During the 4PM lab on Thursday, I became aware that some of you had used the "fill" method of the Graphics2D class to create some great drawings with filled in color, such as these Tanks created by your classmate Moises:

Moises Tanks

Drawing done with fills require a different approach to combine the pieces together. That approach is illustrated in the following BlueJ Project:

The key to this method is to look at the examples inside the following files.

First, look at a very simple example. This example draws an Italian flag using fills, like this one:

When you have looked at this files, look at the ones that draw the Tank images:

To adapt the code in MoisesTank.java for your own drawings with fills:

What you should have when you are finished with Step 1:

When you've done that, you are ready for Step 2:

Step 2: Understanding how to create image files (.png, .jpg, .gif)

Now, copy this project to your directory:

You can copy this file to your directory with the usual "cp -r" command.

Read through this code together with your pair.

Then, compile the code (either in BlueJ, or at the command line, using "javac *.java".

Then, run the main program WritePictureToFile, but run it AT THE COMMAND LINE with the command:

 java WritePictureToFile cups

You should see a file appear in your directory called cups.png. You can use the web browser to open this file (use the File / Open File command and navigate to this file). You should see that you now have an image file that contains a drawing.

Whatever you supply on the command line is what the file will be called—for example:

 java WritePictureToFile fred

creates the file fred.png.

Try to understand how this code works before moving on to the next step.

Also note one other thing: there is a new class in this example called PhillsPictures. Here, we've taken the code to draw the pictures out of the Component class and moved it into its own class—we've "factored it out" so that we can call it from the DrawLotsOfStuffComponent class, or the WritePictureToFile class.

This process of moving code around—factorig things out so that they can be reused—is called refactoring, and is an important skill in software development.

Step 3: Putting it all together

Now, save a copy of GraphicsToFileExample.project as lab06Project in your lab06 directory. Add the classes for your four drawing objects into the lab06Project.

In this new project, each drawing object should now have its own class that either:

(This is exactly the same as where you were supposed to be at the end of Step 1.)

Some notes:

Once you've combined your objects into a single project, make a class similar to either:

That is, write a class with methods that illustrates how you can put examples of all four of your drawings in a single drawing, together, at different locations, and sizes. You may like to make them different colors too, following the example from PhillsPictures, and/or use for loops to make lots of different objects in lines or other patterns.

When you are done, create a JComponent and Viewer class (with a main method that creates a JFrame) to view your drawing, and also modify the WritePictureToFile class so that it draws your picture.

Be sure your picture has a text label in it (like the examples) with the names of you and your pair partner, and a description of your drawing.

(Note: earlier in class, I mentioned you'd have to come up with a fifth item to draw—that is no longer a requirement, though you are free to do so if you want. Draw as much as you like—it's your drawing! It just has to have at least four different types of items.)

Step 4: Putting your picture on the web

Now, its time to show off.

Run your main program—the one that make the .png file—and create a file like ourDrawing.png.

Now, use the "cd" command (by itself) to get back to your home directory, and then create a cs10 directory under public_html—this is a directory where you can put your drawing on teh web:

cd 
mkdir -p public_html/cs10

Copy your .png file into that directory, for example:

cp cs10/lab06/lab06Project/ourDrawing.png public_html/cs10

Then use the following command to make that file readable on the web:

chmod -R a+rx ~/public_html

You should then be able to see your drawing at the URL:

www.cs.ucsb.edu/~yourusername/cs10/ourDrawing.png

When that is working, make a posting on the Gauchospace Forum called "Drawings from lab06", that has your name, and that of your pair partner in the subject line, and the URL of your drawing in the body of the post. You can make it a clickable link using the little link icon (the picture of a chain link).

With that, you are finished with lab06—except for the turnin step.

Turning in lab06

Use the turnin command on just the directory containing the code you are submitting (if you copied other projects into your lab06 directory for practice, don't submit those.)

Grading Rubric:

Grading: This lab is worth Total points: ?, distributed as follows. Partial credit may be awarded for each step at the discretion of the TA/Instructor.

Deadline:

Lab06 will be due by 5pm on Monday June 1st (Note—this is after lab07, so get that one done first.)


End of lab06