1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | import com.sun.j3d.utils.behaviors.mouse.*; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.universe.SimpleUniverse; import java.awt.*; import java.awt.event.*; import javax.media.j3d.*; import javax.swing.*; import javax.vecmath.*; import java.util.*; public class CubeCounting extends JFrame{ private BranchGroup bg=null; private BranchGroup bg2=null; private TransformGroup camera=null; private Transform3D transform; private Vector3d vector; private JPanel p=null; private JTextArea ta=null; private JButton b=null; private JLabel l=null; private int totalcubes=0; public static void main(String[] args){ new CubeCounting(); } public CubeCounting(){ super("Cube Counting"); setSize(800,600); setLayout(new BorderLayout()); GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); Canvas3D c3d=new Canvas3D(config); add("Center",c3d); SimpleUniverse su=new SimpleUniverse(c3d); su.getViewingPlatform().setNominalViewingTransform(); bg=new BranchGroup(); camera=su.getViewingPlatform().getViewPlatformTransform(); camera=new TransformGroup(); camera.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); camera.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); bg.addChild(camera); bg2=new BranchGroup(); camera.addChild(bg2); MouseRotate mr=new MouseRotate(); mr.setTransformGroup(camera); mr.setSchedulingBounds(new BoundingSphere()); bg.addChild(mr); drawCube(); su.addBranchGraph(bg); p=new JPanel(); p.setLayout(new FlowLayout()); ta=new JTextArea(); ta.setColumns(10); b=new JButton("Submit"); b.addActionListener(new ButtonHandler()); l=new JLabel(); p.add(ta); p.add(b); p.add(l); add("South",p); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dispose(); System.exit(0);} } ); setVisible(true); } private void drawCube(){ Random rand=new Random(); int x=0; do{ x=rand.nextInt(10); }while(x<=5); rand=new Random(); int y=0; do{ y=rand.nextInt(10); }while(y<=5); rand=new Random(); int z=0; do{ z=rand.nextInt(10); }while(z<=5); boolean current=false; Boolean[][][] cubearray=new Boolean[z][y][x]; for(int a=0;a<z;a=a+1){ for(int b=0;b<y;b=b+1){ for(int c=0;c<x;c=c+1){ rand=new Random(); int temp=rand.nextInt(2); if(temp==1){ current=true; } else{ current=false; } if(b==0){ cubearray[a][b][c]=current; } else{ if(cubearray[a][b-1][c]==true){ cubearray[a][b][c]=current; } else{ cubearray[a][b][c]=false; } } } } } for(int i=-(z/2);i<z-(z/2);i=i+1){ for(int j=-(y/2);j<y-(y/2);j=j+1){ for(int k=-(x/2);k<x-(x/2);k=k+1){ if(cubearray[i+(z/2)][j+(y/2)][k+(x/2)]==true){ TransformGroup tg=new TransformGroup(); Transform3D t3d=new Transform3D(); Vector3f v=new Vector3f((0.05f*2.1f*k),(0.05f*2.1f*j)+0.15f,(0.05f*2.1f*i)); t3d.setTranslation(v); tg.setTransform(t3d); Shape3D cube=new ColorCube(0.05); tg.addChild(cube); bg2.addChild(tg); totalcubes++; //System.out.println(totalcubes); } else{ continue; } } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String st=ta.getText(); int result=Integer.valueOf(st).intValue(); if(result==totalcubes){ l.setText("Right"); } else{ l.setText("Wrong"); } } } } |
Wednesday, March 9, 2016
3DCube Counting Test
This program utilizes Java 3D API to display a number of cubes on the screen. User counts the total cubes displayed and then submits his/her answer by clicking a button. The result (right or wrong) can be seen on a label. The image displayed is rotatable.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment