Source Code : Image Component By Reference

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Image Component By Reference

Image Component By Reference

/*
 * @(#)ImageComponentByReferenceTest.java 1.14 02/10/21 13:41:45
 * 
 * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *  - Redistribution in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGES.
 * 
 * You acknowledge that Software is not designed,licensed or intended for use in
 * the design, construction, operation or maintenance of any nuclear facility.
 */

import java.awt.Container;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.util.Vector;

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Group;
import javax.media.j3d.ImageComponent;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class ImageComponentByReferenceTest extends JApplet implements
    ActionListener {

  Shape3D s1, s2;

  TextureLoader t0, t1, t2;

  int count = 0;

  Appearance app = new Appearance();

  BranchGroup objRoot = new BranchGroup();

  TransformGroup objTrans = new TransformGroup();

  BufferedImage bImage1;

  TiledImage checkBoard;

  boolean yUp = false;

  boolean byRef = true;

  JComboBox rasterType, texType;

  ImageComponent2D[] image = new ImageComponent2D[8];

  Appearance dummyApp = new Appearance();

  Texture2D texOne, texCheckBoard;

  javax.media.j3d.Raster raster;

  Box textureCube;

  Shape3D boxShape;

  int w1 = 64, h1 = 32, checkw = 16, checkh = 16;

  private java.net.URL texImage = null;

  private SimpleUniverse u = null;

  public BranchGroup createSceneGraph() {
    objRoot = new BranchGroup();

    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(Group.ALLOW_CHILDREN_WRITE);

    objRoot.addChild(objTrans);

    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);

    app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    textureCube = new Box(0.4f, 0.4f, 0.4f, Box.GENERATE_TEXTURE_COORDS
        | Box.GENERATE_NORMALS, app);
    boxShape = textureCube.getShape(Box.FRONT);
    boxShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
    objTrans.addChild(textureCube);

    checkBoard = new TiledImage();
    TextureLoader texLoader = new TextureLoader(texImage, this);
    ImageComponent2D oneImage = texLoader.getImage();
    bImage1 = oneImage.getImage();

    int index = 0;
    image[index++] = new ImageComponent2D(oneImage.getFormat(),
        (RenderedImage) bImage1, false, true);

    image[index++] = new ImageComponent2D(oneImage.getFormat(),
        (RenderedImage) bImage1, true, true);

    image[index++] = new ImageComponent2D(oneImage.getFormat(),
        (RenderedImage) bImage1, false, false);

    image[index++] = new ImageComponent2D(oneImage.getFormat(),
        (RenderedImage) bImage1, true, false);

    createRaster(objRoot);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA,
        checkBoard, false, true);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA,
        checkBoard, true, true);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA,
        checkBoard, false, false);

    image[index++] = new ImageComponent2D(ImageComponent.FORMAT_RGBA,
        checkBoard, true, false);

    texOne = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA, image[2]
        .getWidth(), image[2].getHeight());

    texOne.setCapability(Texture.ALLOW_IMAGE_WRITE);
    texOne.setImage(0, image[2]);

    app.setTexture(texOne);

    texCheckBoard = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
        image[4].getWidth(), image[4].getHeight());

    texCheckBoard.setCapability(Texture.ALLOW_IMAGE_WRITE);
    objRoot.compile();
    return objRoot;
  }

  public void actionPerformed(ActionEvent e) {
    Object target = e.getSource();

    if (target == rasterType) {
      if (rasterType.getSelectedIndex() < 4) {
        raster.setSize(w1, h1);
      } else {
        raster.setSize(checkw, checkh);
      }
      raster.setImage(image[rasterType.getSelectedIndex()]);
    } else if (target == texType) {
      boxShape.setAppearance(dummyApp);
      if (texType.getSelectedIndex() < 4) {
        texOne.setImage(0, image[texType.getSelectedIndex()]);
        app.setTexture(texOne);
      } else {
        texCheckBoard.setImage(0, image[texType.getSelectedIndex()]);
        app.setTexture(texCheckBoard);
      }

      boxShape.setAppearance(app);
    }

  }

  JPanel createImagePanel() {
    JPanel panel = new JPanel();
    String texVals[] = { "One_Yup_ByCopy", "One_Yup_ByReference",
        "One_Ydown_ByCopy", "One_Ydown_ByReference",
        "Checkered_Yup_ByCopy", "Checkered_Yup_ByReference",
        "Checkered_Ydown_ByCopy", "Checkered_Ydown_ByReference" };

    rasterType = new JComboBox(texVals);
    rasterType.setLightWeightPopupEnabled(false);
    rasterType.addActionListener(this);
    rasterType.setSelectedIndex(2);
    panel.add(new JLabel("Raster Image"));
    panel.add(rasterType);

    texType = new JComboBox(texVals);
    texType.setLightWeightPopupEnabled(false);
    texType.addActionListener(this);
    texType.setSelectedIndex(2);
    panel.add(new JLabel("Texture Image"));
    panel.add(texType);
    return panel;

  }

  public ImageComponentByReferenceTest() {
  }

  public ImageComponentByReferenceTest(java.net.URL url) {
    texImage = url;
  }

  public void init() {
    if (texImage == null) {
      // the path to the image for an applet
      try {
        texImage = new java.net.URL(getCodeBase().toString()
            + "/one.jpg");
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    }

    Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);
    Container contentPane = getContentPane();
    JPanel p = new JPanel();
    BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
    p.setLayout(boxlayout);
    contentPane.add("Center", c);

    contentPane.add("South", p);

    p.add(createImagePanel());

  }

  public void destroy() {
    u.cleanup();
  }

  public static void main(String[] args) {
    java.net.URL url = null;
    // the path to the image file for an application
    try {
      url = new java.net.URL("file:one.jpg");
    } catch (java.net.MalformedURLException ex) {
      System.out.println(ex.getMessage());
      System.exit(1);
    }

    new MainFrame(new ImageComponentByReferenceTest(url), 800, 700);
  }

  void createRaster(BranchGroup scene) {

    // Create raster geometries and shapes
    Vector3f trans = new Vector3f();
    Transform3D tr = new Transform3D();
    TransformGroup tg;

    // Left
    raster = new javax.media.j3d.Raster();
    raster.setCapability(javax.media.j3d.Raster.ALLOW_IMAGE_WRITE);
    raster.setCapability(javax.media.j3d.Raster.ALLOW_SIZE_WRITE);
    raster.setPosition(new Point3f(-0.9f, 0.75f, 0.0f));
    raster.setType(javax.media.j3d.Raster.RASTER_COLOR);
    raster.setOffset(0, 0);

    raster.setSize(image[2].getWidth(), image[2].getHeight());
    raster.setImage(image[2]);
    Shape3D sh = new Shape3D(raster, new Appearance());
    scene.addChild(sh);

  }
}

class TiledImage extends Object implements RenderedImage {

  WritableRaster[][] tile = new WritableRaster[3][3];

  WritableRaster bigTile;

  ComponentColorModel colorModel;

  BufferedImage checkBoard;

  int minX = -2;

  int minY = -1;

  TiledImage() {
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    int[] nBits = { 8, 8, 8, 8 };
    int i, j, k, cc = 255;
    int[] bandOffset = new int[4];
    colorModel = new ComponentColorModel(cs, nBits, true, false,
        Transparency.OPAQUE, 0);
    // Create 9 tiles
    bandOffset[0] = 3;
    bandOffset[1] = 2;
    bandOffset[2] = 1;
    bandOffset[3] = 0;
    for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
        tile[i][j] = Raster.createInterleavedRaster(
            DataBuffer.TYPE_BYTE, 8, 8, 32, 4, bandOffset, null);
      }
    }

    // tile {-2, -1}
    byte[] byteData = ((DataBufferByte) tile[0][0].getDataBuffer())
        .getData();
    for (i = 4, k = 8 * 4 * 4 + 4 * 4; i < 8; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) cc;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }

    // tile {-1, -1}
    byteData = ((DataBufferByte) tile[1][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) cc;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }

    // tile {1, -1}
    byteData = ((DataBufferByte) tile[2][0].getDataBuffer()).getData();
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
      for (j = 0; j < 4; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }

    // tile {-2, 0}
    byteData = ((DataBufferByte) tile[0][1].getDataBuffer()).getData();
    for (i = 0, k = 16; i < 4; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) cc;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }
    for (i = 4, k = 8 * 4 * 4 + 16; i < 8; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) 0;
      }
    }
    // tile {-1, 0}
    byteData = ((DataBufferByte) tile[1][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) cc;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }
    for (i = 0, k = 8 * 4 * 4; i < 4; i++) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) 0;
      }

      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) cc;
      }

    }

    // tile {0, 0}
    byteData = ((DataBufferByte) tile[2][1].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 4; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }
    for (i = 4, k = 8 * 4 * 4; i < 8; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) cc;
      }
    }

    // tile {-2, 1}
    byteData = ((DataBufferByte) tile[0][2].getDataBuffer()).getData();
    for (i = 4, k = 16; i < 8; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) 0;
      }
    }

    // tile {-1, 1}
    byteData = ((DataBufferByte) tile[1][2].getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) 0;
      }
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) cc;
      }
    }

    // tile {0, 1}
    byteData = ((DataBufferByte) tile[2][2].getDataBuffer()).getData();
    for (i = 4, k = 0; i < 8; i++, k += 16) {
      for (j = 4; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) cc;
      }
    }

    bigTile = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 16, 16,
        64, 4, bandOffset, null);
    ;
    byteData = ((DataBufferByte) bigTile.getDataBuffer()).getData();
    for (i = 0, k = 0; i < 8; i++) {
      for (j = 0; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) cc;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
      for (; j < 16; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) 0;
        byteData[k + 3] = (byte) cc;
      }
    }
    for (; i < 16; i++) {
      for (j = 0; j < 8; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) 0;
      }
      for (; j < 16; j++, k += 4) {
        byteData[k] = (byte) 0;
        byteData[k + 1] = (byte) 0;
        byteData[k + 2] = (byte) cc;
        byteData[k + 3] = (byte) cc;
      }
    }
    checkBoard = new BufferedImage(colorModel, bigTile, false, null);
  }

  // create four tiles {r, g, b, y}
  public WritableRaster copyData(WritableRaster raster) {
    return checkBoard.copyData(raster);
  }

  public ColorModel getColorModel() {
    return checkBoard.getColorModel();
  }

  public Raster getData() {
    return checkBoard.getData();
  }

  public Raster getData(Rectangle rect) {
    return checkBoard.getData(rect);
  }

  public int getHeight() {
    return 16;
  }

  public int getMinTileX() {
    return minX;
  }

  public int getMinTileY() {
    return minY;
  }

  public int getMinX() {
    return -8;
  }

  public int getMinY() {
    return -8;
  }

  public int getNumXTiles() {
    return 3;
  }

  public int getNumYTiles() {
    return 3;
  }

  public Object getProperty(String name) {
    return checkBoard.getProperty(name);
  }

  public String[] getPropertyNames() {
    return checkBoard.getPropertyNames();
  }

  public SampleModel getSampleModel() {
    return checkBoard.getSampleModel();
  }

  public Vector getSources() {
    return null;
  }

  public Raster getTile(int tileX, int tileY) {
    return tile[tileX - minX][tileY - minY];
  }

  public int getTileGridXOffset() {
    return 4;
  }

  public int getTileGridYOffset() {
    return -4;
  }

  public int getTileHeight() {
    return 8;
  }

  public int getTileWidth() {
    return 8;
  }

  public int getWidth() {
    return 16;
  }
}



           
       

Thank with us