How to update size / color / positions of a sphere within the actionPerformed in Java3D

1

This week I started studying Java 3D but I'm having a little problem, I can not move or change the color of objects within strong> actionPerformed Although I can change in other places

To better understand the problem, here is the main class:

Game3D.java

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.swing.Timer;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

public class Game3D extends Applet implements KeyListener, ActionListener  {
    private Timer timer;
    static BranchGroup conteudos = new BranchGroup();

    Luz luz = new Luz();
    Esfera esfera = new Esfera();
    Cores cor = new Cores();


    public Game3D(){
        setLayout(new BorderLayout());

        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        add("Center", canvas);
        canvas.addKeyListener(this);
        System.out.println("0");

        timer = new Timer(100,this);
        timer.start();
        System.out.println("1");

        luz.draw(cor.branco); //luz.draw(cor.intCor(32, 178, 170));
        esfera.draw(0.0f,0.0f,0.0f, cor.amarelo, 0.1f);
        esfera.draw(0.0f,0.0f,0.0f, cor.verde, 0.1f);
        esfera.draw(0.0f,0.0f,0.0f, cor.vermelho, 0.1f);
        System.out.println("2");


        //Criando o Universo e adicionando o Grupo de Conteudos a ele.
        SimpleUniverse universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(conteudos);
        System.out.println("3");
    }

    public static void main( String[] args ) {

        Game3D game = new Game3D();
        game.addKeyListener(game);

        MainFrame mf = new MainFrame(game, 800,480);

    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO LOOP DO ACTION
        System.out.println("4");

        esfera.draw(0.0f,0.0f,0.0f, cor.azul, 0.1f);
    }

} // end of class Hello3d

As we can see I divide the program into some classes, Colors.java , Sphere.java and Luz.java

Sphere.java is responsible for containing the properties of our sphere As (Size, Position, Color):

Sphere.java

import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Sphere;


public class Esfera {

    Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(0.7f, .15f, .15f);

    Appearance ap = new Appearance();
    Sphere sphere = new Sphere(0.5f,ap);

    TransformGroup tg = new TransformGroup();
    Transform3D transform = new Transform3D();

    public Esfera(){
        // Setando propriedades de cores  |  Cor padarão é branca
        ap.setMaterial(new Material(white, black, white, black, 1.0f));
        transform.setTranslation(new Vector3f( 0.0f, 0.0f, 0.0f));
        tg.setTransform(transform);
        tg.addChild(sphere);
        Game3D.conteudos.addChild(tg);
    }

    public void draw(float x, float y, float z, Color3f cor, float size){

        transform.setTranslation(new Vector3f(x,y,z));
        transform.setScale(new Vector3d(size, size, size));

        tg.setTransform(transform);

        ap.setMaterial(new Material(cor, black, cor, black, 1.0f));
        sphere.setAppearance(ap);
    }
}

Then I'm simply instantiating the Sphere.java and invoking its draw function in public Game3D () {} of the main class (Game3D.java) ...

We can then note in lines 45, 46 and 47 respectively:

esfera.draw(0.0f,0.0f,0.0f, cor.amarelo, 0.1f);
esfera.draw(0.0f,0.0f,0.0f, cor.verde, 0.1f);
esfera.draw(0.0f,0.0f,0.0f, cor.vermelho, 0.2f);
//           x  ,  y ,  z ,  Corlor3f   , size

That works perfectly. Here the sphere is printed on the screen with the color red. But when I try to use this method within the actionPerformed (ActionEvent e) {} error ...

How can I change position and color etc. within Action Performed? Or is there another way to create a loop without Action Performed?

If my question is a bit complicated to understand, here is the project available for those who want to take a complete look at the source code: link

    

asked by anonymous 27.07.2014 / 19:40

1 answer

1

I discovered the problem, I needed to add the permissions to be able to move the object.

  tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  ap.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

Adding these two lines to the class Sphere.java just before the line

 Game3D.conteudos.addChild(tg);

Allows the object to receive a new vector3f for its new position as well as change its color tbm.

Additional detail has been to remove / comment the line sphere.setAppearance (ap);

ap.setMaterial(new Material(cor, black, cor, black, 1.0f));
//sphere.setAppearance(ap);

In the draw () function still in the class Sphere.java

    
27.07.2014 / 21:53