Instructor: Phill Conrad
TAs: Esra Kucukoguz, Yiming Li, Murali Yeleswarapu
Primary TA for this lab: Esra Kucukoguz
First Posted 2pm Thursday 05/14, Updated: 10:40am Friday 05/15
For those of you that finish with lab05 on 05/14 or 05/15, you are encouraged to get a head start on lab06. The instructions below will allow you to do that.
What you'll be doing in lab06:
Start by working with your pair on one of your two lab03 submissions. What you'll be doing is trying to come up with a specific class that implements the "Shape" interface that 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:
public CoffeeCup(double x, double y, double width, double height)// Specify the upper left corner, and the
// width and height of the original points used to p
// plot the coffee cup
final double ORIG_ULX = 100.0;
final double ORIG_ULY = 100.0;
final double ORIG_HEIGHT = 300.0;
final double ORIG_WIDTH = 400.0; // now we put the whole thing together ino a single path.
cup = new GeneralPath ();
cup.append(topAndBottom, false);
cup.append(leftSide, false);
cup.append(rightSide, false);
Shape s = ShapeTransforms.translatedCopyOf(cup, -ORIG_ULX + x, -ORIG_ULY + y);
// scale to correct height and width
s = ShapeTransforms.scaledCopyOf(s,
width/ORIG_WIDTH,
height/ORIG_HEIGHT);
cup = new GeneralPath(s); // convert the scaled cup back to a GeneralPath
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 you can do to get a head start on lab06: See if you and your pair can put together a project that combines classes for all four of your drawing objects, where each drawing object is its own class that implements Shape, and has a constructor that takes(double x,double y,double width,double height)—or some other appropriate parameters that allow it to be drawn anywhere on the screen at any size.
this(centerBottomX - height/4.0, /* upper left x */
centerBottomY - height, /* upper left y */
height/2.0, /* width */
height); /* height */
Once you've combined your objects into a single projects, make a CombinedDrawingComponent that illustrates how you can put examples of all four of your drawings in a single JFrame, together, at different locations, and sizes. You may like to make them different colors too, following the example from AbstractedSnowmenHousesAndCoffeeCupsComponent.java, and/or use for loops to make lots of different objects in lines or other patterns.
By the time you've completed that, you'll be most of the way through lab06! The due date for lab06 will be very shortly after the due date for lab05, so the sooner you get started the better.
More detailed instructions will come later.
End of lab06 preview