How to upgrade the size/color/positions of a sphere within actionPerformed in Java3d

Asked

Viewed 220 times

1

This week I started studying Java 3D but I have a little problem, I can’t move or change the color object within the 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, Java colors., Java sphere. and Luz.

To Java sphere. is responsible for containing the properties of our sphere as(Size, position, color):

Java sphere.

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

So I’m simply instantiating the Java sphere. and invoking its function draw() 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 ball is printed on the screen with the red color. However when I try to use this method within the function actionPerformed(Actionevent and){} Of error...

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

In case my question is a little complicated to understand, here is the project available for those who want to take a full look at the source code: http://www.mediafire.com/download/mmachocp7f8yhcw/Game_3D.rar

OBS: My Goal is to update the Color Changes and Object Placement Sphere.

  • 1

    I advise you to download the Project and test for yourselves, It is easier to understand where the error is. http://www.mediafire.com/download/mmachocp7f8yhcw/Game_3D.rar

1 answer

1


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

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

Adding these two lines in the class Java sphere. just before the line

 Game3D.conteudos.addChild(tg);

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

Additional detail was 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 Java sphere.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.