Results 1 to 4 of 4
- 06-29-2011, 01:53 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
Move objects(sphere) by click on buttons(right,left,top,bottom) using java3d
Hi guys.
I new in java3d. But I ave a job to complete. please help me.
I have a frame application, within it, have a sphere which is moved by mouse translate and have four buttons - Right, left, top and bottom.
But I can't figure it out how can I do it. Code is --
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.GraphicsConfiguration;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom;
/**
*
* @author Mitajit Samanta
*/
public class backgroundImage extends JFrame implements ActionListener {
JButton right,left,top,bottom;
//Vector3f lightDir;
public backgroundImage(){
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3d = new Canvas3D(config);
add(canvas3d,BorderLayout.CENTER);
right = new JButton("RIGHT");
left = new JButton("LEFT");
top = new JButton("TOP");
bottom = new JButton("BOTTOM");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(right);
panel.add(left);
panel.add(top);
panel.add(bottom);
add(panel,BorderLayout.SOUTH);
right.addActionListener(this);
BranchGroup scene = createSceneGraph(canvas3d);
SimpleUniverse universe = new SimpleUniverse(canvas3d);
universe.getViewingPlatform().setNominalViewingTra nsform();
universe.addBranchGraph(scene);
}
public void actionPerformed(ActionEvent e){
String command = e.getActionCommand();
JOptionPane.showMessageDialog(null,command);
//String s = getAlignmentAxis(lightDir);
}
public void fetchCoordinates(){
}
public Appearance createAppearance(){
Appearance appearance = new Appearance();
//ColoringAttributes color = new ColoringAttributes();
//color.setColor(0f,0f,1.0f);
//appearance.setColoringAttributes(color);
Color3f ambientColor = new Color3f(1.0f,0.0f,0.0f);
Color3f diffuseColor = new Color3f(1.0f,0.0f,0.0f);
Color3f specularColor = new Color3f(1.0f,1.0f,1.0f);
Color3f emissiveColor = new Color3f(0.0f,0.0f,0.0f);
float shininess = 20.0f;
appearance.setMaterial(new Material(ambientColor,diffuseColor,specularColor,e missiveColor,shininess));
return appearance;
}
public BranchGroup createSceneGraph(Canvas3D canvas3d){
//Create the root of the Bramch Graph
BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds = new BoundingSphere(new Point3d(),10.0);
Color3f ambientColor = new Color3f(0.2f,0.2f,0.2f);
AmbientLight ambientLight = new AmbientLight(ambientColor);
ambientLight.setInfluencingBounds(new BoundingSphere(new Point3d(),100.0));
Color3f lightColor = new Color3f(1.0f,1.0f,1.0f);
Vector3f lightDir = new Vector3f(-1.0f,-1.0f,-1.0f);
DirectionalLight light = new DirectionalLight(lightColor,lightDir);
light.setInfluencingBounds(new BoundingSphere(new Point3d(),100.0));
objRoot.addChild(ambientLight);
objRoot.addChild(light);
Transform3D tf3 = new Transform3D();
TransformGroup mainGroup = new TransformGroup(tf3);
objRoot.addChild(mainGroup);
Transform3D tf3Background = new Transform3D();
TransformGroup groupBackground = new TransformGroup(tf3Background);
mainGroup.addChild(groupBackground);
//objRoot.addChild(TGgroup);
/**
* The following 7 lines of code is for set an image as background.
*/
TextureLoader myLoader = new TextureLoader("D:\\practice\\my_practice\\src\\pra ctice_28062011\\bluelake.jpg",this);
ImageComponent2D myImage = myLoader.getImage();
BoundingSphere worldBounds = new BoundingSphere(new Point3d(),100.0);
Background back = new Background();
back.setApplicationBounds(worldBounds);
back.setImage(myImage);
groupBackground.addChild(back);
/**
* The following 7 commented line of code are for color canvas background, here color is white
*/
/*BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),1000.0);
Color3f color = new Color3f(1f,1f,1f);
Background back = new Background();
back.setColor(color);
back.setCapability(Background.ALLOW_COLOR_WRITE);
back.setApplicationBounds(worldBounds);
objRoot.addChild(back);*/
Transform3D tf3Sphere = new Transform3D();
//The following two lines are for set the co-ordinate of the sphere on the canvas
/*Vector3f vector1 = new Vector3f(-0.5f, -0.1f, -0.5f);
tf3Sphere.set(vector1);*/
TransformGroup groupSphere = new TransformGroup(tf3Sphere);
Sphere sphere = new Sphere(0.1f,Sphere.GENERATE_NORMALS,createAppearan ce());
groupSphere.addChild(sphere);
mainGroup.addChild(groupSphere);
mainGroup.setCapability(TransformGroup.ALLOW_TRANS FORM_WRITE);
mainGroup.setCapability(TransformGroup.ALLOW_TRANS FORM_READ);
//Setup the mouse rotation behaviour
MouseRotate mr = new MouseRotate();
mr.setTransformGroup(mainGroup);
mr.setSchedulingBounds(bounds);
mr.setFactor(0.005f);
mainGroup.addChild(mr);
//Setup the mouse translation behaviour
MouseTranslate mt = new MouseTranslate();
mt.setTransformGroup(mainGroup);
mt.setSchedulingBounds(bounds);
mt.setFactor(0.001f);
mainGroup.addChild(mt);
// set up the mouse soom behavior
MouseZoom mz = new MouseZoom();
mz.setTransformGroup(mainGroup);
mz.setSchedulingBounds(bounds);
mz.setFactor(0.001f);
mainGroup.addChild(mz);
MouseWheelZoom mwz = new MouseWheelZoom();
mwz.setTransformGroup(mainGroup);
mwz.setSchedulingBounds(bounds);
mwz.setFactor(0.001f);
mainGroup.addChild(mwz);
return objRoot;
}
public static void main(String args[]){
backgroundImage frame = new backgroundImage();
frame.setSize(new Dimension(700,700));
frame.setTitle("Practice from start");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}
When click on right button the sphere(ball) should move right , when click on left button the mouse should move left side and others will doing same as their name suggests.
Please help me guys.
Thank you.
- 07-01-2011, 10:28 PM #2
Hello,
At first some housekeeping:
- Classnames (backgroundImage) should start with a capital character
- You should use codetags and proper indentation
That said, now your problem:
- I made two more buttons (FRONT and BACK. It is Java3D, not 2D
)
- Only "right" had an actionlistener. I added five more.
- I changed the ActionPerformed. (If you had trouble differentiating between the buttons, you can do that with code like
but I did't use that.)Java Code:if(e.getSource()==right)JOptionPane.showMessageDialog(null,"rechts");)
It became:
I made the declarations of maingroup and tf3 global. Otherwise I coudn't see them from the new translate().Java Code:public void actionPerformed(ActionEvent e){ String command = e.getActionCommand(); translate( command );//new method }
Translate() became like this:
Guess what? It worked!Java Code:private void translate( String direction ){ float step = .05f; Transform3D myMove = new Transform3D(); if( direction.equals("RIGHT") ){ System.out.println( "rechts" ); myMove.setTranslation( new Vector3f(step, 0.0f , 0.0f ) ); tf3.mul(myMove); mainGroup.setTransform( tf3 ); } if( direction.equals("LEFT") ){...and so on for the other buttons
Last edited by Jodokus; 07-04-2011 at 12:56 PM. Reason: ActionListener to A-Performed
No bug ever had to calculate its fitnessfunction.
- 07-04-2011, 08:15 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 59
- Rep Power
- 0
Thanks a lot for ur help.
Again thanks thanks thanks, it solved my problem.
- 07-04-2011, 12:50 PM #4
Similar Threads
-
reverse of (control + left click)
By nocturnalhacker in forum IntelliJ IDEAReplies: 0Last Post: 03-22-2011, 07:30 AM -
Cannot click on the buttons
By pabloma2002 in forum CLDC and MIDPReplies: 0Last Post: 01-24-2011, 04:35 PM -
gridBagLayout anchor ... wanna move all components to top left
By alinaqvi90 in forum AWT / SwingReplies: 16Last Post: 11-17-2010, 01:02 PM -
Problem with click to move ball algorithm
By Laythe in forum Java AppletsReplies: 1Last Post: 12-19-2009, 12:00 AM -
How to make the menu bar objects appears from right to left
By JavaBean in forum AWT / SwingReplies: 3Last Post: 07-19-2007, 11:12 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks