Home arrow static arrow Java Programming [Archive] - Moving Squares
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Moving Squares
This topic has 5 replies on 1 page.

Posts:43
Registered: 12/30/03
Moving Squares  
Jun 6, 2004 12:38 AM



 
howsit all

Im programming a "flat-layout" of the magic cube - i need to find out how to move the squares - i spoke to some people and they suggested a cyclic array? not sure how to implement it - any feed back will be appreaciate it - sorry dont have any dukes left. - the program runs.

thanx in advanced


import javax.swing.*;import javax.swing.event.*;import javax.swing.text.*;import javax.swing.border.*;import javax.swing.colorchooser.*;import javax.swing.filechooser.*;import javax.accessibility.*; import java.awt.*;import java.awt.event.*;import java.beans.*;import java.util.*;import java.io.*;import java.applet.*;import java.net.*;  import javax.swing.*;import javax.swing.BorderFactory.*;import java.awt.event.*;import java.awt.*;import javax.swing.border.*;import javax.swing.event.*;    class CRubicCubeRectangle extends Rectangle{     public final static Color DEFAULT_COLOUR = Color.BLUE;    public Color clrColor; //CRubicCubeRectangle with two constructors     CRubicCubeRectangle(int iXPosition,                         int iYPosition,                         int iWidth,                         int iHeight)    {        super(iXPosition, iYPosition, iWidth, iHeight);        clrColor = DEFAULT_COLOUR;    }        CRubicCubeRectangle(int iXPosition,                         int iYPosition,                         int iWidth,                         int iHeight,                         Color clrColour)    {        super(iXPosition,iYPosition,iWidth,iHeight);	//With reference to the Rectangle class : passing the argument to Rectangle class        clrColor = clrColour;    }        void setColour(Color clrColour)    {        clrColor = clrColour;            }} class CRubicCubeSide {    public CRubicCubeRectangle rcrArray[][] = new CRubicCubeRectangle[3][3];         public CRubicCubeSide(int iXPosition,                           int iYPosition,                           int iWidth,                           int iHeight)    {        for (int iCountY = 0; iCountY < 3; iCountY++)            for (int iCountX = 0; iCountX < 3; iCountX++)            {                rcrArray[iCountX][iCountY]                     = new CRubicCubeRectangle((iXPosition) + (iCountX*iWidth),                                               (iYPosition) + (iCountY*iHeight),                                               iWidth,                                               iHeight);                                System.out.println("X : " + (iXPosition + iCountX*iWidth));                System.out.println("Y : " + (iYPosition + iCountY*iHeight));                System.out.println("Width  : "  + iWidth);                System.out.println("Height : " + iHeight);             }     }         public CRubicCubeSide(int iXPosition,                           int iYPosition,                           int iWidth,                           int iHeight,                          Color clrColour)    {        for (int iCountY = 0; iCountY < 3; iCountY++)            for (int iCountX = 0; iCountX < 3; iCountX++)            {                rcrArray[iCountX][iCountY]                     = new CRubicCubeRectangle((iXPosition) + (iCountX*iWidth),                                               (iYPosition) + (iCountY*iHeight),                                               iWidth,                                               iHeight,                                               clrColour);                                System.out.println("X : " + (iXPosition + iCountX*iWidth));                System.out.println("Y : " + (iYPosition + iCountY*iHeight));                System.out.println("Width  : "  + iWidth);                System.out.println("Height : " + iHeight);                System.out.println("Colour : " + clrColour);             }     }    void draw(Graphics2D g2dDisplay)    {      CRubicCubeRectangle rcrTemp;	//filling colours of the small rectangle blocks            for (int iCountY = 0; iCountY < 3; iCountY++)        for (int iCountX = 0; iCountX < 3; iCountX++)        {            rcrTemp = this.rcrArray[iCountX][iCountY];            g2dDisplay.setColor(rcrTemp.clrColor);            g2dDisplay.fillRect(rcrTemp.x, rcrTemp.y, rcrTemp.width, rcrTemp.height);        }                            g2dDisplay.setColor(Color.BLACK);	//Drawing or splitting up of them side into 3x3       rcrTemp = this.rcrArray[0][0];              g2dDisplay.drawRect(rcrTemp.x, 	//Drawing a rectangle                           rcrTemp.y,                            rcrTemp.width*3,                            rcrTemp.height*3);              //Splitting up the whole block into rectangles             g2dDisplay.drawLine(rcrTemp.x, 	                           rcrTemp.y + rcrTemp.height,                           rcrTemp.x + rcrTemp.width*3,                            rcrTemp.y + rcrTemp.height);              g2dDisplay.drawLine(rcrTemp.x,                            rcrTemp.y + rcrTemp.height*2,                           rcrTemp.x + rcrTemp.width*3,                            rcrTemp.y + rcrTemp.height*2);                        g2dDisplay.drawLine(rcrTemp.x + rcrTemp.width,                            rcrTemp.y,                           rcrTemp.x + rcrTemp.width,                            rcrTemp.y + rcrTemp.height*3);              g2dDisplay.drawLine(rcrTemp.x + rcrTemp.width*2, 	//Draws a line(y=i): initial x position and x postion (width*2) to                            rcrTemp.y ,				//                       (y=i + (blocks height*3)     initial x position and x position (width*2)                           rcrTemp.x + rcrTemp.width*2,                            rcrTemp.y + rcrTemp.height*3);                 }    }  class CRubicCube{    public final static int NUMBER_OF_SIDES = 6;        CRubicCubeSide rcrSides[] = new CRubicCubeSide[NUMBER_OF_SIDES];        CRubicCube( int iXOffset, int iYOffset, int iBlockWidth, int iBlockHeight)    {//        for (int iCount = 0; iCount < 6; iCount++)        rcrSides[0] = new CRubicCubeSide(iBlockWidth*3 + iXOffset,                                         0 + iYOffset,                                          iBlockWidth,                                         iBlockHeight,                                          Color.YELLOW);                rcrSides[1] = new CRubicCubeSide(0 + iXOffset,                                         iBlockHeight*3 + iYOffset,                                         iBlockWidth,                                         iBlockHeight,                                          Color.RED);                rcrSides[2] = new CRubicCubeSide(iBlockWidth*3 + iXOffset,                                         iBlockHeight*3 + iYOffset,                                         iBlockWidth,                                         iBlockHeight,                                          Color.BLUE);                rcrSides[3] = new CRubicCubeSide(iBlockWidth*3*2 + iXOffset,                                         iBlockHeight*3 + iYOffset,                                         iBlockWidth,                                         iBlockHeight,                                          Color.GREEN);                rcrSides[4] = new CRubicCubeSide(iBlockWidth*3 + iXOffset,                                         iBlockHeight*3*2 + iYOffset,                                         iBlockWidth,                                         iBlockHeight,                                          Color.WHITE);                rcrSides[5] = new CRubicCubeSide(iBlockWidth*3 + iXOffset,                                         iBlockHeight*3*3 + iYOffset,                                         iBlockWidth,                                         iBlockHeight,                                          Color.ORANGE); 		    }            void draw(Graphics2D g2dDisplay)    {        for (int iCount = 0; iCount < 6; iCount++)            rcrSides[iCount].draw(g2dDisplay);    }} public class CTestFrame extends javax.swing.JFrame	//Displaying the output{        CRubicCubeSide rcsTest;    CRubicCube rcCube;    //(Rectangle)(rcsTest.rctRectangleArray[0][0])        public CTestFrame()     {        initComponents();	//methods which manipulate the frame        this.setSize(800,800);                       }            private void initComponents() {	//ending the Frame output                addWindowListener(new java.awt.event.WindowAdapter() {            public void windowClosing(java.awt.event.WindowEvent evt) {                exitForm(evt);            }        });                pack();	//Method inherited by window class : It packs the components in the window    }          public void paint(Graphics comp)  {    Graphics2D g2dDisplay = (Graphics2D)comp;    rcCube.draw(g2dDisplay);   }          private void exitForm(java.awt.event.WindowEvent evt) {	//Exiting the frame or ending it        System.exit(0);    }           public static void main(String args[]) {                CTestFrame ctf = new CTestFrame();        ctf.rcCube = new CRubicCube(50,50, 50,50); // change the position						 // and the block width here        ctf.show();    }          }
 

Posts:43
Registered: 12/30/03
Re: Moving Squares  
Jun 6, 2004 11:37 PM (reply 1 of 5)



 
Some help Please?
 

Posts:214
Registered: 6/21/01
Re: Moving Squares  
Jun 7, 2004 7:52 AM (reply 2 of 5)



 
I havent really had time to look at your code but I would do something like:
Make a 9*12 Color array
The flattened cube sits within this in a cross shape.
Then have various operator methods to do the various rotations and face turns.
E.g
rightClockwise(cubeArray) - for a face turn (there are 18 possible face and slice turns)
...
cubeXClockwise(cubeArray) - for a cube turn (there are 6 possible cube turns)

inside the methods you must swap the appropriate values to their new positions within the array.

Cheers,
Jim
 

Posts:214
Registered: 6/21/01
Re: Moving Squares  
Jun 7, 2004 8:58 AM (reply 3 of 5)



 
the cube Array will start off like:
NNNOOONNN
NNNOOONNN
NNNOOONNN
NNNYYYNNN
NNNYYYNNN
NNNYYYNNN
BBBRRRGGG
BBBRRRGGG
BBBRRRGGG
NNNWWWNNN
NNNWWWNNN
NNNWWWNNN

where N = null
O = orange
Y = yellow
B = blue
R = red
G = Green
W = white

so this represent red is front white is down green is right
 

Posts:43
Registered: 12/30/03
Re: Moving Squares  
Jun 9, 2004 8:02 AM (reply 4 of 5)



 
inside the methods you must swap the appropriate values to their new positions within the array.

thanx for the advice, my problem is swapping the values to their new positions within the array..
any ideas?
 

Posts:214
Registered: 6/21/01
Re: Moving Squares  
Jun 17, 2004 4:17 AM (reply 5 of 5)



 
use the approach:
e.g.
if you had a 3*1 Color array
Color[] array = new Color[] {red, blur, green};
you could have method like:
rotateColors(Color[] array)
{
Color tempColor = array[0];
array[0] = array[1];
array[1] = array[2];
array[2] = tempColor;
}
Cheers,
Jim
 
This topic has 5 replies on 1 page.