FileDocCategorySizeDatePackage
BooksDemo.javaAPI DocExample8713Mon Jan 09 11:02:00 GMT 2006None

BooksDemo

public class BooksDemo extends JFrame

Fields Summary
private static final int
CANVAS3D_WIDTH
private static final int
CANVAS3D_HEIGHT
private boolean
front
private JPanel
centerPanel
private Canvas3D
c3d
private RotationInterpolator
rotator1
private Alpha
rotor1Alpha
private com.sun.j3d.utils.geometry.Box
book
private HashMap
textures
Constructors Summary
public BooksDemo()

    
     
    
        super("AmazonPick");

        JButton cover1 = UIHelper.createButton("", "cover1_small_button", true);
        cover1.addActionListener(new CoverSwitcher("cover1"));
        JButton cover2 = UIHelper.createButton("", "cover2_small_button", true);
        cover2.addActionListener(new CoverSwitcher("cover2"));
        JButton cover3 = UIHelper.createButton("", "cover3_small_button", true);
        cover3.addActionListener(new CoverSwitcher("cover3"));

        JPanel buttons = new JPanel();
        buttons.add(cover1);
        buttons.add(cover2);
        buttons.add(cover3);
        buttons.setOpaque(false);
        
        c3d = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        c3d.setSize(CANVAS3D_WIDTH, CANVAS3D_HEIGHT);

        centerPanel.add(c3d);
        centerPanel.setOpaque(false);

        this.setContentPane(new GradientPanel());
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(centerPanel, BorderLayout.CENTER);
        this.getContentPane().add(buttons, BorderLayout.SOUTH);

        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new JMenuItem("New Search..."));
        fileMenu.add(new JMenuItem("Save Search..."));
        fileMenu.addSeparator();
        fileMenu.add(new JMenuItem("Quit"));
        menuBar.add(fileMenu);
        this.setJMenuBar(menuBar);

        this.pack();
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        UIHelper.centerOnScreen(this);
    
Methods Summary
protected BackgroundcreateBackground()

        BufferedImage image = new BufferedImage(getContentPane().getWidth(),
                                                getContentPane().getHeight(),
                                                BufferedImage.TYPE_INT_RGB);
        getContentPane().paint(image.getGraphics());
    
        BufferedImage subImage = new BufferedImage(CANVAS3D_WIDTH,
                                                   CANVAS3D_HEIGHT,
                                                   BufferedImage.TYPE_INT_RGB);
        Graphics2D subGraphics = (Graphics2D) subImage.getGraphics();
        subGraphics.drawImage(image, null, -c3d.getX(), -c3d.getY());
    
        Background bg = new Background(new ImageComponent2D(ImageComponent2D.FORMAT_RGB, subImage));
        BoundingSphere bounds = new BoundingSphere();
        bounds.setRadius(100.0);
        bg.setApplicationBounds(bounds);
    
        return bg;
    
public voidcreateScene()

        BranchGroup objRoot = new BranchGroup();
        objRoot.addChild(createBackground());
    
        TransformGroup objTg = new TransformGroup();
        objTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    
        BoundingSphere bounds = new BoundingSphere();
        bounds.setRadius(100.0);

        Transform3D yAxis = new Transform3D();
        rotor1Alpha = new Alpha(1, 400);
        rotator1 = new RotationInterpolator(rotor1Alpha, objTg, yAxis, (float) Math.PI * 1.0f, (float) Math.PI * 2.0f);
        rotator1.setSchedulingBounds(bounds);
    
        textures.put("pages_top", createTexture("pages_top.jpg"));
        textures.put("pages",     createTexture("amazon.jpg"));
        textures.put("amazon",    createTexture("amazon.jpg"));
        textures.put("cover1",    createTexture("cover1.jpg"));
        textures.put("cover2",    createTexture("cover2.jpg"));
        textures.put("cover3",    createTexture("cover3.jpg"));
    
        book = new com.sun.j3d.utils.geometry.Box(0.5f, 0.7f, 0.15f, com.sun.j3d.utils.geometry.Box.GENERATE_TEXTURE_COORDS, new Appearance());
        book.getShape(book.TOP).setAppearance((Appearance) textures.get("pages_top"));
        book.getShape(book.RIGHT).setAppearance((Appearance) textures.get("pages"));
        book.getShape(book.LEFT).setAppearance((Appearance) textures.get("amazon"));
        book.getShape(book.FRONT).setAppearance((Appearance) textures.get("cover1"));

        book.getShape(book.BACK).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
        book.getShape(book.FRONT).setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);

        objTg.addChild(book);
        objTg.addChild(rotator1);
    
        Transform3D spin = new Transform3D();
        Transform3D tempspin = new Transform3D();
        
        spin.rotX(Math.PI / 8.0d);
        tempspin.rotY(Math.PI / 7.0d);
        spin.mul(tempspin);
       
        TransformGroup objTrans = new TransformGroup(spin);
        objTrans.addChild(objTg);
    
        objRoot.addChild(objTrans);
    
        SimpleUniverse u = new SimpleUniverse(c3d);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(objRoot);
    
        View view = u.getViewer().getView();
        view.setSceneAntialiasingEnable(true);
    
private AppearancecreateTexture(java.lang.String fileName)

        Image sourceImage = UIHelper.readImage(fileName);
        if (sourceImage == null)
          System.out.println("Image could not be loaded from " + fileName);

        TextureLoader loader = new TextureLoader(sourceImage, this);
        ImageComponent2D image = loader.getImage();
        
        if (image == null)
          System.out.println("Texture could not be loaded from " + fileName);
        
        Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image.getWidth(), image.getHeight());
        texture.setImage(0, image);
        texture.setEnable(true);
        texture.setMagFilter(Texture.BASE_LEVEL_LINEAR);
        texture.setMinFilter(Texture.BASE_LEVEL_LINEAR);
 
        Appearance appearance = new Appearance();
        PolygonAttributes polyAttributes = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0f);
        appearance.setPolygonAttributes(polyAttributes);
        appearance.setTexture(texture);

        TextureAttributes textureAttributes = new TextureAttributes();
        appearance.setTextureAttributes(textureAttributes);

        return appearance;
    
private booleanisBookShowingFront()

      return front;
    
public static voidmain(java.lang.String[] argv)

        final BooksDemo xframe = new BooksDemo();
        xframe.setVisible(true);
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                xframe.createScene();
            }
        });
    
private voidrotateBook()

        if (!isBookShowingFront())
        {
            rotator1.setMinimumAngle((float) Math.PI * 1.0f);
            rotator1.setMaximumAngle(0.0f);
        } else {
            rotator1.setMinimumAngle(0.0f);
            rotator1.setMaximumAngle((float) Math.PI * 1.0f);
        }

        front = !front;
        rotor1Alpha.setStartTime(System.currentTimeMillis());