我已经搜索了几个帖子,但我无法让我的程序正常工作。所以这是问题----我有一个显示颜色立方体和保存按钮的类,保存按钮用于将java3d对象(此处为colorcube)保存到目标文件以供以后使用。
目标文件已创建,但除了 2-3 行外,它似乎是空的。请看一看 - -
public class Rotation extends Applet implements ActionListener {
//---- Attribute -----------------------
private SimpleUniverse universe;
private Canvas3D canvas3D;
private static Frame frame;
BranchGroup szene = new BranchGroup();
TransformGroup objDreh = new TransformGroup();
ColorCube cube = new ColorCube(0.4);
public Rotation()
{}
public void init() {
setLayout(new BorderLayout());
JPanel panel = new JPanel();
JButton btn = new JButton("Save");
btn.addActionListener(this);
panel.add(btn);
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas3D = new Canvas3D(config);
add(canvas3D, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
szene = macheSzene();
szene.compile();
universe = new SimpleUniverse(canvas3D);
universe.getViewingPlatform().setNominalViewingTransform();
universe.addBranchGraph(szene);
}
/**
* Erstellt den Szenegraphen
*
* @return BranchGroup
*/
public BranchGroup macheSzene() {
BranchGroup objWurzel = new BranchGroup();
// Transformation, 2 Rotationen:
Transform3D drehung = new Transform3D();
Transform3D drehung2 = new Transform3D();
drehung.rotX(Math.PI / 4.0d);
drehung2.rotY(Math.PI / 5.0d);
drehung.mul(drehung2);
objDreh = new TransformGroup(drehung);
TransformGroup spin = new TransformGroup();
spin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
spin.addChild(cube);
objDreh.addChild(spin);
objWurzel.addChild(objDreh);
// Drehung
Alpha spinAlpha = new Alpha(-1, 5000);
RotationInterpolator dreher = new RotationInterpolator(spinAlpha, spin);
BoundingSphere zone = new BoundingSphere();
dreher.setSchedulingBounds(zone);
spin.addChild(dreher);
return objWurzel;
}
/**
* gibt speicher frei
*/
public void destroy() {
universe.removeAllLocales();
}
@Override
public void actionPerformed(ActionEvent e)
{
try {
writeIntoFile();
} catch (FileNotFoundException ex) {
Logger.getLogger(Rotation.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void writeIntoFile() throws FileNotFoundException
{
//throw new UnsupportedOperationException("Not supported yet.");
SerializeObject obj = new SerializeObject(cube);
try {
FileOutputStream out = new FileOutputStream("e://j3dObjects.xml");
try {
ObjectOutputStream sout = new ObjectOutputStream(out);
sout.writeObject(obj);
sout.close();
} catch (Exception err)
{ err.printStackTrace(); }
} catch(Exception err)
{ err.printStackTrace(); }
}
public static void main(String[] args) {
frame = new MainFrame(new Rotation(), 500, 500);
frame.setTitle("Rotation");
}
}
和可序列化的类--------
public class SerializeObject extends ColorCube implements Serializable {
ColorCube cb;
SerializeObject(ColorCube brn)
{
this.cb = brn;
}
}
请看一下,让我知道我在哪里做错了。还有一件事是可以将转换组写入目标文件吗?谢谢。