Changement d'apparence sur une Sphere
Bonjour,
j'ai réalisé une petite appilcation 3D où j'affiche juste une sphere.
Quand je veu modifier la couleur de cette sphere en utilisant la methode setAppearance(Appearance) à l'execution ca me donne l'exception"CapabilityNotSetException" car il faut la capabilités "ALLOw_APPEARANCE_WRITE".
J'ai pourtant appelé la methode setCapability(ENABLE_APPEARANCE_MODIFY) qui donne les capabilités "ALLOw_APPEARANCE_READ" et "ALLOw_APPEARANCE_WRITE" mais sa ne change rien.
Pouvez vous m'aidez svp, c'est tres important pour moi
Code:
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
|
// classes Java standart
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// classes Java 3D
import com.sun.j3d.utils.universe.SimpleUniverse;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
public class testApparence extends Frame implements WindowListener{
private TransformGroup Transform;
private Sphere ball;
public testApparence(){
this.addWindowListener(this);
setLayout(new BorderLayout());
setBackground(Color.WHITE);
Transform = new TransformGroup();
Transform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
Transform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance defaultAppearance = this.newAppearance(0.3f,0.2f,1.0f);
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
add("Center", canvas3D);
BranchGroup UneScene=new BranchGroup();
UneScene.setCapability(UneScene.ALLOW_DETACH);
/* *******************/
ball =new Sphere(0.1f,defaultAppearance);
//Specifies that the ALLOW_APPEARANCE_READ and ALLOW_APPEARANCE_WRITE bits are to be set on the generated geometry's Shape3D nodes.
ball.setCapability(ball.ENABLE_APPEARANCE_MODIFY);
TransformGroup tmp = new TransformGroup();
tmp.setCapability(tmp.ALLOW_TRANSFORM_READ);
tmp.setCapability(tmp.ALLOW_TRANSFORM_WRITE);
tmp.addChild(ball);
Transform.addChild(tmp);
/* ******************/
// Creation comportement rotation a la souris
MouseRotate rotateMouse = new MouseRotate(Transform);
rotateMouse.setSchedulingBounds(new BoundingSphere());
UneScene.addChild(rotateMouse);
// Creation comportement deplacement a la souris
MouseTranslate translateMouse = new MouseTranslate(Transform);
translateMouse.setSchedulingBounds(new BoundingSphere());
UneScene.addChild(translateMouse);
// Creation comportement zoom a la souris
MouseZoom zoomMouse = new MouseZoom(Transform);
zoomMouse.setSchedulingBounds(new BoundingSphere());
UneScene.addChild(zoomMouse);
/* ******************/
UneScene.addChild(Transform);
UneScene.compile();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.addBranchGraph(UneScene);
}
public void changeApparence(){
Appearance player1Appearance = this.newAppearance(1.0f,0.1f,0.1f);
ball.setAppearance(player1Appearance);
}
public static void main(String[] args){
testApparence ta = new testApparence();
ta.setSize(800,800);
ta.setVisible(true);
ta.changeApparence();
}
public Appearance newAppearance(float r,float v,float b){
// on crée une apparence avec une couleur et une couche alpha
Appearance app0=new Appearance();
app0.setColoringAttributes(new ColoringAttributes(new Color3f(r,v,b),ColoringAttributes.SHADE_GOURAUD));
return app0;
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
} |