0

I don't quite get it, here's this code to produce a 3D cube:

import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.vecmath.*;
import javax.swing.*;
import java.awt.event.*;

public class pject extends JFrame {
private Canvas3D canvas;
private SimpleUniverse uni;
private BranchGroup scene;


public pject() {
    this.setSize(600,400);
    this.addKeyListener(new KeyHandler());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    this.add(canvas);

    uni = new SimpleUniverse(canvas);
    scene = new BranchGroup();
    start();
}
private void start() {
    ColorCube cc = new ColorCube(0.1d);
    scene.addChild(cc);
    Transform3D lookAt = new Transform3D();
    lookAt.lookAt(new Point3d(0.0, 0.0, 1.0), new Point3d(0.0, 0.0, 0.0), new Vector3d(0.0, 1.0, 0));
    lookAt.invert();
    uni.getViewingPlatform().getViewPlatformTransform().setTransform(lookAt);
    uni.addBranchGraph(scene);



}



public static void main(String[] args) {
    pject frame = new pject();
}

float x = 0.4f;     
private class KeyHandler extends KeyAdapter {
    public void keyPressed(KeyEvent e) {                       
        if (e.getKeyCode() == KeyEvent.VK_LEFT) x += 0.001;   

        Transform3D lookAtx = new Transform3D();
        lookAtx.lookAt(new Point3d(x, 0.0, 1.0), new Point3d(x, 0.0, 0.0), new Vector3d(0.0, 1.0, 0));
        lookAtx.invert();
        uni.getViewingPlatform().getViewPlatformTransform().setTransform(lookAtx);

    }
    public void keyReleased(KeyEvent e) {
    }     
}

 } 

It works, but sometimes. I compile it, it works. I make absolutely no changes at all, compile it, and it doesn't work. I then have to compile it 10-20 times until one of those compilations actually work.

When it doesn't work, it just shows a JFrame with no Canvas3D on it (or if there is one, it's not visible).

This just doesn't make any sense to me. Why would my code only sometimes compile right? Why is it that compiling it over and over again 10-20 eventually will show me my cube?

4

1 回答 1

0

调用 this.setVisible(true); 在构造函数的末尾。

于 2012-10-29T00:16:12.197 回答