Rechercher : dans
Par :

[java3d] Applet et Swing

saswing, le 16 avr 2009 à 15:44:02 
 Signaler ce message aux modérateurs

Bonjour,

Je réalise un Applet avec Netbeans (javax.swing.JApplet) qui affiche juste deux JPanel. Je souhaite intégrer du java3d dans un des JPanel.

J'ai par conséquent deux classes :

* JPanelCanvas3D qui contient mon Canvas3D et qui dérive de JPanel
* NewJApplet qui est mon Applet pour tester avec 1 JPanel et un JPanelCanvas3D


Pour arriver à mes fins, je me suis très largement inspiré de ce site :
http://deven3d.free.fr/java3d/chap10.htm

Ce qui fonctionne :

* L'Applet se lance correctement et mes deux JPanel s'affichent.
* Il n'y a aucune erreurs à la compilation ni au lancement de l'Applet


Ce qui ne fonctionne pas :

* Mon JPanelCanvas3D n'apparaît pas, j'ai juste mes 2 JPanel.


J'ai lu plusieurs posts sur des forums où il était mentionné que la cohabitation de tout ce monde était complexe mais pas impossible.

Références :

* http://deven3d.free.fr/java3d/chap10.htm
* http://www.eteks.com/coursjava/java3D.html
* http://www.jguru.com/faq/view.jsp?EID=87438
* http://java.sun.com/products/jfc/tsc...ing/index.html
* https://java3d.dev.java.net/ et biens d'autres...


J'ai de plus bien surcharger la méthode setVisible().

Question : Qu'est-ce que j'ai raté ? Avez-vous un petit bout de code ou un lien vers une documentation adéquate ?

J'ai également mis en pièce jointe le code original dont je me suis inspiré et qui fonctionne JTabbedPaneCanvas3D.java

Voici les class en question :
NewJApplet

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJApplet.java
 *
 * Created on 16 avr. 2009, 12:40:10
 */

package mypackage;
// Etape 1 :
// Importation des packages Java 2


public class NewJApplet extends javax.swing.JApplet {

    /** Initializes the applet NewJApplet */
    @Override
    public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                    jPanel1.setVisible(true);
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new mypackage.JPanelCanvas3D();
        jPanel2 = new javax.swing.JPanel();

        jPanel1.setVisible(true);
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 234, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>

/*
    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration
*/
   // private mypackage.JPanelCanvas3D jPanel1;
//    private uk.ac.ed.java3d_handler.Java3DPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private mypackage.JPanelCanvas3D jPanel1;

   
}

JPanelCanvas3D
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mypackage;


import java.awt.*;
import javax.swing.*;

// Etape 2 :
// Importation des packages Java 3D

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;


public  class JPanelCanvas3D extends javax.swing.JPanel {

    Canvas3D canvas3D = null;

    public JPanelCanvas3D() {

      this.setLayout(new BorderLayout());

      // Etape 3 :
      // Creation du Canvas 3D
      canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
      this.add(canvas3D, BorderLayout.CENTER);

      // 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);
    }

    /**
     * Creation de la scene 3D qui contient tous les objets 3D
     * @param sphereColor couleur de la sphere
     * @return scene 3D
     */
    public BranchGroup createSceneGraph() {
      // Creation de l'objet parent qui contiendra tous les autres objets 3D
      BranchGroup parent = new BranchGroup();

      // Creation du materiau qui possede une couleur d'emission blanche
      Material emissiveColor = new Material();
      emissiveColor.setEmissiveColor(new Color3f(Color.blue));

      // Creation de l'apparence qui contient le materiau precedemment cree
      Appearance appearance = new Appearance();
      appearance.setMaterial(emissiveColor);

      // Creation de la sphere pour laquelle on applique l'apparence contenant
      // la couleur d'emission
      Sphere sphere = new Sphere(0.5f, Primitive.GENERATE_NORMALS, 64);
      sphere.setAppearance(appearance);
      parent.addChild(sphere);

      return parent;
    }
        /**
     * Methode surchargee de JPanel afin de synchroniser l'affichage du
     * canvas 3D avec celui du JPanel
     * @param v boolean
     */
    public void setVisible(boolean v) {
      super.setVisible(v);
      canvas3D.setVisible(v);
    }


  }

Meilleures réponses pour « [java3d] Applet et Swing » dans :
Télécharger Applet deroule_menu Voir Dans les pages web, les menus sont souvent présentés en détails, c’est à dire que vous voyez tous les liens affichés au premier plan. Ceci gâche de l’espace et n’est pas très esthétique. Applet déroule_menu est un outil permettant...
Télécharger Applet menu_animé VoirPour vos logiciels ou pour votre site web, plus c’est animé, plus les utilisateurs apprécient. Applet menu_animé est un outil tout simple permettant de créer un menu animé. Le programme propose de nombreux paramètres redéfinissables tels que...
Télécharger Applet link_bouton VoirAméliorer l’interface graphique du menu de vos logiciels ou dans vos pages web. Applet link_bouton est un programme de création de boutons dans le style XP. Il remplacera vos boutons monotones par des boutons en couleurs avec polices et images,...