Saturday, March 12, 2016

3D Box With Different Faces

This program shows the use of the TextureLoader class to apply a different texture to each face of a 3D cube. Six two-dimensional images are used for this purpose. By clicking the "Spin" button, the dice will be rotated around x,y, and x axis. The Transform3D is modified randomly with the help of Random class.


  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
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.*;
import javax.vecmath.Vector3f;
import java.util.*;

public class Dice3D extends JFrame{
    private Box box=null;
    private BranchGroup bg=null;
    private SimpleUniverse su=null;
    private TransformGroup tg=null;
    private Transform3D t3d=null;
    private JButton bspin=null;
    
    public Dice3D(){
        super("Dice 3D");
        setSize(300,400);
        setLayout(new BorderLayout());
        
        GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas=new Canvas3D(config);
        canvas.setSize(300,300);
        add("Center",canvas);
        
        su=new SimpleUniverse(canvas);
        su.getViewingPlatform().setNominalViewingTransform();
        
        bg=new BranchGroup();
        Appearance ap=new Appearance();
        ap.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
 ap.setCapability(Appearance.ALLOW_TEXGEN_WRITE);
        box=new Box(0.5f,0.5f,0.5f,Primitive.GENERATE_NORMALS + Primitive.GENERATE_TEXTURE_COORDS,ap);
        box.setCapability(Box.ENABLE_APPEARANCE_MODIFY);
        box.setCapability(Box.GEOMETRY_NOT_SHARED);
        
        TextureLoader loader = new TextureLoader("c://Users/MARIO/one.png","RGB",new Container());
        Texture texture = loader.getTexture();
        ap.setTexture(texture);
        box.getShape(Box.FRONT).setAppearance(ap);
        
        Appearance ap2=new Appearance();
        TextureLoader loader2 = new TextureLoader("c://Users/MARIO/two.png","RGB",new Container());
        Texture texture2 = loader2.getTexture();
        ap2.setTexture(texture2);
        box.getShape(Box.BACK).setAppearance(ap2);
        
        Appearance ap3=new Appearance();
        TextureLoader loader3 = new TextureLoader("c://Users/MARIO/three.png","RGB",new Container());
        Texture texture3 = loader3.getTexture();
        ap3.setTexture(texture3);
        box.getShape(Box.LEFT).setAppearance(ap3);
        
        Appearance ap4=new Appearance();
        TextureLoader loader4 = new TextureLoader("c://Users/MARIO/four.png","RGB",new Container());
        Texture texture4 = loader4.getTexture();
        ap4.setTexture(texture4);
        box.getShape(Box.RIGHT).setAppearance(ap4);
        
        Appearance ap5=new Appearance();
        TextureLoader loader5 = new TextureLoader("c://Users/MARIO/five.png","RGB",new Container());
        Texture texture5 = loader5.getTexture();
        ap5.setTexture(texture5);
        box.getShape(Box.TOP).setAppearance(ap5);
        
        Appearance ap6=new Appearance();
        TextureLoader loader6 = new TextureLoader("c://Users/MARIO/six.png","RGB",new Container());
        Texture texture6 = loader6.getTexture();
        ap6.setTexture(texture6);
        box.getShape(Box.BOTTOM).setAppearance(ap6);
        
        tg = new TransformGroup();
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        t3d = new Transform3D();
        Vector3f v3f = new Vector3f(0.0f,0.0f,0.0f);
        t3d.setTranslation(v3f);
        
        tg.setTransform(t3d);
        tg.addChild(box);
        bg.addChild(tg);
        su.addBranchGraph(bg);
        
        bspin=new JButton("Spin");
        bspin.addActionListener(new ButtonHandler());
        add("South",bspin);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    
    class ButtonHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            Random rand=new Random();
            int temp=rand.nextInt(5)+1;
            Transform3D rot = new Transform3D();
            rot.rotX(Math.PI/temp);
            t3d.mul(rot);
            rot.rotY(Math.PI/temp);
            t3d.mul(rot);
            rot.rotZ(Math.PI/temp);        
            t3d.mul(rot);
            tg.setTransform(t3d);
        }
    }
    
    public static void main(String[] args){
        new Dice3D();
    }
}

No comments:

Post a Comment