Bonjour à tous d'abord,
débutant en Java, je suis en train d'essayer d'afficher un canvas3D dans un JDesktopPane de ma JFrame. Mon but serait d'arriver à changer la couleur du cube 3D (contenu dans le canvas3D) à partir de 3 JSlider (Rouge, Vert et Bleu).
Or, quand je lance l'application, je vois bien mes 3 sliders (plus le JEditorPane que j'ai ajouté uniquement pour voir si le changement de couleur fonctionnait...) mais pas ma scène en 3D (pas d'erreur à la compilation)... :-(
Bref, si quelqu'un avait une petite idée de mon erreur (qui doit être grossière je pense, vu mon inexpérience en Java), je l'en remercie énormément d'avance !!!
Ci-après le code :
// Etape 1 :
// Importation des packages Java 2
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Panel;
import javax.swing.JSlider;
import javax.swing.JSplitPane;
import javax.swing.JEditorPane;
import javax.swing.JInternalFrame;
import javax.swing.JCheckBox;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
// Etape 2 :
// Importation des packages Java 3D
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.loaders.objectfile.*;
import com.sun.j3d.loaders.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.geometry.Box;
import javax.media.j3d.*;
import javax.media.j3d.Canvas3D;
import javax.vecmath.*;
public class Bof extends JFrame {
private JDesktopPane jDesktopPane = null;
private JSlider jSlider = null;
private JSlider jSlider1 = null;
private JSlider jSlider2 = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private JEditorPane jEditorPane = null;
private Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
/**
* This is the default constructor
*/
public Bof() {
super("ONK ONK ONK !!!");
initialize();
setDefaultCloseOperation(EXIT_ON_CLOSE);
jSlider.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
sliderMouseDraggedCB();
}
});
jSlider1.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
sliderMouseDraggedCB();
}
});
jSlider2.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
sliderMouseDraggedCB();
}
});
// Etape 4 :
// Creation d'un objet SimpleUniverse
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// Etape 5 :
// Positionnement du point d'observation pour avoir une vue correcte de la
// scene 3D
simpleU.getViewingPlatform().setNominalViewingTransform();
// Etape 6 :
// Creation de la scene 3D qui contient tous les objets 3D que l'on veut visualiser
BranchGroup scene = createSceneGraph();
// Etape 7 :
// Compilation de la scene 3D
scene.compile();
// Etape 8:
// Attachement de la scene 3D a l'objet SimpleUniverse
simpleU.addBranchGraph(scene);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/**
* Creation de la scene 3D qui contient tous les objets 3D
* @return scene 3D
*/
public BranchGroup createSceneGraph() {
// Creation de l'objet parent qui contiendra tous les autres objets 3D
BranchGroup parent = new BranchGroup();
// Creation d'une lumiere ambiante de couleur blanche
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100);
Light ambientLight = new AmbientLight(new Color3f(Color.white));
ambientLight.setInfluencingBounds(bounds);
parent.addChild(ambientLight);
// Creation d'une lumiere directionnelle de couleur blanche
Light directionalLight = new DirectionalLight(
new Color3f(Color.white),
new Vector3f(1, -1, -1));
directionalLight.setInfluencingBounds(bounds);
parent.addChild(directionalLight);
// Creation du groupe de transformation sur lequel vont s'appliquer les
// comportements
TransformGroup mouseTransform = new TransformGroup();
// Le groupe de transformation sera modifie par le comportement de la
// souris
mouseTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
mouseTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
// Creation comportement rotation a la souris
MouseRotate rotate = new MouseRotate(mouseTransform);
rotate.setSchedulingBounds(new BoundingSphere());
parent.addChild(rotate);
// Creation comportement deplacement a la souris
MouseTranslate translate = new MouseTranslate(mouseTransform);
translate.setSchedulingBounds(new BoundingSphere());
parent.addChild(translate);
// Creation comportement zoom a la souris
MouseZoom zoom = new MouseZoom(mouseTransform);
zoom.setSchedulingBounds(new BoundingSphere());
parent.addChild(zoom);
Background background = new Background(new Color3f(Color.white));
background.setApplicationBounds(new BoundingBox());
parent.addChild(background);
// Chargement de l'objet Wavefront et ajout au graphe de la scene
mouseTransform.addChild(new Box(0.5f, 0.5f, 0.2f, null));
parent.addChild(mouseTransform);
return parent;
}
private void sliderMouseDraggedCB() {
float Rcolor, Vcolor, Bcolor;
Rcolor = jSlider.getValue() / 100f;
Vcolor = jSlider1.getValue() / 100f;
Bcolor = jSlider2.getValue() / 100f;
jEditorPane.setBackground(new java.awt.Color(Rcolor,Vcolor,Bcolor));
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(600, 400);
this.setContentPane(getJDesktopPane());
this.setTitle("JFrame");
}
/**
* This method initializes jDesktopPane
*
* @return javax.swing.JDesktopPane
*/
private JDesktopPane getJDesktopPane() {
if (jDesktopPane == null) {
jLabel2 = new JLabel();
jLabel2.setBounds(new java.awt.Rectangle(152,333,36,16));
jLabel2.setText("Bleu");
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1 = new JLabel();
jLabel1.setText("Vert");
jLabel1.setSize(new java.awt.Dimension(36,16));
jLabel1.setLocation(new java.awt.Point(152,307));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel = new JLabel();
jLabel.setBounds(new java.awt.Rectangle(151,279,38,16));
jLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel.setText("Rouge");
jDesktopPane = new JDesktopPane();
jDesktopPane.add(getJSlider(), null);
jDesktopPane.add(getJSlider1(), null);
jDesktopPane.add(getJSlider2(), null);
jDesktopPane.add(jLabel, null);
jDesktopPane.add(jLabel1, null);
jDesktopPane.add(jLabel2, null);
jDesktopPane.add(getJEditorPane(), null);
jDesktopPane.add(getCanvas3D(), null);
}
return jDesktopPane;
}
/**
* This method initializes jSlider
*
* @return javax.swing.JSlider
*/
private JSlider getJSlider() {
if (jSlider == null) {
jSlider = new JSlider();
jSlider.setBounds(new java.awt.Rectangle(189,279,200,16));
}
return jSlider;
}
/**
* This method initializes jSlider1
*
* @return javax.swing.JSlider
*/
private JSlider getJSlider1() {
if (jSlider1 == null) {
jSlider1 = new JSlider();
jSlider1.setBounds(new java.awt.Rectangle(189,306,200,16));
}
return jSlider1;
}
/**
* This method initializes jSlider2
*
* @return javax.swing.JSlider
*/
private JSlider getJSlider2() {
if (jSlider2 == null) {
jSlider2 = new JSlider();
jSlider2.setSize(new java.awt.Dimension(200,16));
jSlider2.setLocation(new java.awt.Point(189,333));
}
return jSlider2;
}
/**
* This method initializes jEditorPane
*
* @return javax.swing.JEditorPane
*/
private JEditorPane getJEditorPane() {
if (jEditorPane == null) {
jEditorPane = new JEditorPane();
jEditorPane.setBackground(new java.awt.Color(127,127,127));
jEditorPane.setBounds(new java.awt.Rectangle(30,281,106,69));
}
return jEditorPane;
}
private Canvas3D getCanvas3D() {
if (canvas3D == null) {
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
canvas3D.setSize(new java.awt.Dimension(000,100));
canvas3D.setLocation(new java.awt.Point(10,10));
}
return canvas3D;
}
public static void main(String [] args) {
Bof tv = new Bof();
tv.show();
}
}