Search code examples
javanetbeansgraphics3dcube

cube shrinks successively, when i rotate it with mouse..


i write the following code for a cube, that rotates on mouse motion, but when i rotate the cube, or resize the Frame, the cube shrinks successively. Is this the code or Frame, that is to be changed.. Pls Help,,

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) {
    }
}

suggest the solution code for it.. It's working fine , except that the cube is shrinking


Solution

  • You're storing the coordinates as ints.

    Each time you do a rotation, you do a calculation which doesn't yield an integer; when you cast, you truncate the integer, rounding towards zero. Hence the shrinkage.

    Use double instead, or, better, simply calculate a rotation matrix and apply that to the coordinates when you are rendering it.