Results 1 to 13 of 13
  1. #1
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 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].


    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;
    		}
    	}
    }
    Picture of what I basically want if my words did not make sense:
    StdDraw &amp; Arrays-stddraw.png

    I want this to be for every block that has been drawn.

  2. #2
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 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].


    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;
            }
        }
    }
    Picture of what I basically want if my words did not make sense:
    StdDraw &amp; Arrays-stddraw.png

    I want this to be for every block that has been drawn.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 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??

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default 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.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: StdDraw and Arrays

    Cheers for the update Jim.

  9. #9
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 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.

  10. #10
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default 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 :(

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: StdDraw and Arrays

    Quote Originally Posted by jim829 View Post
    Ronin,

    The poster cross-posted to Java2D. I can't do anything about it but I told him/her the same thing.

    Regards,
    Jim
    I've merged the two threads here. Asmicor, please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Arrow 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

  1. Replies: 1
    Last Post: 03-09-2012, 09:53 AM
  2. problems with StdDraw and Standard Drawing
    By elvis0288 in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 04:57 AM
  3. problem with StdDraw and Eclipse
    By elvis0288 in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 10:17 PM
  4. StdDraw.setXscale i have problems with this
    By elvis0288 in forum New To Java
    Replies: 3
    Last Post: 03-04-2012, 01:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •