import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*; //SimpleUniverse
import com.sun.j3d.utils.behaviors.vp.*; //OrbitBehavior
public class PerformanceTest extends JFrame implements ActionListener
{
public static final long serialVersionUID = 0;
public static final int SIDE_LENGTH = 125;
//Konstruktor
public PerformanceTest()
{
super ("Performance-Test");
setSize (getToolkit().getScreenSize());
setLocationRelativeTo (null); //zentrieren
setDefaultCloseOperation (EXIT_ON_CLOSE);
JPopupMenu.setDefaultLightWeightPopupEnabled (false);
setCloseMenuBar (this);
Canvas3D c3d = new Canvas3D (SimpleUniverse.getPreferredConfiguration()); //So am besten
SimpleUniverse simpleU = new SimpleUniverse (c3d); //Das VirtualUniverse, hier wird auch schon der View hinzugefügt
BranchGroup scene = createSceneGraph();
OrbitBehavior orbit = new OrbitBehavior(c3d, OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds (new BoundingSphere (new Point3d(), 10000));
simpleU.getViewingPlatform().setViewPlatformBehavior (orbit);
scene.compile();
simpleU.addBranchGraph (scene); //Fügt den SceneGraph hinzu
Transform3D cam = new Transform3D();
simpleU.getViewingPlatform().getViewPlatformTransform().getTransform(cam);
cam.setTranslation (new Vector3f (0, 8, 10));
simpleU.getViewingPlatform().getViewPlatformTransform().setTransform(cam);
add (c3d);
setVisible (true);
}
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
TransformGroup objSpin = new TransformGroup();
objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objSpin);
Alpha rotationAlpha = new Alpha(-1, 60000); //Unendlich oft drehen, einmal in 60 s
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objSpin);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 10000);
rotator.setSchedulingBounds(bounds);
objSpin.addChild(rotator);
BranchGroup hill = createHill();
objSpin.addChild(hill);
return objRoot;
}
public BranchGroup createHill()
{
GeometryArray array = new QuadArray(SIDE_LENGTH * 1000, GeometryArray.COORDINATES);
Point3d point = new Point3d();
System.out.println("Generating Points...");
for (int i = 0, index = 0; i < SIDE_LENGTH; i++){
point.x = -SIDE_LENGTH / 20 + i / 10.0;
for (int j = 0; j < SIDE_LENGTH; j++){
point.z = -SIDE_LENGTH / 20 + j / 10.0;
setCoordinate(array, index++, point);
point.z += 0.1;
setCoordinate(array, index++, point);
point.x += 0.1;
setCoordinate(array, index++, point);
point.z -= 0.1;
setCoordinate(array, index++, point);
point.x -= 0.1;
}
}
System.out.println("Done");
Appearance appearance = new Appearance();
appearance.setColoringAttributes(new ColoringAttributes(new Color3f(Color.WHITE), ColoringAttributes.FASTEST));
appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0));
Shape3D shape = new Shape3D (array, appearance);
BranchGroup bg = new BranchGroup();
bg.addChild (shape);
return bg;
}
private void setCoordinate (GeometryArray array, int index, Point3d point)
{
//y = 5 / (0.2*x²+0.8) (und für z)
point.y = 5 / (0.2 * point.x * point.x + 0.8) + 5 / (0.2 * point.z * point.z + 0.8);
array.setCoordinate(index, point);
}
//Beenden-Menu
private void setCloseMenuBar (JFrame f)
{
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu ("Datei");
jmb.add (jm);
JMenuItem close = new JMenuItem ("Beenden");
jm.add (close);
close.addActionListener (this);
f.setJMenuBar (jmb);
}
public void actionPerformed (ActionEvent evt)
{
System.exit (0);
}
//Startmethode
public static void main (String[]args)
{
new PerformanceTest();
}
}