📄 curve.java
字号:
//**************************************************************************
//* Curve Applet by Michael Heinrichs
//*
//* This applet allows the user to specify control points to be used for
//* plotting curves. Three curve types are supported: Hermite, Bezier,
//* and B-Spline. The user can add, delete and move control points.
//*
//* This applet was inspired by the DrawTest demo applet included in the
//* 1.0 beta Java Developer's Kit from Sun Microsystems.
//* If you are interested in the algorithms used to display the curves,
//* Please see the book: "Computer Graphics. Principles and Practice" by
//* Foley, VanDam, Feiner, and Hughes.
//**************************************************************************
import java.awt.*;
import java.applet.*;
import java.util.Vector;
public class Curve extends Applet {
public void init() {
setLayout(new BorderLayout());
CurvePanel dp = new CurvePanel();
add("Center", dp);
add("South",new CurveControls(dp));
add("North",new CurveControls2(dp));
}
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.WINDOW_DESTROY:
System.exit(0);
return true;
default:
return false;
}
}
public static void main(String args[]) {
Frame f = new Frame("Curve");
Curve curve = new Curve();
curve.init();
curve.start();
f.add("Center", curve);
f.show();
}
}
class ControlPoint extends Object {
public int x;
public int y;
public static final int PT_SIZE = 4;
public ControlPoint(int a, int b) {
x = a;
y = b;
}
public boolean within(int a, int b) {
if (a >= x - PT_SIZE &&
b >= y - PT_SIZE &&
a <= x + PT_SIZE &&
b <= y + PT_SIZE)
return true;
else
return false;
}
}
class CurvePanel extends Panel {
public static final int HERMITE = 0;
public static final int BEZIER = 1;
public static final int BSPLINE = 2;
private int mode = HERMITE;
public static final int ADD = 0;
public static final int MOVE = 1;
public static final int DELETE = 2;
private int action = ADD;
private Vector points = new Vector(16,4);
// If a control point is being moved, this is the index into the list
// of the moving point. Otherwise it contains -1
private int moving_point;
private int precision;
private static float hermiteMatrix[][] = new float[4][4];
private static float bezierMatrix[][] = new float[4][4];
private static float bsplineMatrix[][] = new float[4][4];
private float eMatrix[][] = new float[4][4];
// Initialize the curve-type matrices
static {
hermiteMatrix[0][0] = 2;
hermiteMatrix[0][1] = -2;
hermiteMatrix[0][2] = 1;
hermiteMatrix[0][3] = 1;
hermiteMatrix[1][0] = -3;
hermiteMatrix[1][1] = 3;
hermiteMatrix[1][2] = -2;
hermiteMatrix[1][3] = -1;
hermiteMatrix[2][0] = 0;
hermiteMatrix[2][1] = 0;
hermiteMatrix[2][2] = 1;
hermiteMatrix[2][3] = 0;
hermiteMatrix[3][0] = 1;
hermiteMatrix[3][1] = 0;
hermiteMatrix[3][2] = 0;
hermiteMatrix[3][3] = 0;
bezierMatrix[0][0] = -1;
bezierMatrix[0][1] = 3;
bezierMatrix[0][2] = -3;
bezierMatrix[0][3] = 1;
bezierMatrix[1][0] = 3;
bezierMatrix[1][1] = -6;
bezierMatrix[1][2] = 3;
bezierMatrix[1][3] = 0;
bezierMatrix[2][0] = -3;
bezierMatrix[2][1] = 3;
bezierMatrix[2][2] = 0;
bezierMatrix[2][3] = 0;
bezierMatrix[3][0] = 1;
bezierMatrix[3][1] = 0;
bezierMatrix[3][2] = 0;
bezierMatrix[3][3] = 0;
float mult = (float)(1.0/6.0);
bsplineMatrix[0][0] = -mult;
bsplineMatrix[0][1] = 3 * mult;
bsplineMatrix[0][2] = -3 * mult;
bsplineMatrix[0][3] = mult;
bsplineMatrix[1][0] = 3 * mult;
bsplineMatrix[1][1] = -6 * mult;
bsplineMatrix[1][2] = 3 * mult;
bsplineMatrix[1][3] = 0;
bsplineMatrix[2][0] = -3 * mult;
bsplineMatrix[2][1] = 0;
bsplineMatrix[2][2] = 3 * mult;
bsplineMatrix[2][3] = 0;
bsplineMatrix[3][0] = mult;
bsplineMatrix[3][1] = 4 * mult;
bsplineMatrix[3][2] = mult;
bsplineMatrix[3][3] = 0;
}
public CurvePanel() {
setBackground(Color.white);
}
private void calcEMatrix(int prec) {
// In order to use the "forward difference" method of curve plotting,
// we must generate this matrix. The parameter indicates the precision;
// the number of line segments to use for each curve.
float step = (float) (1.0/(float)prec);
eMatrix[0][0] = 0;
eMatrix[0][1] = 0;
eMatrix[0][2] = 0;
eMatrix[0][3] = 1;
eMatrix[1][2] = step;
eMatrix[1][1] = eMatrix[1][2] * step;
eMatrix[1][0] = eMatrix[1][1] * step;
eMatrix[1][3] = 0;
eMatrix[2][0] = 6 * eMatrix[1][0];
eMatrix[2][1] = 2 * eMatrix[1][1];
eMatrix[2][2] = 0;
eMatrix[2][3] = 0;
eMatrix[3][0] = eMatrix[2][0];
eMatrix[3][1] = 0;
eMatrix[3][2] = 0;
eMatrix[3][3] = 0;
}
public void setAction(int action) {
// Change the action type
switch (action) {
case ADD:
case MOVE:
case DELETE:
this.action = action;
break;
default:
throw new IllegalArgumentException();
}
}
public void setCurveType(int mode) {
// Change the curve display type
switch (mode) {
case HERMITE:
case BEZIER:
case BSPLINE:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}
public void setPrecision(int prec) {
precision = prec;
calcEMatrix(prec);
}
public void clearPoints() {
points.removeAllElements();
}
private int findPoint(int a, int b) {
// Scan the list of control points to find out which (if any) point
// contains the coordinates: a,b.
// If a point is found, return the point's index, otherwise return -1
int max = points.size();
for(int i = 0; i < max; i++) {
ControlPoint pnt = (ControlPoint)points.elementAt(i);
if (pnt.within(a,b)) {
return i;
}
}
return -1;
}
public boolean handleEvent(Event e) {
switch (e.id) {
case Event.MOUSE_DOWN:
// How we handle a MOUSE_DOWN depends on the action mode
switch (action) {
case ADD:
// Add a new control point at the specified location
ControlPoint pnt;
points.addElement(pnt = new ControlPoint(e.x, e.y));
repaint();
break;
case MOVE:
// Attempt to select the point at the location specified.
// If there is no point at the location, findPoint returns
// -1 (i.e. there is no point to be moved)
moving_point = findPoint(e.x, e.y);
break;
case DELETE:
// Delete a point if one has been clicked
int delete_pt = findPoint(e.x, e.y);
if(delete_pt >= 0) {
points.removeElementAt(delete_pt);
repaint();
}
break;
default:
throw new IllegalArgumentException();
}
return true;
case Event.MOUSE_UP:
// We only care about MOUSE_UP's if we've been moving a control
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -