My applet doesn’t work… (10 posts)

  • Profile picture of triss211 triss211 said 3 months ago:

    Hi
    I’m working on a Connect 4 online game for my studies.
    I’m trying to make an applet with jmonkey to integrate it in the game website.
    The applet worked yersteday, but tonight it mysteriously does not want to start, whereas I didn’t make hudge changes in my code.
    The webstart doesn’t work too but the .jar and windows .exe runs fine.
    I’m a kind of newbie with jMonkey and I really don’t see where the problem is.
    Could you help me ?

    my whole jme project is here (dist and assets included) :

    http://puissance4.olympe-network.com/Power4.zip

    And here, just my 3 java classes.

    package mygame;
    
    //classes du moteur 3D
    import com.jme3.app.SimpleApplication;
    import com.jme3.material.Material;
    import com.jme3.math.ColorRGBA;
    import com.jme3.math.Quaternion;
    import com.jme3.math.Vector3f;
    import com.jme3.renderer.RenderManager;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.shape.Box;
    import com.jme3.scene.Spatial;
    import com.jme3.texture.Texture;
    import com.jme3.material.RenderState.BlendMode;
    import com.jme3.renderer.queue.RenderQueue.Bucket;
    import com.jme3.light.DirectionalLight;
    
    /**
     * test
     * @author normenhansen
     */
    // La classe Power4 herite de la classe Simple application de jMonkey
    public class Power4 extends SimpleApplication {
    
        //l'application, à envoyer aux autres objets du puissance 4 lors de leur creation
        public static  Power4 app;
        //grille
        protected Grid grid;
    
        protected int count=0;
    
        public static void main(String[] args) {
            app = new Power4();
            app.setShowSettings(false);
            app.start();
    
        }
    
        @Override
        public void simpleInitApp() {
                grid=new Grid( app);
    
                grid.addToken(0,"red");
    
                DirectionalLight sun = new DirectionalLight();
                sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
                sun.setColor(ColorRGBA.White);
                rootNode.addLight(sun);
    
        }
    
        @Override
        // la methode simpleUpdate est appellée à chaque boucle de l'application
        //tpf est un ratio qui varie en fonction du nombre de boucle par seconde
        //=>il permet de faire bouger les objets à la meme vitesse quelque soit le fps
        public void simpleUpdate(float tpf) {
            grid.update(tpf);
            if (count<20)
            {
                if (grid.addToken(count%7,"yellow")){
                        count++;}
            }
            else{
    
                grid.rotate(tpf);
            }
    
        }
    
        @Override
        public void simpleRender(RenderManager rm) {
            //TODO: add render code
        }
    }
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     *
     * */
    package mygame;
    
    import com.jme3.material.Material;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.Spatial;
    import com.jme3.texture.Texture;
    import com.jme3.material.RenderState.BlendMode;
    import com.jme3.scene.Node;
    import com.jme3.asset.AssetManager;
    import com.jme3.math.ColorRGBA;
    import com.jme3.math.Quaternion;
    import com.jme3.math.Vector3f;
    
    /**
     *
     * @author Dromadaire fuchia
     */
    public class Grid{
    
        //modèle 3D de la grille
        protected Geometry model;
    
        //variables propres à l'application
        protected Node rootNode;
        protected AssetManager assetManager;
    
        //tableau de jetons
        protected Token[][] tokens= new Token[7][6]; 
    
        //constante : taille d'une case/2
        public final float RADIUS2=0.525f;
    
        //dernier jeton ajouté
        protected Token lastToken;
    
        //pivot nécessaire à la rotation de la grille
        protected Node node;
    
        //Constructeur
        Grid(Power4 app){
            //récupération des variables de l'application
            rootNode=app.getRootNode();
            assetManager=app.getAssetManager();
    
          //Chargement du modèle et de la texture, avec transparence
            model = (Geometry) assetManager.loadModel("Models/test.j3o");
            Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            Texture tex = assetManager.loadTexture("Models/textgrille.png");
    
            mat2.setTexture("ColorMap", tex);
            mat2.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
            model.setMaterial(mat2);
             model.move(0,-3f,0);
            node=new Node();
            node.attachChild(model);
            rootNode.attachChild(node);    
    
        }
    
        public void update(float tpf){
            if (lastToken!=null){
                lastToken.update(tpf);
            }
        }
    
        public boolean rotate(float tpf){
            node.rotate(0,tpf,0);
            return true;
        }
    
        public boolean addToken(int column,String color){
          if ((lastToken==null )|| (lastToken.getStopped())){
                int i=-1;
                 while((i<=4)&&(tokens[column][i+1]==null)){
                     i++;
                  }
                  if (i!=-1)
                  {
                    tokens[column][i]=new Token(this,column,i,color);
                    lastToken=tokens[column][i];
                   return true;
                 }
                 else
                 {
                    return false;
                }
          }
          else return false;
    
        }
    
        public Node getNode(){
            return node;
        }
         public Node getRootNode(){
            return rootNode;
        }
        public AssetManager getAssetManager(){
            return assetManager;
        }
    
    }
    
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package mygame;
    
    import com.jme3.scene.shape.Sphere;
    import com.jme3.material.Material;
    import com.jme3.scene.Geometry;
    import com.jme3.scene.Spatial;
    import com.jme3.texture.Texture;
    import com.jme3.math.ColorRGBA;
    import com.jme3.math.Vector3f;
    import com.jme3.scene.Node;
    import com.jme3.texture.Texture;
    import com.jme3.util.TangentBinormalGenerator;
    
    /**
     *
     * @author Dromadaire fuchia
     */
    public class Token {
        public final float RADIUS1=0.35f;
        public final float RADIUS2=0.525f;
    
        //grille a laquelle appartient le jeton
        protected Grid grid;
    
        // couleur du jeton
        protected String color;
    
        //modele 3D du jeton
        protected Geometry model;
    
        //booléen : est-ce que le jeton a fini de tomber ou pas
       protected boolean stopped;
    
       protected int line;
    
        Token(Grid grid, int column, int line, String color){
    
              this.color=color;
              this.grid=grid;
              this.line=line;
              stopped=false;
    
        Sphere s = new Sphere(8,8, RADIUS1);
        model= new Geometry("jeton", s);
    
        //effets de lumière...
        s.setTextureMode(Sphere.TextureMode.Projected);
        TangentBinormalGenerator.generate(model);
        Material mat_lit = new Material(grid.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md");
        mat_lit.setTexture("DiffuseMap", grid.getAssetManager().loadTexture("Textures/Pond.jpg"));
        mat_lit.setTexture("NormalMap", grid.getAssetManager().loadTexture("Textures/Pond_normal.png"));
        mat_lit.setBoolean("UseMaterialColors",true);    
    
        if (color=="red")
        {
                    mat_lit.setColor("Specular",new ColorRGBA(1f,0.7f,0.5f,1f));
                    mat_lit.setColor("Diffuse",new ColorRGBA(1f,0.2f,0.2f,1f) );
        }
        else if (color=="yellow")
        {
             mat_lit.setColor("Specular",new ColorRGBA(0.9f,0.9f,0.5f,1f));
             mat_lit.setColor("Diffuse",new ColorRGBA(0.8f,0.7f,0.1f,1f) );
    
        }
        else System.err.println("Erreur : couleur du jeton non conforme");
    
        mat_lit.setFloat("Shininess", 5f); // [1,128]    
    
             model.setMaterial(mat_lit);
          // Rotate it a bit
         model.move(-6*RADIUS2+(float)(column*2*RADIUS2),5*RADIUS2,0);  
    
         grid.getNode().attachChild(model);   
    
        }
    
        public void update(float tpf){
    
            if (!stopped){
                if (model.getLocalTranslation().getY()>(5*RADIUS2-2*line*RADIUS2)){
                    if (tpf>0.1f){tpf=0.1f;}
                    model.move(0,(float)-6*tpf,0);
                }
                else
                    stopped=true;
    
            }
        }
    
        public boolean getStopped(){
            return stopped;
        }
    
    }
    

    PS : I’m sorry for my poor English, it is not my native language.
    You can see my french comments in my code :p

  • Profile picture of triss211 triss211 said 3 months ago:

    No answer ? :?

  • Profile picture of kbender88 kbender8844p said 3 months ago:

    what sort of error messages are you getting?

  • Profile picture of triss211 triss211 said 3 months ago:

    It’s the applet which doesn’t run, so I haven’t any error messages : the java app load the .jar files and other stuff, then I only have a white page. Maybe you know a way to obtain error messages from the applet ?

  • Profile picture of Momoko_Fan Momoko_Fan371p said 3 months ago:

    Does it work when running as a regular desktop app?
    If you’re using Windows, you can view the Java console by right clicking the Java system tray icon and selecting view console

  • Profile picture of triss211 triss211 said 3 months ago:

    Thanks for your anwser, i didn’t know we could see java console during a web applet execution.
    it works when running as a regular desktop app but doesn’t work running as a web applet or a webstart.
    I get the following error message during the web app execution :

    Exception in thread “LWJGL Renderer Thread” java.lang.NullPointerException
    at mygame.Grid.(Grid.java:49)
    at mygame.Power4.simpleInitApp(Power4.java:49)
    at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)
    at java.lang.Thread.run(Unknown Source)

  • Profile picture of Momoko_Fan Momoko_Fan371p said 3 months ago:

    It happens in your own class (mygame.Grid) and not in jME3
    Make sure you’re not doing any initialization in the main() method as it won’t be called in an applet.

  • Profile picture of triss211 triss211 said 2 months, 4 weeks ago:
    public class Power4 extends SimpleApplication {
    
        public static  Power4 app;
        protected Grid grid;
        protected int count=0;
    
        public static void main(String[] args) {
            app = new Power4();
            app.setShowSettings(false);
            app.start();
    
        }
    
        @Override
        public void simpleInitApp() {
                grid=new Grid( app);

    It seems that the pointer app I send to the grid construcor is null during a web applet execution and correct in desktop mode… But I initialise it in the main of Power4.java which is the main class, so I really don’t see why It’s different between the two kind of execution
    [EDIT] Oh, maybe you mean really no main method of any classes are called in java web app…
    So ho can I send the jmonkey Application to the other classes if i need it ?

  • Profile picture of Momoko_Fan Momoko_Fan371p said 2 months, 4 weeks ago:

    Your application’s simpleInitApp() is still called so you can create an AppState or similar that can reference it. And yes in your case main() won’t be called when running as applet

  • Profile picture of triss211 triss211 said 2 months, 4 weeks ago:

    My app works fine now :)
    Thanks for your precious help !