Java Reference
In-Depth Information
@Override
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
}
Directory "CurveApplet 3 moving the control points"
Of course, to make the effect of moving the control points apparent, you must update the curve objects
before you draw them. You can add the following code to the paint() method to do this:
@Override
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...
Directory "CurveApplet 3 moving the control points"
You can update the data members that store the control point coordinates for the curves directly because
they are public members of each curve class. You get the coordinates of the new positions for the control
points from their markers by calling the getCenter() method for each Marker object. You can then use
the appropriate data member of the Point2D.Double object that is returned to update the fields for the
curve objects.
If you recompile the applet with these changes and run it again you should get something like the window
shown in Figure 19-15 .
 
Search WWH ::




Custom Search