Java Reference
In-Depth Information
// Rotate an element
public void setRotation(double angle) {
this.angle = angle;
}
// Get the rotation angle
public double getRotation() {
return angle;
}
Directory "Sketcher 10 moving and rotating elements"
The methods are inherited in all the derived element classes.
For the geometric elements, you can arrange to draw them rotated through angle radians about position
by modifying the draw() method that is implemented in the Element class:
protected void draw(Graphics2D g2D, Shape element) {
g2D.setPaint(highlighted ? Color.MAGENTA : color); // Set the element color
AffineTransform old = g2D.getTransform(); // Save copy of current transform
// Add a translation to current transform
g2D.translate((double)position.x, (double)position.y);
g2D.rotate(angle); // Rotate about position
g2D.draw(element); // Draw the element
g2D.setTransform(old); // Restore original transform
}
Directory "Sketcher 10 moving and rotating elements"
Just one line of code does it. The additional statement adds a rotation through angle radians to the current
transform in the graphics context. Thus the transform performs a translation of the origin to the point posi-
tion , followed by a rotation about the origin through angle radians before the element is drawn.
Element.Text objects are a special case, because they take care of drawing themselves. Updating the
draw() method for Text elements is not a great challenge either:
public void draw(Graphics2D g2D) {
g2D.setPaint(highlighted ? HIGHLIGHT_COLOR : color);
Font oldFont = g2D.getFont(); // Save the old font
g2D.setFont(font); // Set the new font
Search WWH ::




Custom Search