Java Reference
In-Depth Information
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;
Directory "CurveApplet 3 moving the control points"
The
mousePressed()
method calls a
contains()
method for a
Marker
that should test whether the point
defined by the arguments is inside the marker. You can implement this in the
Marker
class like this:
// Test if a point x,y is inside the marker
public boolean contains(double x, double y) {
return circle.contains(x,y);
}
Directory "CurveApplet 3 moving the control points"
This just calls the
contains()
method for the circle object that is the marker. This returns
true
if the
point (
x,y
) is inside the circle, and
false
if it isn't.
The
mouseDragged()
method calls a
setLocation()
method for the selected
Marker
object that is sup-
posed to move the marker to a new position, so you need to implement this in the
Marker
class, too:
// Sets a new control point location
public void setLocation(double x, double y) {
center.x = x;
// Update control point
center.y = y;
// coordinates
circle.x = x-radius;
// Change circle position
circle.y = y-radius;
// correspondingly
}
Directory "CurveApplet 3 moving the control points"
After updating the coordinates of the point
center
, you also update the position of the circle by setting
its data member directly. You can do this because
x
and
y
are public members of the
Ellipse2D.Double
class and store the coordinates of the center of the ellipse.
You 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: