Gestion des images Raster
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.net.*;
import java.awt.image.BufferedImage;
public class SimpleRaster extends Applet {
static boolean applet = true ;
private String base ;
private ImageComponent2D chercheImageComponent2D(String s) {
if ( applet ) {
try {
String adresse = base+s ;
URL u1 = new URL(adresse);
return((ImageComponent2D) (new TextureLoader(u1,this).getTexture().getImage(0))) ; }
catch (MalformedURLException e) {
return(new ImageComponent2D(ImageComponent2D.FORMAT_RGB,16,16));} }
else
return((ImageComponent2D) (new TextureLoader(s,this).getTexture().getImage(0)));
}
public BranchGroup createSceneGraph(SimpleUniverse u) {
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds;
bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
TransformGroup objTrans0 = new TransformGroup();
Transform3D t3D = new Transform3D();
t3D.setTranslation(new Vector3f(-0.3f,0.3f,0.0f));
objTrans0.setTransform(t3D);
objRoot.addChild(objTrans0);
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
objTrans0.addChild(objTrans);
{ MouseRotate behavior = new MouseRotate(objTrans);
objTrans.addChild(behavior);
behavior.setSchedulingBounds(bounds); }
{ ImageComponent2D ic2D = chercheImageComponent2D("Parquet.jpg");
Raster raster = new Raster(new Point3f(-0.2f,0.2f,-0.2f),
Raster.RASTER_COLOR,
0,0,
128,128,
ic2D,
null);
Shape3D shape = new Shape3D(raster);
objTrans.addChild(shape); }
{ ImageComponent2D ic2D = chercheImageComponent2D("Bois.jpg");
Raster raster = new Raster(new Point3f(-0.15f,0.1f,0.2f),
Raster.RASTER_COLOR,
0,0,
128,128,
ic2D,
null);
Shape3D shape = new Shape3D(raster);
objTrans.addChild(shape); }
objRoot.compile();
return objRoot;
}
public void lanceJava3D() {
setLayout(new BorderLayout());
GraphicsConfiguration config;
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center",c);
SimpleUniverse u = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(u);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void init() {
if ( applet ) {
URL url = this.getCodeBase();
base = url.toString();
lanceJava3D(); }
}
public SimpleRaster() {
if ( !applet ) {
lanceJava3D(); }
}
public static void main(String[] args) {
applet = false ;
new MainFrame(new SimpleRaster(),300,250);
}
}
Remarques,erreurs
nicolas.janey@univ-fcomte.fr