📄 beziersurfpaneldx.java
字号:
package graphics0;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.awt.geom.Line2D;
import javax.swing.*;
public class bezierSurfPanelDX
extends JPanel {
BorderLayout borderLayout = new BorderLayout();
Point[][] ControlPoints = new Point[4][4];
int i, j, k, h, flag = 0;
Panel pdown;
Button process;
public bezierSurfPanelDX() {
this.setLayout(borderLayout);
pdown = new Panel();
process = new Button();
process.setLabel("GO");
pdown.setBackground(Color.pink);
this.add(pdown, BorderLayout.SOUTH);
pdown.add(process);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
ControlPoints[i][j] = new Point( (j + 1) * 80, (i + 1) * 80);
}
}
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
panel_mousePressed(e);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
panel_mouseDragged(e);
}
});
process.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
process_actionPerformed(e);
}
});
}
public void panel_mousePressed(MouseEvent e) {
flag = 0;
Graphics g = getGraphics();
int[] ax = new int[4];
int[] ay = new int[4];
int x0 = e.getX();
int y0 = e.getY();
Polygon a;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
ax[0] = ControlPoints[i][j].x - 10;
ax[1] = ControlPoints[i][j].x + 10;
ax[2] = ControlPoints[i][j].x + 10;
ax[3] = ControlPoints[i][j].x - 10;
ay[0] = ControlPoints[i][j].y - 10;
ay[1] = ControlPoints[i][j].y - 10;
ay[2] = ControlPoints[i][j].y + 10;
ay[3] = ControlPoints[i][j].y + 10;
a = new Polygon(ax, ay, 4);
if (a.contains(x0, y0)) {
k = i;
h = j;
flag = 1;
break;
}
}
}
}
public void panel_mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
if (flag == 1) {
int x1 = e.getX();
int y1 = e.getY();
ControlPoints[k][h].x = x1;
ControlPoints[k][h].y = y1;
repaint();
}
}
public void process_actionPerformed(ActionEvent e) {
Graphics g = getGraphics();
curve_generation(g, ControlPoints);
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 2; j++) {
g.drawLine(ControlPoints[i][j].x, ControlPoints[i][j].y,
ControlPoints[i][j + 1].x, ControlPoints[i][j + 1].y);
g.drawLine(ControlPoints[j][i].x, ControlPoints[j][i].y,
ControlPoints[j + 1][i].x, ControlPoints[j + 1][i].y);
}
}
}
void curve_generation(Graphics g, Point[][] p) {
Point[][] R = new Point[4][4];
Point[][] W = new Point[4][4];
Point[][] G = new Point[4][4];
Point[][] Q = new Point[4][4];
Point[][][] result = new Point[4][4][4];
Vector v = new Vector();
int k, xx, yy, i, j, kk;
double m;
m = 0.0D;
for (k = 0; k <= 3; k++) {
double temp1, temp2, temp3;
// temp1=Math.max(distance(p[k][1],p[k][0],p[k][3]),distance(p[k][2],p[k][0],p[k][3]));
// temp2=Math.max(distance(p[1][k],p[0][k],p[3][k]),distance(p[2][k],p[0][k],p[3][k]));
temp1 = Math.max(distance(p[k][0], p[k][1]), distance(p[k][1], p[k][2]));
temp2 = Math.max(distance(p[k][2], p[k][3]), distance(p[0][k], p[1][k]));
temp3 = Math.max(distance(p[1][k], p[2][k]), distance(p[2][k], p[3][k]));
if (temp1 > m) {
m = temp1;
}
if (temp2 > m) {
m = temp2;
}
if (temp3 > m) {
m = temp3;
}
}
if (m >= 5) {
result = curve_split(p);
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
for (k = 0; k <= 3; k++) {
v.addElement(result[i][j][k]);
}
}
}
int s = v.size();
for (k = 0; k <= 3; k++) {
for (i = 0; i <= 3; i++) {
for (j = 0; j <= 3; j++) {
W[i][j] = (Point) v.get(s - 1);
v.remove(s - 1);
s = s - 1;
}
}
curve_generation(g, W);
}
}
else {
System.out.println("m=" + m);
for (int z = 0; z <= 3; z++) {
g.drawLine(p[z][0].x, p[z][0].y, p[z][3].x, p[z][3].y);
g.drawLine(p[0][z].x, p[0][z].y, p[3][z].x, p[3][z].y);
}
}
}
Point[][][] curve_split(Point[][] p) {
int i, j, h, a, b, c, xx, yy;
Point[][] R = new Point[4][4];
Point[][] W = new Point[4][4];
Point[][] G = new Point[4][4];
Point[][] Q = new Point[4][4];
Point[][][] result = new Point[4][4][4];
System.arraycopy(p, 0, G, 0, p.length);
int n = 3;
int m = 3;
for (j = 0; j <= m; j++) {
for (i = 1; i <= n; i++) {
R[n + 1 - i][j] = new Point(G[n][j].x, G[n][j].y);
for (h = n; h >= i; h--) {
G[h][j].x = (G[h - 1][j].x + G[h][j].x) / 2;
G[h][j].y = (G[h - 1][j].y + G[h][j].y) / 2;
}
}
R[0][j] = new Point(G[n][j].x, G[n][j].y);
}
for (a = 0; a <= n; a++) {
for (b = 1; b <= m; b++) {
Q[a][m + 1 - b] = new Point(G[a][m].x, G[a][m].y);
W[a][m + 1 - b] = new Point(R[a][m].x, R[a][m].y);
for (c = m; c >= b; c--) {
G[a][c].x = (G[a][c].x + G[a][c - 1].x) / 2;
G[a][c].y = (G[a][c].y + G[a][c - 1].y) / 2;
R[a][c].x = (R[a][c].x + R[a][c - 1].x) / 2;
R[a][c].y = (R[a][c].y + R[a][c - 1].y) / 2;
}
}
Q[a][0] = new Point(G[a][m].x, G[a][m].y);
W[a][0] = new Point(R[a][m].x, R[a][m].y);
}
for (xx = 0; xx <= 3; xx++) {
for (yy = 0; yy <= 3; yy++) {
result[0][xx][yy] = new Point(G[xx][yy].x, G[xx][yy].y);
result[1][xx][yy] = new Point(Q[xx][yy].x, Q[xx][yy].y);
result[2][xx][yy] = new Point(R[xx][yy].x, R[xx][yy].y);
result[3][xx][yy] = new Point(W[xx][yy].x, W[xx][yy].y);
}
}
return result;
}
double distance(Point p1, Point p2, Point p3) {
double d;
Line2D.Float line = new Line2D.Float(p2.x, p2.y, p3.x, p3.y);
d = line.ptLineDist(p1);
return d;
}
double distance(Point p1, Point p2) {
double d;
d = p1.distance(p2);
return d;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -