My Code i must say its not very impressive or OOD, excuse it. I want you
to find alternative soluton.
On Tue, 26 Oct 1999, Daniel Selman wrote:
> Inaam,
>
> Very interesting problem... and quite a scenegraph!
>
> I've given it 10 minutes thought, so I'm not convinced there isn't an easier
> way, but here are some thoughts:
>
> 1. Create all of your cubelets under a dummy BranchGroup. Create 9 TGs each
> with an attached behavior to rotate its children through the "correct" +- 90
> degrees. As the user switches between slices you have to dynamically
> reparent the cubelets for the slice into the correct TG and deactive the
> other behaviors.
>
> 2. You might be able to use a SharedGroup and a Link Node in each TG? It's
> late and my head hurts, so maybe this wouldn't work! This might avoid all
> the ugly reparenting that will happen with method 1.
>
> 3. Essentially fake the whole thing. If you are only rotating by 90 degree
> steps you are not displaying the (interesting) intermediate rotation from
> position to position. Track the position of your cubes using an internal
> data structure and just *recolor* your static 3D representation to give the
> impression that the slices have been rotated.
>
> There must be more, but maybe this will kick start some ideas for you.
>
> Best of luck, let me know how your solve it!
>
> Sincerely,
>
> Daniel Selman
>
> [EMAIL PROTECTED]
> http://www.tornadolabs.com
>
> > -----Original Message-----
> > From: Discussion list for Java 3D API
> > [mailto:[EMAIL PROTECTED]]On Behalf Of Inaam Khan
> > Sent: 26 October 1999 00:18
> > To: [EMAIL PROTECTED]
> > Subject: [JAVA3D] Logical problem using Java3D for Making Rubik Cube
> >
> >
> > Hello i am using Java3d for the first time and am having trouble with
> > a method to pick up specific layers of the cube. If any of you understand
> > the cube you would know that there are nine layers which we can choose to
> > rotate in any direction. I have made the cubelets by using the Box utility
> > class. Given them the appropriate color and arranged them in their proper
> > places.I have so far made three layers which i can manipulate through the
> > key boards but as you know i can only manipulate three layers as they are
> > transform group with simple behaviour classes for 90 degrees rotation. I
> > can rotate the whole cube by choosing to enable the behavor for the
> > branchgroup containing all three transform groups.Now my problem is that
> > java3d does not allow cubelets to be put into more than one transform
> > group.Or share the cubelets in more than one transform group.If i could do
> > so i might update other transform group when ever i rotate a layer but
> > this is not possible.
> > Just what should i do to be able to pick up any of the nine layers through
> > mouse making them rotate only in one axis.Pls i need help and i am very
> > desperate if you guys can please send me any
> > additional reading material apart from what is given by Java3d i would be
> > greatfull.
> > I need help fast,some code would do better. I am currently looking in the
> > possibility of redoing the cube with geometry classes if their is no way
> > around.
> > Please i have a deadline approaching in a day any help would be welcome.
> > Thanks in advance.
> > Inaam Afzal Khan
> >
> > ==================================================================
> > =========
> > To unsubscribe, send email to [EMAIL PROTECTED] and include
> > in the body
> > of the message "signoff JAVA3D-INTEREST". For general help, send email to
> > [EMAIL PROTECTED] and include in the body of the message "help".
> >
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA3D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
import java.lang.System;
import java.util.Properties;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.picking.*;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Enumeration;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
public class Shape extends Frame {
public static BranchGroup scene=null;
public static Canvas3D c=null;
public EventHandler eh=new EventHandler();
public static MouseRotate behavior=null;
public static PickRotateBehavior pickx1=null;
public static final Color3f red=new Color3f(1.0f,0.0f,0.0f);
public static final Color3f green=new Color3f(0.0f,1.0f,0.0f);
public static final Color3f blue=new Color3f(0.0f,0.0f,1.0f);
public static final Color3f yellow=new Color3f(1.0f,1.0f,0.0f);
public static final Color3f white=new Color3f(0.3f,0.6f,0.8f);
public static final Color3f black=new Color3f(1.0f,1.0f,1.0f);
public static final Color3f cyan=new Color3f(0.70f,0.50f,0.06f);
public static final Color3f wall=new Color3f(0.148f,0.209f,0.2156f);
public static final Color3f wall2=new Color3f(0.196f,0.1686f,0.2627f);
public static final Color3f wall3=new Color3f(0.201f,0.20235f,0.4156f);
public static final Color3f wall4=new Color3f(0.301f,0.28235f,0.3098f);
public static final Color3f wall5=new Color3f(0.321568f,0.2352f,0.3098f);
public BranchGroup createSceneGraph(Canvas3D canvas) {
//------------------------------------------------------------------------------
BranchGroup objRoot = new BranchGroup();
//------------------------------------------------------------------------------
Transform3D dimension=new Transform3D();
Transform3D dimensionY=new Transform3D();
dimension.rotX(Math.PI/4.0d);
dimensionY.rotY(Math.PI/4.0d);
dimension.mul(dimensionY);
//------------------------------------------------------------------------------
TransformGroup GroupD=new TransformGroup(dimension);
GroupD.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
GroupD.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//------------------------------------------------------------------------------
// Create the bounding leaf node
BoundingBox bounds = new BoundingBox();
BoundingLeaf boundingLeaf = new BoundingLeaf(bounds);
boundingLeaf.setCapability(BoundingLeaf.ALLOW_REGION_READ);
boundingLeaf.setCapability(BoundingLeaf.ALLOW_REGION_WRITE);
GroupD.addChild(boundingLeaf);
GroupD.setBoundsAutoCompute(true);
//------------------------------------------------------------------------------
TransformGroup x1=new TransformGroup();
x1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
x1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
x1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
//enables the picking up of the group when asked for
x1.setBoundsAutoCompute(true);
//enables the checking of the bounds it self
BoundingBox xx1 = new BoundingBox();
BoundingLeaf boundingxx1 = new BoundingLeaf(xx1);
boundingLeaf.setCapability(BoundingLeaf.ALLOW_REGION_READ);
boundingLeaf.setCapability(BoundingLeaf.ALLOW_REGION_WRITE);
x1.addChild(boundingxx1);
MyBehavior behave=new MyBehavior(x1);
x1.addChild(behave);
behave.setSchedulingBoundingLeaf(boundingxx1);
pickx1=new
PickRotateBehavior(objRoot,canvas,xx1,PickObject.USE_BOUNDS);
x1.addChild(pickx1);
//pickx1.updateScene(4,5);
//pickx1.setEnable(false);
//------------------------------------------------------------------------------
TransformGroup x2=new TransformGroup();
x2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
x2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
x2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
x2.setBoundsAutoCompute(true);
//------------------------------------------------------------------------------
TransformGroup x3=new TransformGroup();
x3.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
x3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
x3.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
x3.setBoundsAutoCompute(true);
//------------------------------------------------------------------------------
TransformGroup y1=new TransformGroup();
y1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
y1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//------------------------------------------------------------------------------
TransformGroup y2=new TransformGroup();
y2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
y2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//------------------------------------------------------------------------------
TransformGroup y3=new TransformGroup();
y3.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
y3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//------------------------------------------------------------------------------
// Create the drag behavior node
behavior = new MouseRotate();
//behavior.setFactor(100.0d,10.0d);
behavior.setTransformGroup(GroupD);
GroupD.addChild(behavior);
behavior.setSchedulingBoundingLeaf(boundingLeaf);
//behavior.setEnable(false);
Box cubelets[][][]=new Box[3][3][3];
for(float x=-1;x<2;x++){
for(float y=-1;y<2;y++){
for(float z=-1;z<2;z++){
cubelets[(int)x+1][(int)y+1][(int)z+1]=addShape(new
Vector3f(0.24f*x,0.24f*y,0.24f*z),
new Box(),x,y,z,x1,x2,x3,y1,y2,y3);
}
}
}
GroupD.addChild(x1);
GroupD.addChild(x2);
GroupD.addChild(x3);
//GroupD.addChild(y1);
//GroupD.addChild(y2);
//GroupD.addChild(y3);
objRoot.addChild(GroupD);
// Let Java 3D perform optimizations on this scene graph.
objRoot.compile();
return objRoot;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
private Box addShape(Vector3f tr, Box shapeNode,float x,float y,float z,TransformGroup
x1,TransformGroup x2,TransformGroup x3,TransformGroup y1,TransformGroup
y2,TransformGroup y3)
{
Transform3D t = new Transform3D();
if(y==-1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cay = new ColoringAttributes();
ColoringAttributes cayy=new ColoringAttributes();
cay.setColor(red);
cayy.setColor(wall);
app.setColoringAttributes(cay);
app2.setColoringAttributes(cayy);
shapeNode.getShape(shapeNode.BOTTOM).setAppearance(app);
shapeNode.getShape(shapeNode.TOP).setAppearance(app2);
}
else if(y==0){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cay=new ColoringAttributes();
ColoringAttributes cayy=new ColoringAttributes();
cay.setColor(white);
cayy.setColor(wall);
app.setColoringAttributes(cay);
app2.setColoringAttributes(cayy);
shapeNode.getShape(shapeNode.BOTTOM).setAppearance(app);
shapeNode.getShape(shapeNode.TOP).setAppearance(app2);
}
else if(y==1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cay = new ColoringAttributes();
ColoringAttributes cayy=new ColoringAttributes();
cayy.setColor(blue);
cay.setColor(white);
app.setColoringAttributes(cay);
app2.setColoringAttributes(cayy);
shapeNode.getShape(shapeNode.BOTTOM).setAppearance(app);
shapeNode.getShape(shapeNode.TOP).setAppearance(app2);
}
if(x==-1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cax = new ColoringAttributes();
ColoringAttributes caxx=new ColoringAttributes();
cax.setColor(green);
caxx.setColor(wall2);
app.setColoringAttributes(cax);
app2.setColoringAttributes(caxx);
shapeNode.getShape(shapeNode.LEFT).setAppearance(app);
shapeNode.getShape(shapeNode.RIGHT).setAppearance(app2);
}
else if(x==0){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cax=new ColoringAttributes();
ColoringAttributes caxx=new ColoringAttributes();
cax.setColor(wall3);
caxx.setColor(wall2);
app.setColoringAttributes(cax);
app2.setColoringAttributes(caxx);
shapeNode.getShape(shapeNode.LEFT).setAppearance(app);
shapeNode.getShape(shapeNode.RIGHT).setAppearance(app2);
}
else if(x==1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cax = new ColoringAttributes();
ColoringAttributes caxx=new ColoringAttributes();
cax.setColor(wall3);
caxx.setColor(yellow);
app.setColoringAttributes(cax);
app2.setColoringAttributes(caxx);
shapeNode.getShape(shapeNode.LEFT).setAppearance(app);
shapeNode.getShape(shapeNode.RIGHT).setAppearance(app2);
}
if(z==-1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cazz = new ColoringAttributes();
ColoringAttributes cazzz=new ColoringAttributes();
cazz.setColor(cyan);
cazzz.setColor(wall4);
app.setColoringAttributes(cazz);
app2.setColoringAttributes(cazzz);
shapeNode.getShape(shapeNode.FRONT).setAppearance(app2);
shapeNode.getShape(shapeNode.BACK).setAppearance(app);
}
else if(z==0){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cazz=new ColoringAttributes();
ColoringAttributes cazzz=new ColoringAttributes();
cazz.setColor(wall5);
cazzz.setColor(wall4);
app.setColoringAttributes(cazzz);
app2.setColoringAttributes(cazz);
shapeNode.getShape(shapeNode.FRONT).setAppearance(app);
shapeNode.getShape(shapeNode.BACK).setAppearance(app2);
}
else if(z==1){
Appearance app=new Appearance();
Appearance app2=new Appearance();
ColoringAttributes cazz = new ColoringAttributes();
ColoringAttributes cazzz=new ColoringAttributes();
cazz.setColor(wall5);
cazzz.setColor(black);
app.setColoringAttributes(cazz);
app2.setColoringAttributes(cazzz);
shapeNode.getShape(shapeNode.FRONT).setAppearance(app2);
shapeNode.getShape(shapeNode.BACK).setAppearance(app);
}
t.set (tr);
// Shrink the objects so we don't have to be too far away to
// see them all.
t.setScale(0.10);
//shapeNode.setPickable(true);
TransformGroup newTrans = new TransformGroup(t);
//parent.addChild(newTrans);
newTrans.addChild(shapeNode);
if(y==1){
x1.addChild(newTrans);
}
else if(y==0){
x2.addChild(newTrans);
}
else if(y==-1){
x3.addChild(newTrans);
}
/*
if(x==-1){
y1.addChild(newTrans);
}
else if(x==0){
y2.addChild(newTrans);
}
else if(x==1){
y3.addChild(newTrans);
}
*/
return shapeNode;
}
public Shape() {
//-----------------------------------------------------------------
MenuItem menuItem ;
MenuItem menuItem2;
Menu menu ;
menuItem2 = new MenuItem( "Reset" ) ;
menuItem2.addActionListener(eh);
menuItem = new MenuItem( "Quit" ) ;
menuItem.addActionListener(eh);
menu = new Menu( "Game" ) ;
menu.add( menuItem2) ;
menu.add(menuItem);
addWindowListener(eh);
//-----------------------------------------------------------------
Menu Rotate=new Menu("Rotate");
MenuItem cube=new MenuItem("Cube");
cube.addActionListener(eh);
MenuItem faces=new MenuItem("Faces");
faces.addActionListener(eh);
Rotate.add(cube);
Rotate.add(faces);
//-----------------------------------------------------------------
MenuBar menuBar ;
menuBar = new MenuBar() ;
menuBar.add( menu ) ;
menuBar.add(Rotate);
setMenuBar( menuBar ) ;
setLayout(new BorderLayout());
c = new Canvas3D(null);
add("Center", c);
setSize(500,500);
scene = createSceneGraph(c);
SimpleUniverse u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
addWindowListener(new WindowEventHandler());
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
show();
}
}
class Player{
public static Shape frame;
public static void main(String[]args) {
Shape frame = new Shape();
}
public static void reset(){
frame=new Shape();
}
}
class WindowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
class EventHandler extends WindowAdapter implements ActionListener{
public void actionPerformed(ActionEvent e){
String selection=e.getActionCommand();
if("Quit".equals(selection)){
System.exit(0);
}
else if("Faces".equals(selection)){
Shape.behavior.setEnable(false);
Shape.pickx1.setEnable(true);
}
else if("Cube".equals(selection)){
Shape.behavior.setEnable(true);
Shape.pickx1.setEnable(false);
}
else if("Reset".equals(selection)){
Player.reset();
}
}
}
/*
class MyMenu extends Menu{
public MyMenu(Object labels[],ActionListener al,ItemListener il){
super((String)labels[0]);
String menuName=(String)labels[0];
char first
}
}
*/
class MyBehavior extends Behavior{
private TransformGroup parent;
private Transform3D rotation=new Transform3D();
private double angle=0.0;
public MyBehavior(TransformGroup parent){
this.parent=parent;
}
public void initialize(){
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
public void processStimulus(Enumeration criteria){
angle+=Math.PI/2.0d;
rotation.rotY(angle);
parent.setTransform(rotation);
this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
}
}
/*
class MenuFrame {
DemoFrame frameWindow ;
Button showFrame ;
public void init() {
showFrame = new Button( "Show Frame" ) ;
add( showFrame ) ;
frameWindow = new DemoFrame( "This is a Frame" ) ;
frameWindow.resize( 200, 100 ) ;
}
public boolean action( Event evt, Object arg ) {
if ( evt.target instanceof Button ) {
if ( !frameWindow.isShowing() ) {
frameWindow.show() ;
return true ;
}
}
return false ;
}
}
class DemoFrame extends Frame {
MenuItem menuItem ;
Menu menu ;
MenuBar menuBar ;
Label label ;
DemoFrame( String title ) {
super( title ) ;
menuItem = new MenuItem( "Hide Frame" ) ;
menu = new Menu( "My Menu" ) ;
menu.add( menuItem ) ;
menuBar = new MenuBar() ;
menuBar.add( menu ) ;
setMenuBar( menuBar ) ;
}
public boolean action( Event evt, Object arg ) {
if ( evt.target instanceof MenuItem ) {
if ( ((String)arg).equals( "Hide Frame" ) ) {
hide() ;
return true ;
}
}
return false ;
}
}
/*
public void init(){
// Set the layout manager
setLayout(new BorderLayout());
// Create the 3D canvas
Canvas3D canvas = new Canvas3D(null);
add("Center", canvas);
// Create the scene branch graph
BranchGroup scene = createSceneGraph(canvas);
// Create the Universe, the Locale, and the view branch graph
SimpleUniverse u = new SimpleUniverse(canvas);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
*/