我为一个立方体编写以下代码,该代码在鼠标运动时旋转, 但是当我旋转立方体或调整框架大小时,立方体会连续缩小。 这是代码还是框架,需要更改.. 请帮忙,,
public class Cube extends javax.swing.JPanel implements MouseMotionListener {
int node0[] = {-100, -100, -100};
int node1[] = {-100, -100, 100};
int node2[] = {-100, 100, -100};
int node3[] = {-100, 100, 100};
int node4[] = {100, -100, -100};
int node5[] = { 100, -100, 100};
int node6[] = {100, 100, -100};
int node7[] = {100, 100, 100};
int nodes[][] = {node0, node1, node2, node3, node4, node5, node6, node7};
int edge0[] = {0, 1};
int edge1[] = {1, 3};
int edge2[] = {3, 2};
int edge3[] = {2, 0};
int edge4[] = {4, 5};
int edge5[] = {5, 7};
int edge6[] = {7, 6};
int edge7[] = {6, 4};
int edge8[] = {0, 4};
int edge9[] = {1, 5};
int edge10[] = {2, 6};
int edge11[] = {3, 7};
int edges[][] = {edge0, edge1, edge2, edge3, edge4, edge5, edge6, edge7, edge8, edge9, edge10, edge11};
// Rotate shape around the z-axis
public void rotateZ3D(int theta) {
double sinTheta = Math.sin(theta);
double cosTheta = Math.cos(theta);
for (int n=0; n<8; n++) {
int[] node = nodes[n];
int x = node[0];
int y = node[1];
node[0] = (int) (x * cosTheta - y * sinTheta);
node[1] = (int) (y * cosTheta + x * sinTheta);
}
};
public void rotateX3D(int theta) {
double sinTheta = Math.sin(theta);
double cosTheta = Math.cos(theta);
for (int n=0; n<8; n++) {
int[] node = nodes[n];
int y = node[1];
int z = node[2];
node[1] = (int) (y * cosTheta - z * sinTheta);
node[2] = (int) (z * cosTheta + y * sinTheta);
}
};
public void rotateY3D(int theta) {
double sinTheta = Math.sin(theta);
double cosTheta = Math.cos(theta);
for (int n=0; n<8; n++) {
int[] node = nodes[n];
int x = node[0];
int z = node[2];
node[0] = (int) (x * cosTheta - z * sinTheta);
node[2] = (int) (z * cosTheta + x * sinTheta);
}
};
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw edges
g.translate(150, 150);
rotateZ3D(60);
rotateY3D(60);
rotateX3D(60);
for (int e=0; e<12; e++) {
int n0 = edges[e][0];
int n1 = edges[e][1];
int node0[] = nodes[n0];
int node1[] = nodes[n1];
g.drawLine(node0[0], node0[1], node1[0], node1[1]);
}
// Draw nodes
for (int n=0; n<8; n++) {
int node[] = nodes[n];
g.drawOval(node[0], node[1], 5,5);
g.drawString(n+"", node[0], node[1]);
}
};
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JFrame j = new JFrame();
Cube p = new Cube();
j.add(p);
j.addMouseMotionListener(p);
j.setSize(500,500);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.show();
// TODO code application logic here
}
@Override
public void mouseDragged(MouseEvent me) {
int x0 = me.getX();
int y0 = me.getY();
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(Cube.class.getName()).log(Level.SEVERE, null, ex);
}
int x1 = me.getX();
int y1 = me.getX();
rotateY3D(x1 - x0);
rotateX3D(y1 - y0);
repaint();
}
@Override
public void mouseMoved(MouseEvent me) {
}
}
建议解决方案代码.. 它的工作正常,除了立方体缩小
答案 0 :(得分:1)
您将坐标存储为int
s。
每次进行旋转时,都会进行不产生整数的计算;当你施放时,你截断整数,向零舍入。因此缩小了。
改为使用double
,或者更好的是,只需计算旋转矩阵并在渲染时将其应用于坐标。