Java Reference
In-Depth Information
You will also need to draw the gradient line in its new orientation:
g2D.draw(rect1); // Fill the rectangle
//g2D.draw(new Line2D.Float(p1, p2));
g2D.draw(new Line2D.Float(p1.x, p1.y - 20, p2.x, p2.y + 20));
The annotation for the end points will also have to be moved:
g2D.drawString("p1",p1.x - 20,p1.y - 20);
g2D.drawString("p2",p2.x + 10,p2.y + 20);
If you run the applet with these changes, you can see
how the gradient is tilted, and how the colors of a
point off the gradient line matches that of the point
that is the orthogonal projection onto it.
Managing Shapes
When we create shapes in Sketcher, we'll have no idea of the sequence of shape types that will occur.
This is determined totally by the person using the program to produce a sketch. We'll therefore need to
be able to draw shapes and perform other operations on them without knowing what they are - and
polymorphism can help here.
We don't want to use the shape classes defined in java.awt.geom directly as we will want to add our
own attributes such as color or line style, and store them as part of the object. We could consider using
the shape classes as base classes for our shapes, but we couldn't use the GeneralPath class in this
scheme of things because, as we have already seen, it's final and we might not want this restriction.
We could consider defining an interface that all our shape classes would implement. However, there
will be some methods that have a common implementation in all our shape classes so we would need to
repeat this code in every class.
Taking all of this into account, the easiest approach might be to define a common base class for our
shape classes, and include a member in each class to store a shape object of one kind or another. We'll
then be able to include a polymorphic method to return a reference to a shape as type Shape for use
with the draw() method of a Graphics2D object.
Search WWH ::




Custom Search