📄 curveapplet.java
字号:
g2D.draw(tangent);
}
}
}
}
// Inner class defining a point marker.
class Marker {
public Marker(Point2D.Double point, Color color) {
center = point; // Save point as circle center
this.color = color;
// Create circle around control point
circle = new Ellipse2D.Double(center.x-radius, center.y-radius,
2.0*radius, 2.0*radius);
}
// Draw the marker
public void draw(Graphics2D g2D) {
g2D.setColor(color);
g2D.draw(circle);
}
// Get center of marker - the control point position
Point2D.Double getCenter() {
return center;
}
// Test if a point x,y is inside the marker
public boolean contains(double x, double y) {
return circle.contains(x,y);
}
// 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
}
Ellipse2D.Double circle; // Circle around control point
Point2D.Double center; // Circle center - the control point
Color color; // Color of this marker, controls red, curve pts blue
static final double radius = 3; // Radius of circle
}
class MouseHandler extends MouseInputAdapter {
// In this method we are defining a new curve point when curveComplete is false
// or potentially selecting a control or defining point when curveComplete is true.
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
if(curveComplete) {
// Check if the cursor is inside any marker
Marker marker = null;
// First check the point markers
for(int i = 0 ; i<pointMarkers.size() ;i++) {
marker = (Marker)(pointMarkers.elementAt(i));
if(marker.contains(p.x,p.y)) {
selected = marker;
selectedIndex = i;
return;
}
}
// It was not a point marker so check the control markers
for(int i = 0 ; i<controlMarkers.length ;i++) {
if(controlMarkers[i].contains(p.x,p.y)) {
selected = controlMarkers[i];
selectedIndex = i;
return;
}
}
} else {
// We are adding a new defining point
pointMarkers.add(new Marker(new Point2D.Double(p.x,p.y), Color.BLUE));
if(pointMarkers.size()>3)
createCurves();
pane.repaint();
}
}
public void mouseReleased(MouseEvent e) {
selected = null; // Deselect any selected marker
}
// If selected is not null, this method moves the selected marker
// and modifies the curve segments that are affected.
public void mouseDragged(MouseEvent e) {
if(selected != null) { // If a marker is selected
// Set the marker to current cursor position
selected.setLocation(e.getX(), e.getY());
if(selected == controlMarkers[selectedIndex]){
// It must be a control marker so update it in the curve
if(selectedIndex%2 == 0) {
// It is the 1st control point that was changed
curveSegments[selectedIndex/2].setCurve(curveSegments[selectedIndex/2].getP1(),
selected.getCenter(),
curveSegments[selectedIndex/2].getCtrlP2(),
curveSegments[selectedIndex/2].getP2());
/* For control points interior to the set of curve segments, we must adjust
the control marker the other side of curve point. This will be the
second marker for the preceding curve segment.
There are two control markers for each point apart from the first and the
last, so ifselectedIndex than the index for the first and less than the
index for the last, we have another control marker and curve segment to
adjust.
*/
if(selectedIndex>0 && selectedIndex<controlMarkers.length-1) {
Point2D.Double point = (Point2D.Double)(curveSegments[selectedIndex/2-1].getP2());
double x = point.x + (point.x - selected.getCenter().x);
double y = point.y + (point.y - selected.getCenter().y);
controlMarkers[selectedIndex-1].setLocation(x, y);
curveSegments[selectedIndex/2-1].setCurve(
curveSegments[selectedIndex/2-1].getP1(),
curveSegments[selectedIndex/2-1].getCtrlP1(),
controlMarkers[selectedIndex-1].getCenter(),
curveSegments[selectedIndex/2-1].getP2());
}
} else {
// It is the 2nd control point that was changed
curveSegments[selectedIndex/2].setCurve(curveSegments[selectedIndex/2].getP1(),
curveSegments[selectedIndex/2].getCtrlP1(),
selected.getCenter(),
curveSegments[selectedIndex/2].getP2());
// For interior control markers we must adjust the next one analogous
// to the previous case
if(selectedIndex>0 && selectedIndex<controlMarkers.length-1) {
Point2D.Double point = (Point2D.Double)(curveSegments[selectedIndex/2+1].getP1());
double x = point.x + (point.x - selected.getCenter().x);
double y = point.y + (point.y - selected.getCenter().y);
controlMarkers[selectedIndex+1].setLocation(x, y);
curveSegments[selectedIndex/2+1].setCurve(
curveSegments[selectedIndex/2+1].getP1(),
controlMarkers[selectedIndex+1].getCenter(),
curveSegments[selectedIndex/2+1].getCtrlP2(),
curveSegments[selectedIndex/2+1].getP2());
}
}
} else {
// It must be a point marker. If the index is other than the first or
// the last, two curve segments have to be updated
double xDelta = 0.0;
double yDelta = 0.0;
Point2D point = null;
if(selectedIndex>0){
// Change the segment where this is the second point
point = curveSegments[selectedIndex-1].getP2();
xDelta = selected.getCenter().getX() - point.getX();
yDelta = selected.getCenter().getY() - point.getY();
point = curveSegments[selectedIndex-1].getCtrlP2();
controlMarkers[2*selectedIndex-1].setLocation(point.getX()+xDelta,
point.getY()+yDelta);
curveSegments[selectedIndex-1].setCurve(curveSegments[selectedIndex-1].getP1(),
curveSegments[selectedIndex-1].getCtrlP1(),
controlMarkers[2*selectedIndex-1].getCenter(),
selected.getCenter());
}
if(selectedIndex<curveSegments.length) {
// Change the segment where this is the first point
point = curveSegments[selectedIndex].getP1();
xDelta = selected.getCenter().getX() - point.getX();
yDelta = selected.getCenter().getY() - point.getY();
point = curveSegments[selectedIndex].getCtrlP1();
controlMarkers[2*selectedIndex].setLocation(point.getX()+xDelta, point.getY()+yDelta);
curveSegments[selectedIndex].setCurve(selected.getCenter(),
controlMarkers[2*selectedIndex].getCenter(),
curveSegments[selectedIndex].getCtrlP2(),
curveSegments[selectedIndex].getP2());
}
}
pane.repaint(); // Redraw pane contents
}
}
Marker selected = null; // Stores reference to selected marker
int selectedIndex = 0; // Index position of selected Marker
}
Vector pointMarkers = new Vector(); // Vector of Marker objects
CubicCurve2D.Double[] curveSegments = null; // Array of cubic curve segments
Point2D.Double[] controlVec = null; // Tngent vectors for control points
Marker[] controlMarkers = null; // Array of control point markers
CurvePane pane = new CurvePane(); // Pane to contain curves
boolean curveComplete = false; // Indicator for complete curve
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -