Java Reference
In-Depth Information
TRY IT OUT: Rotating Elements
You need to record the original rotation angle for an element and the new angle through which it is rotated
by mouse dragged events in the MouseHandler class. Add the following members to the class:
private double oldAngle = 0.0;
// Initial
element rotation
private double angle = 0.0;
// Additional
rotation
The mousePressed() method sets values for these when the mode is ROTATE :
public void mousePressed(MouseEvent e) {
start = e.getPoint(); // Save the cursor
position in start
buttonState = e.getButton(); // Record which
button was pressed
if(showContextMenu(e)) {
start = null;
buttonState = MouseEvent.NOBUTTON;
return;
}
if(theApp.getWindow().getElementType() == TEXT && mode ==
NORMAL) return;
// Initialize rotation angles when mode is ROTATE
if(mode == ROTATE && selectedElement != null) {
oldAngle = selectedElement.getRotation();
angle = 0.0;
}
if(buttonState == MouseEvent.BUTTON1 && mode == NORMAL) {
g2D = (Graphics2D)getGraphics(); // Get graphics
context
g2D.setXORMode(getBackground()); // Set XOR mode
}
}
Directory "Sketcher 10 moving and rotating elements"
The check for the element type being TEXT now has an additional condition for a return from the method;
it must be NORMAL mode, too.
When the mode is ROTATE and there is a selected element, the current element rotation angle is recorded
in the oldAngle member of the MouseHandler object. The angle member that records any additional
rotation is set to zero.
Search WWH ::




Custom Search