Java Reference
In-Depth Information
public void move(int deltax, int deltay) {
position.x += deltax;
position.y += deltay;
}
public void rotate(double angle) {
this.angle += angle; }
public abstract Rectangle getBounds();
public abstract void draw(Graphics2D g2D);
public abstract void modify(Point start, Point last);
protected Color color; // Color of a shape
protected boolean highlighted = false; // Highlight flag
final static Point origin = new Point(); // Point 0,0
protected Point position; // Element position
protected double angle = 0.0; // Rotation angle
// inner shape classes declared here
}
All the rotate() method does is add the angle passed to it to the current value of angle . The value of
angle is assumed to be in radians. Naturally, when we create an element the angle will be zero. We have
modified the draw() method in the base class to apply a rotation through angle radians about the point
position . It is important that we apply the rotation after the translation, otherwise the translation would be
applied in the rotated coordinate system, which would give quite a different result from what we require.
Since we now have the possibility of rotated shapes, the getBounds() method also has to take account of
this, so we apply a rotation here, too. We mustn't forget that the draw() method in the Element.Text
class is a special case. We need to add a line to this method to apply the rotation:
public void draw(Graphics2D g2D) {
g2D.setPaint(highlighted ? Color.MAGENTA : color);
Font oldFont = g2D.getFont(); // Save the old font
g2D.setFont(font); // Set the new font
AffineTransform old = g2D.getTransform(); // Save the current
transform
g2D.translate(position.x, position.y); // Translate to position
g2D.rotate(angle); // Rotate about position
g2D.drawString(text, origin.x, origin.y+(int)bounds.getHeight());
g2D.setTransform(old); // Restore original
transform
g2D.setFont(oldFont); // Restore the old font
}
Recompile all the stuff we have changed and try out the new context menus. Having a rotate capability
adds flexibility and with the move operation giving you much more precision in positioning elements
relative to one another, this should enable a massive leap forward in the quality of your artwork.
Search WWH ::




Custom Search