View Single Post
  #1 (permalink)  
Old 07-16-2007, 11:16 PM
romina romina is offline
Member
 
Join Date: Jul 2007
Posts: 26
romina is on a distinguished road
Adding graphics to array
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)
Code:
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.

Code:
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
Code:
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
Code:
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...
Code:
public void moveDisk(){ //no idea }
Any suggestions?
Thanks.
Reply With Quote
Sponsored Links