# Java3D Game_(die erste)



## Developer_X (28. Jun 2009)

Hi, vielleicht ist in meinem Programm ein kleiner Bug den ich nicht bemerke, aber

was mache ich denn am folgenden Programm falsch, der ball bewegt sich nicht wieso?


```
package Game1;

import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Game3D1 extends JFrame implements KeyListener
{
	//Attributes
	
	//Dimension
	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	
	//Appearance
    Appearance red = new Appearance();
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);

	//Rest
	float x, y;
	Transform3D move;
	TransformGroup Move;
	
	public Game3D1()
	{
		super("Game3D1");
		setSize(d.width/2,d.height/2);
		setLayout(new BorderLayout());
		
        BranchGroup scene = createtheScene();       
		 GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
         Canvas3D canvas = new Canvas3D(config);
         add("Center", canvas);       
        SimpleUniverse  universe = new SimpleUniverse(canvas);       
         universe.addBranchGraph(scene);
         
		setVisible(true);
	}
	public BranchGroup createtheScene()
	{
		BranchGroup X = new BranchGroup();
		
		  Material redProps = new Material();
	      redProps.setAmbientColor(1.0f,0.0f,0.0f); 
	      red.setMaterial(redProps);       
	         
		move = new Transform3D();
		move.setTranslation(new Vector3f(x,y,-5));
		Move = new TransformGroup();
		Move.setTransform(move);
		Move.addChild(new Sphere(0.1f, red));
		Move.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		X.addChild(Move);

       Color3f lightColor = new Color3f(.3f,.3f,.3f);
       AmbientLight ambientLight= new AmbientLight(lightColor);
       ambientLight.setInfluencingBounds(bounds);
       X.addChild(ambientLight);
       DirectionalLight directionalLight = new DirectionalLight();
       directionalLight.setColor(lightColor);
       directionalLight.setInfluencingBounds(bounds);
       X.addChild(directionalLight);       

		return X;
	}
	
	public static void main(String[]args)
	{
		Game3D1 gm3d1 = new Game3D1();
		
		gm3d1.addKeyListener(gm3d1);
	}
	
	public void keyPressed(KeyEvent arg0) 
	{		
		if(arg0.getKeyCode()==KeyEvent.VK_UP)
		{
			y = y + 0.1f;
			move.setTranslation(new Vector3f(x,y,-5));
			Move.setTransform(move);
		}
		else if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
		{
			y = y - 0.1f;
			move.setTranslation(new Vector3f(x,y,-5));
			Move.setTransform(move);
		}
		else if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
		{
			x = x - 0.1f;
			move.setTranslation(new Vector3f(x,y,-5));
			Move.setTransform(move);
		}
		else if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
		{
			x = x + 0.1f;
			move.setTranslation(new Vector3f(x,y,-5));
			Move.setTransform(move);
		}	
	}
	public void keyReleased(KeyEvent arg0) 
	{		
	}
	public void keyTyped(KeyEvent arg0) 
	{
	}
}
```


----------



## Developer_X (28. Jun 2009)

funktionierts etwa bei euch?


----------



## Noctarius (29. Jun 2009)

Komisches Anfängerbuch hast du...


----------

