Java Reference
In-Depth Information
circle.y = y-radius; // correspondingly
}
After updating the coordinates of the point, center , we also update the position of circle by setting
its data member directly. We can do this because x and y are public members of the
Ellipse2D.Double class.
We can create a MouseHandler object in the init() method for the applet and set it as the listener
for mouse events for the pane object:
public void init() {
pane = new CurvePane(); // Create pane containing curves
Container content = getContentPane(); // Get the content pane
// Add the pane displaying the curves to the content pane for the applet
content.add(pane); // BorderLayout.CENTER is default position
MouseHandler handler = new MouseHandler(); // Create the listener
pane.addMouseListener(handler); // Monitor mouse button presses
pane.addMouseMotionListener(handler); // as well as movement
}
Of course, to make the effect of moving the control points apparent, we must update the curve objects
before we draw them. We can add the following code to the paint() method to do this:
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D)g; // Get a 2D device context
// Update the curves with the current control point positions
quadCurve.ctrlx = ctrlQuad.getCenter().x;
quadCurve.ctrly = ctrlQuad.getCenter().y;
cubicCurve.ctrlx1 = ctrlCubic1.getCenter().x;
cubicCurve.ctrly1 = ctrlCubic1.getCenter().y;
cubicCurve.ctrlx2 = ctrlCubic2.getCenter().x;
cubicCurve.ctrly2 = ctrlCubic2.getCenter().y;
// Rest of the method as before...
We can update the data members that store the control point coordinates for the curves directly because
they are public members of each curve class. We get the coordinates of the new positions for the control
points from their markers by calling the getCenter() method for each, and then accessing the
appropriate data member of the Point2D.Double object that is returned.
Search WWH ::




Custom Search