每当我调整窗口大小时,它都会变成空白,形状消失。 我尝试添加:Toolkit.getDefaultToolkit()。sync();刷新画布但它没有用。 有谁知道这是什么问题?
这是代码:
public class test2 extends JFrame {
static float x;
static float y;
static float z;
// Setup a SimpleUniverse by referenching a Canvas3D
static GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(config);
// Creation du Canvas 3D
RefreshCanvas3D canvas3D =
new RefreshCanvas3D(config);
class RefreshCanvas3D extends Canvas3D {
public RefreshCanvas3D(GraphicsConfiguration config) {
super(config);
}
public void paint(Graphics g) {
super.paint(g);
Toolkit.getDefaultToolkit().sync();
}
}
//Build View
protected BranchGroup buildViewBranch(Canvas3D c) {
BranchGroup viewBranch = new BranchGroup();
Transform3D viewXfm = new Transform3D();
viewXfm.set(new Vector3f(0.0f, 0.0f, 5.0f));
TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
ViewPlatform myViewPlatform = new ViewPlatform();
PhysicalBody myBody = new PhysicalBody();
PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
viewXfmGroup.addChild(myViewPlatform);
viewBranch.addChild(viewXfmGroup);
View myView = new View();
myView.addCanvas3D(c);
myView.attachViewPlatform(myViewPlatform);
myView.setPhysicalBody(myBody);
myView.setPhysicalEnvironment(myEnvironment);
return viewBranch;
}
//Build branchgroup
protected BranchGroup buildContentBranch(Node shape) {
BranchGroup contentBranch = new BranchGroup();
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0));
TransformGroup rotationGroup = new TransformGroup(rotateCube);
contentBranch.addChild(rotationGroup);
rotationGroup.addChild(shape);
return contentBranch;
}
//create a container box
protected Node buildShape(float x, float y, float z) {
Appearance app = new Appearance();
//Create the polygon attributes
PolygonAttributes polyAttr = new PolygonAttributes();
//Set them so that the draw mode is polygon line
polyAttr.setPolygonMode(PolygonAttributes.CULL_BACK);
app.setPolygonAttributes(polyAttr);
Box containerBox = new Box( x, y, z, app);
return containerBox;
}
static Box addProductBox(Float x1, Float y1, Float z1, Color3f c1, Color3f c2)
{
Appearance app = new Appearance();
Color3f color = new Color3f(Color.yellow);
Color3f black = new Color3f(Color.lightGray );
Color3f white = new Color3f(Color.lightGray);
Material mat=new Material(color, black, color, white, 70f);
mat.setCapability(Material.SPECULAR);
mat.setDiffuseColor(c1);
mat.setSpecularColor(c2);
mat.setShininess(6.0f);
app.setMaterial(mat);
Box box = new Box(x1, y1, z1, app);
return box;
}
public test2(float x, float y , float z) {
VirtualUniverse myUniverse = new VirtualUniverse();
Locale myLocale = new Locale(myUniverse);
BranchGroup nodeRoot = new BranchGroup();
nodeRoot=buildContentBranch(buildShape(x, y, z));
Shape3D backShape = ((Box) buildShape(x, y, z)).getShape(Box.BACK);
TriangleStripArray geo = (TriangleStripArray) backShape.getGeometry();
int rr = 2;
float[] position = new float[3];
geo.getCoordinates(rr, position);
Vector3f pp = new Vector3f (position[0],position[1], position[2]);
Transform3D transform = new Transform3D();
transform.setTranslation(pp);
Box ProductBox = addProductBox(0.1f,0.1f,0.1f,
new Color3f(0.7f, .15f, .15f), new Color3f(0.7f, .15f, .15f));
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0));
TransformGroup rotationGroup = new TransformGroup(rotateCube);
TransformGroup TranslateGroup = new TransformGroup(transform);
TranslateGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
rotationGroup.addChild(ProductBox);
TranslateGroup.addChild(rotationGroup);
nodeRoot.addChild(TranslateGroup);
myLocale.addBranchGraph(nodeRoot);
setTitle("Container Loading");
setSize(800,800);
setLayout(new BorderLayout());
add("Center", canvas);
setVisible(true);
myLocale.addBranchGraph(buildViewBranch(canvas));
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test2(x, y , z);
}
});
}
}