import java.applet.Applet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.behaviors.keyboard.*; import javax.media.j3d.*; import javax.vecmath.*; public class Kuubilisamine2 extends JApplet implements ActionListener{ TransformGroup keere1; Label lx=new Label("x="); Label ly=new Label("y="); Label lz=new Label("z="); Label lk1=new Label("nurkx="); Label lk2=new Label("nurky="); Label lk3=new Label("nurkz="); TextField tfx=new TextField("1"); TextField tfy=new TextField("0"); TextField tfz=new TextField("-4"); TextField tfk1=new TextField("0"); TextField tfk2=new TextField("0.2"); TextField tfk3=new TextField("0"); Button lisanupp=new Button("Lisa kuup"); Button tagasinupp=new Button("Algasendisse"); Checkbox c1=new Checkbox("Juhukoht"); Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); SimpleUniverse u = new SimpleUniverse(c); BranchGroup juur = new BranchGroup(); public Kuubilisamine2() { Panel p=new Panel(new GridLayout(2, 6)); p.add(lx); p.add(tfx); p.add(ly); p.add(tfy); p.add(lz); p.add(tfz); p.add(lk1); p.add(tfk1); p.add(lk2); p.add(tfk2); p.add(lk3); p.add(tfk3); Panel p2=new Panel(new GridLayout(1, 3)); p2.add(c1); p2.add(lisanupp); p2.add(tagasinupp); Panel p3=new Panel(new BorderLayout()); p3.add(p2, BorderLayout.SOUTH); p3.add(p, BorderLayout.CENTER); getContentPane().add(c, BorderLayout.CENTER); getContentPane().add(p3, BorderLayout.NORTH); juur.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); KeyNavigatorBehavior kb=new KeyNavigatorBehavior( u.getViewingPlatform().getViewPlatformTransform() ); kb.setSchedulingBounds(new BoundingSphere(new Point3d(), 100)); juur.addChild(kb); juur.compile(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(juur); lisanupp.addActionListener(this); tagasinupp.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==lisanupp){ BranchGroup oks=new BranchGroup(); TransformGroup nihe=new TransformGroup(); oks.insertChild(nihe, 0); Transform3D t1=new Transform3D(); Transform3D tx=new Transform3D(); Transform3D ty=new Transform3D(); Transform3D tz=new Transform3D(); if(c1.getState()){ tfx.setText(""+(20*Math.random()-10)); tfy.setText(""+(20*Math.random()-10)); tfz.setText(""+(20*Math.random()-10)); tfk1.setText(""+(3*Math.random()-1)); tfk2.setText(""+(3*Math.random()-1)); tfk3.setText(""+(3*Math.random()-1)); } t1.setTranslation(new Vector3f(numbriks(tfx), numbriks(tfy), numbriks(tfz))); tx.rotX(numbriks(tfk1)); ty.rotY(numbriks(tfk2)); tz.rotZ(numbriks(tfk3)); t1.mul(tx); t1.mul(ty); t1.mul(tz); nihe.setTransform(t1); nihe.addChild(new ColorCube(0.3)); oks.compile(); juur.insertChild(oks, 0); } if(e.getSource()==tagasinupp){ u.getViewingPlatform().setNominalViewingTransform(); } } float numbriks(TextField tf){ return Float.parseFloat(tf.getText().trim()); } public static void main(String[] args) { Frame f=new Frame("Kuubi lisamine"); f.add(new Kuubilisamine2()); f.setSize(300, 400); f.setVisible(true); } }