PDA

View Full Version : Java - adding graphics to an array and moving them


Yakisoba
09-06-2006, 09:00 AM
I have a function that draws a stack of rounded rectangles ("disks") in a particular column.
As the stack increases the disk size decreases.

I would like to be able to move these shapes between different columns. This is done buy the user clicking on a "disk" then clicking on a destination column. (I have already written a method that will get the first and second click co-ordinates, and draw something on the second click)

when the "move" occurs I need that particular disk to maintain its properties (i.e. Length).

I would like to somehow store each disk as it is made with an ID, so that later on I can draw a specific disk by refering to the ID.

i.e: (in this case the values don't matter, just the fact that they are different)
disks[0] = fillRoundRect(1,1,1,1,1,1)
disks[1] = fillRoundRect(2,2,2,2,2,2)
disks[2] = fillRoundRect(3,3,3,3,3,3)
disks[3] = fillRoundRect(4,4,4,4,4,4)
disks[4] = fillRoundRect(15,12,14,16,45)
disks[5] = fillRoundRect(n, n, n, n, n)

Is this possible?

Below is the code that I have been writing to accomplish this. Any suggestions would be greatly appreciated.




public void drawDisk(int diskAmount, Graphics gfx){

//set the color of the disks
gfx.setColor(Color.ORANGE);

int numberOfDisks = diskAmount;
int i = 0;
int row = 0;
int col = 0;
int startX = 7;
int startY = 282;
int endX = GAME_CELL_WIDTH - 4;
int endY = DISK_HEIGHT - 4;

//calculate the starting position of the cell
int x = startX + col * GAME_CELL_WIDTH;

//create the number of disks requested
while (numberOfDisks > i){
int y = startY - row * DISK_HEIGHT;

//create a new instance of disk
Disk newDisk;
newDisk = new Disk(row, 0, x, y, endX, endY);

//decreases the disk of the next disk by moving the
//x and y. Note the height stays the same
x = x + 8;
y = y + 8;
endX = endX - 16;


//not necessary in this loop, just here for testing
int ax = newDisk.getStartX();
int ay = newDisk.getStartY();
int dx = newDisk.getEndX();
int dy = newDisk.getEndY();

//draw the "disk"
gfx.fillRoundRect(ax, ay, dx, dy, 15, 15);

//add the disk to an array
this.addDisk(newDisk, i);

//move to the next row
row++;

i++;
}
}


This little piece will add the disks to an array


public void addDisk(Disk newDisk, int diskId){

int row = newDisk.getRow();
int col = newDisk.getCol();

//add the Disk to the Array
this.disks[diskId] = newDisk;
//change the value of the cell
this.gameCells[row][col] = diskId;

}


here is the code for my disk Class

public class Disk {

private int row;
private int col;
private int diskSize;
private int startX;
private int startY;
private int endX;
private int endY;

/** Creates a new instance of Disk */
public Disk(int row, int col, int x, int y, int dx, int dy) {
this.row = row;
this.col = col;
//this.diskSize = size;
this.startX = x;
this.startY = y;
this.endX = dx;
this.endY = dy;
}

public int getRow(){
return this.row;
}

public int getCol(){
return this.col;
}



public int getStartX(){
return this.startX;
}

public int getStartY(){
return this.startY;
}

public int getEndX(){
return this.endX;
}

public int getEndY(){
return this.endY;
}

}


I am trying to create a method called "moveDisk" that will, for example, delete disk[2] from the first clicked col, and redraw disk[2] in the second clicked column. I have been stuck on this for some time now and am going postal...


public void moveDisk(){
//no idea
}


Any suggestions? A different approach?

Thanks,

Yak

tricolaire
09-14-2006, 01:41 AM
I would make my Disk class extend Object, and manipulate things from within a java.util.ArrayList

that way you could create a temporary Disk, and insert it at a specific place in the ArrayList

Yakisoba
09-14-2006, 04:17 AM
Thanks for the suggestion...

I have come across a temporary solution by using:


x = disks[diskIdentifier].getStartX();
y = disks[diskIdentifier].getStartY();


by using the ID with my "getters" I am able to get the co-ordinates I need.

Thanks

Yak