Results 1 to 13 of 13
Thread: StdDraw & Arrays
- 03-15-2013, 05:57 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
StdDraw & Arrays
I have to make the game Flood-It for a project and I'm struggling with the blocks you have to make.
This is my code so far, basically all I have done is drawn random colored blocks to see if I can do it, now the problem I have is how do I assign say grid[0][0] one of the random colors. In the end I want every block that was drawn to be assigned to a different grid[i][j].
Picture of what I basically want if my words did not make sense:Java Code:public class Colours { public static void main(String[] args) { int N = 5; double xpos = 0.0; double ypos = 0.0; double size = 0.05; double[][] grid = new double[N][N]; for (int i = 0; i < N; i = i + 1) { for ( int j = 0; j < N; j = j + 1) { grid[i][j] = 5; System.out.println(i + " " + j + " " + grid[i][j]); } } StdDraw.setCanvasSize(500,500); for (double c = 0.0; c < 1; c = c + size) { for (double z = 0.0; z < 1; z = z + size) { int rn = (int) (Math.random() * 6 + 1); if (rn == 1) { StdDraw.setPenColor(StdDraw.RED); } if (rn == 2) { StdDraw.setPenColor(StdDraw.BLUE); } if (rn == 3) { StdDraw.setPenColor(StdDraw.ORANGE); } if (rn == 4) { StdDraw.setPenColor(StdDraw.GREEN); } if (rn == 5) { StdDraw.setPenColor(StdDraw.GRAY); } if (rn == 6) { StdDraw.setPenColor(StdDraw.PINK); } StdDraw.filledSquare(xpos, ypos, size/2); xpos = xpos + size; } xpos = 0.0; ypos = ypos + size; } } }

I want this to be for every block that has been drawn.
- 03-16-2013, 08:22 PM #2
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
StdDraw & Arrays
Make the game Flood-It and I'm struggling with the blocks you have to make.
This is my code so far, basically all I have done is drawn random colored blocks to see if I can do it, now the problem I have is how do I assign say grid[0][0] one of the random colors. In the end I want every block that was drawn to be assigned to a different grid[i][j].
Picture of what I basically want if my words did not make sense:Java Code:public class Colours { public static void main(String[] args) { int N = 5; double xpos = 0.0; double ypos = 0.0; double size = 0.05; double[][] grid = new double[N][N]; for (int i = 0; i < N; i = i + 1) { for ( int j = 0; j < N; j = j + 1) { grid[i][j] = 5; System.out.println(i + " " + j + " " + grid[i][j]); } } StdDraw.setCanvasSize(500,500); for (double c = 0.0; c < 1; c = c + size) { for (double z = 0.0; z < 1; z = z + size) { int rn = (int) (Math.random() * 6 + 1); if (rn == 1) { StdDraw.setPenColor(StdDraw.RED); } if (rn == 2) { StdDraw.setPenColor(StdDraw.BLUE); } if (rn == 3) { StdDraw.setPenColor(StdDraw.ORANGE); } if (rn == 4) { StdDraw.setPenColor(StdDraw.GREEN); } if (rn == 5) { StdDraw.setPenColor(StdDraw.GRAY); } if (rn == 6) { StdDraw.setPenColor(StdDraw.PINK); } StdDraw.filledSquare(xpos, ypos, size/2); xpos = xpos + size; } xpos = 0.0; ypos = ypos + size; } } }

I want this to be for every block that has been drawn.
- 03-16-2013, 08:43 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 683
- Rep Power
- 1
Re: StdDraw & Arrays
Didn't you already post this in the New to Java forum? Cross posting is a forum no-no will incur the wrath of the moderators.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-16-2013, 08:52 PM #4
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
Re: StdDraw & Arrays
Yes I know, but I saw that no one has replied on my thread so I decided to move it to another section to see if I could have better luck. I'm really desprite to make this game work. Would you be able to help me???? Am I going in the wrong direction with the game??
- 03-16-2013, 08:57 PM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: StdDraw and Arrays
Hi Asmicor,
StdDraw not in any standard Java package but is a class developed by a third party.
To resolve you issue you need to use a two dimensional array of type Color. Looping through the array you can generate a random colour then assign it to the element.
Once you are ready to draw the image, loop through the array again referencing the colour stored in each element.
Regards.
- 03-16-2013, 09:14 PM #6
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 683
- Rep Power
- 1
Re: StdDraw & Arrays
The problem is that the methods you are using do not exist. It is as though you were guessing at the methods required. You need to check out the Tutorials (see my signature link) to learn how to use the graphics classes. Then, try writing the code and running it. The forum will still be here to assist.
Here is a quick start. Classes to check are JFrame, JPanel, Graphics and Graphics2D. However, there are many others.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-16-2013, 09:17 PM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 683
- Rep Power
- 1
Re: StdDraw and Arrays
Ronin,
The poster cross-posted to Java2D. I can't do anything about it but I told him/her the same thing.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-16-2013, 09:19 PM #8
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: StdDraw and Arrays
Cheers for the update Jim.
- 03-16-2013, 09:41 PM #9
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
Re: StdDraw and Arrays
Thank you Ronin for your help, you just gave me hope again. I know StdDraw is not a standard package so I imported StdIn, StdAudio and StdDraw in the begining because I will need them. The picture I posted above was generated from my original code. I also knew that about the two dimensional arrays that will be needed but I did not know you could make them a type Color. That solved my problem. I knew all this time what I wanted but had no idea how to do it, I always got conversion errors with all my attempts.
And yes I cross-posted, I'm sorry but no one was giving me anything so I got desprite.
Please link this thread as I may need more help in the future. I'm still a rooky, learning at every corner, I'm only in my 5th week now.
- 03-16-2013, 09:42 PM #10
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
Re: StdDraw & Arrays
I will have a look at those tuts jim829, I appreciate your help so far. I've been waiting 24h for someone to respond :(
- 03-16-2013, 09:51 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 683
- Rep Power
- 1
Re: StdDraw & Arrays
Your welcome. However, understand that even 72 hours is not a long time to wait for a response. It all depends on who is on and their interest and expertise level. Also, your chance of getting a quicker response is more likely if you read the forum guidelines.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-17-2013, 01:18 AM #12
Re: StdDraw and Arrays
I've merged the two threads here. Asmicor, please go through the Forum Rules, particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-14-2013, 05:25 PM #13
Member
- Join Date
- Jan 2013
- Posts
- 12
- Rep Power
- 0
Re: StdDraw & Arrays
For those of you that want my finished game, please go download it here.
Flood-It.rar
It took me quite a while and I'm pretty pleased with the final product. Please note that not all features are working and I'm still busy improving.
*Auto Solve and Hint does not work.
*High Scores works but not correctly yet. About 45% done.
*When you click on the "High Scores" words more than one Notepad will open. Have not found a fix for this yet.
If any of you have any knowledge of this game and know how I might be able to improve it please e-mail me: asmicor@gmail.com
Enjoy
Similar Threads
-
I want to use StdDraw or any other library to use .gif and show in animation
By elvis0288 in forum New To JavaReplies: 1Last Post: 03-09-2012, 09:53 AM -
problems with StdDraw and Standard Drawing
By elvis0288 in forum New To JavaReplies: 0Last Post: 03-08-2012, 04:57 AM -
problem with StdDraw and Eclipse
By elvis0288 in forum New To JavaReplies: 1Last Post: 03-06-2012, 10:17 PM -
StdDraw.setXscale i have problems with this
By elvis0288 in forum New To JavaReplies: 3Last Post: 03-04-2012, 01:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks