Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 3 123 LastLast
Results 1 to 25 of 61

Thread: Making a method to move blocks in a grid

  1. #1
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making a method to move blocks in a grid

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.Color;
    import java.util.Iterator;
    import java.util.Random;
    import java.util.List;
     
    public class LAB1
    {
    	final static int BLOCKWIDTH = 50;
    	final static int BLOCKHEIGHT = 10;
    	final static int NBLOCKS = 10;
    	final static int BLOCKROWS = 5;
    	final static int ROWZERO = 50;
    	final static int ROWSEPARATION = (5*BLOCKHEIGHT);
    	final static int WIDTH = (BLOCKWIDTH*NBLOCKS);
    	final static int HEIGHT = 600;
     
    	public static java.util.List<acm.graphics.GRect> createPlayfield()
    	{
    		Random r = new Random();
    		List<GRect> l = null;
    		int x = 0;
    		int y = ROWZERO - BLOCKHEIGHT;
    		Color c = new Color(r.nextInt(256));
     
    		addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c);
     
    		for (int i = 0; i < BLOCKROWS; i++)
    		{
    			i = (i * ROWSEPARATION) + ROWZERO;
     
    			for(int j = 0; j < NBLOCKS; j++)
    			{
    				j = j * BLOCKWIDTH;
    			}
    		}
     
     
     
     
    	}
     
    	public static void addBrick(java.util.List<acm.graphics.GRect> l, int x, int y, int width, int height, java.awt.Color c)
    	{
    		l.add(createBrick(x,y,WIDTH,HEIGHT,c));
    	}
     
    	public static acm.graphics.GRect createBrick(int x, int y, int width, int height, java.awt.Color c)
    	{
    		GRect s = new GRect(x,y,WIDTH,HEIGHT);
     
    		return s;
    	}
    	public static acm.graphics.GRect intersect(acm.graphics.GObject o,java.util.List<acm.graphics.GRect> l)
    	{
    		GRectangle r = o.getBounds();
     
    		for(GRect e : l)
    		{
    			if(e.getBounds().intersects(r))
    				return e;
    		}
    		return null;
    	}

    My main problem is the createplayfield method, this is the description to make it:

    createPlayfield is a static method that returns a List of GRects. Each GRect is coloured randmoly and is
    of size BLOCKWIDTH x BLOCKHEIGHT. The blocks are placed in a grid BLOCKROWS height x
    NBLOCKS. The i'th row of blocks has its upper edge aligned with row i * ROWSEPARATION +
    ROWZERO. The j'th column of blocks has its left edge aligned with j * BLOCKWIDTH. You may
    find it useful to implement this method using the addBrick method below.

    API's : The acm.graphics.GRect Class.

    As you can see I tryed to do it but haven't done well, it's a very confusing description.
    Last edited by geforce; May 17th, 2012 at 08:54 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Alright edited it, take a look now, every other method should be fine except the method "createPlayfield".

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Making a method to move blocks in a grid

    Well in your createPlayField method, you never return a list at all and you only add one brick the whole time. Your loops really don't make any logical sense either. Try and think about it again step by step.

  5. #5
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Um... yeah, I'm aware of that, thats why I'm trying to figure out what to do because I don't understand the statement for the createplayfield method, if my method had an small error I would of fixed it, but I'm obviously lost out of my mind on this method..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Rewrite the requirements for the method as a list of small simple steps. Then work on the code for each step.

    What was posted is hard to read because it was has weird line breaks and doesn't have the steps in a simple list.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Quote Originally Posted by Norm View Post
    Rewrite the requirements for the method as a list of small simple steps. Then work on the code for each step.

    What was posted is hard to read because it was has weird line breaks and doesn't have the steps in a simple list.
    I understand how to follow the simple steps but i dont understand how to implement it in code, like how would i return the list of grects. Since the descriptions talks about j'tg coloumn and i'th row i know that i need a nested for loop but i dont know where to put the description of the i'th row into the i'th block, same as j coloumn and where to insert the blockrow x nblock

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Often you can code from the list of simple steps.
    If you have a list of steps and are having a problem coding any of them, list the step you are having problems with.

    how would i return the list of grects.
    With a return statement:
    return grects;

    l is a terrible name for a variable. Can you add a few more letters to it?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Quote Originally Posted by Norm View Post
    Often you can code from the list of simple steps.
    If you have a list of steps and are having a problem coding any of them, list the step you are having problems with.


    With a return statement:
    return grects;

    l is a terrible name for a variable. Can you add a few more letters to it?
    I will rename all the variables to make it look better once I can make the program function right.

    ok so


     
    public static java.util.List<acm.graphics.GRect> createPlayfield()
    {
         Random r = new Random();
         List<GRect> l = null;
         int x = 0;
         int y = ROWZERO - BLOCKHEIGHT;
         Color c = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
          // Where to implement how to insert the blocks into a grid?
         for (i = 0; i <= BLOCKROWS; i++) // 
         {// How to implement that i'th row is aligned with i * ROWSEPARATION + ROWZERO
            i = i * ROWSEPARATION + ROWZERO;
              for (j = 0; j <= NBLOCKS; j++) 
              {// How to implement that i'th row is aligned with j x BLOCKWIDTH;         
                 j = j * BLOCKWIDTH;
              }
                 addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c); // How to implement i and j into the list of grects.
          }
            return l;
    }



    These are my problems and theres some more, I think my other methods are wrong too.
    Last edited by geforce; May 21st, 2012 at 04:05 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    I will rename all the variables to make it look better once I can make the program function right.
    The idea of using well named variables is so when you (or anyone) reads the code they can understand what it does. When the coding is finished and is being tossed in the bin who cares what the names are.

    Those comments hidden in the code are hard to read.
    how to insert the blocks into a grid?
    What is a block? Is it an object?
    What is the grid? Is it an array?

    At what locations in the array are the objects to be inserted?
    Last edited by Norm; May 21st, 2012 at 04:07 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Yeah I'm aware, it's for coding to be more effiecent but at this time It won't make a difference if the whole program doesn't work.

  12. #12
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    The things I put in the comments are my main issues in this method and also that I'm not sure if I'm intializing variables that I don't need to before the constructor of addBrick, or if I'm adding it too late or without a proper loop etc.

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Writing code before you have a design for what the code is supposed to do usually is a bad idea.
    Get the design, then write the code.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Quote Originally Posted by Norm View Post
    Writing code before you have a design for what the code is supposed to do usually is a bad idea.
    Get the design, then write the code.
    Thats exactly what I've been telling you, I know what to do but I don't know where to implement the code, I don't know how to thats why I need help, If I knew the design I would of done it myself, I just don't know "Where to put the code" and "Where it belongs"

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    I have not seen the List of steps the code has to do to solve the problem. Post that and we'll work on the code for each step one by one.

    Perhaps the steps you are thinking of are at a too high a level and need to be further broken down into simpler steps.
    Last edited by Norm; May 21st, 2012 at 04:22 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    1) Create a static method (createPlayfield)
    2) Each GRect is randomly coloured
    3) Each GRect is of size BLOCKWIDTH x BLOCKHEIGHT
    4) The blocks are placed in an grid
    5) The grid is of size BLOCKROWS x NBLOCKS
    6) The i'th row of the grid is aligned at the upper edge with i x ROWSEPARATION + ROWZERO
    7) The j'th coloumn is aligned with it's left edge with j x BLOCKWIDTH
    8) The list of GRects are returned

    The words in capital are finals which are listed on my first post, the addBrick method and maybe the createBrick (Not sure) method are used in this method.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    #4 what is a block? what is a grid? At what locations in the grid do the blocks go?
    #5 implies the grid is a 2D array. Is that right?

    I have no idea what #6 & #7 mean

    #8 What is the List that is returned? How does that relate to the grid (an array?)?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Quote Originally Posted by Norm View Post
    #4 what is a block? what is a grid? At what locations in the grid do the blocks go?
    #5 implies the grid is a 2D array. Is that right?

    I have no idea what #6 & #7 mean

    #8 What is the List that is returned? How does that relate to the grid (an array?)?
    You're thinking way too complicated, there are no arrays in this at all.

    This program is based on a game breakout http://www.arcadebond.com/games/imag...akout_game.jpg

    The package imported acm.graphics implements the block, and the grid is implemented. On the grid there is a low left edge and a upper edge and their aligned with those things assigned above.. the picture I showed gives a VERY VERY good description of what the program is, it's basically that. The variable "l" in List<GRect> is returned.

    this is the api for GRect Generated Documentation (Untitled) and Generated Documentation (Untitled) is the Package of everything such as graphic tools for the blocks etc

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Ok, I think I get it. The method is supposed to layout shapes on a visual grid. Most of the needed logic is for step #4. Work on the rows one by one using step #6 to get the rows position. On each row position the shapes column as per step #7

    Now you need to expand step #4 to handle going through the rows and columns etc
    Last edited by Norm; May 21st, 2012 at 04:56 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    This should give you a good example the methods are used in this program.

    import acm.graphics.*;
    import acm.program.*;
    import java.awt.Color;
    import java.util.Random;
    import java.util.List;
     
    public class Lab1Exerciser extends GraphicsProgram 
    {
    	private static final int BALLWIDTH = 20;
     
     
    	public void run() 
    	{
    		this.setSize(GraphicsTools.WIDTH,GraphicsTools.HEIGHT);
     
     
    		Random r = new Random();
    		int x = (GraphicsTools.WIDTH-BALLWIDTH) /2;
    		int y = (GraphicsTools.HEIGHT-BALLWIDTH) / 2;
    		int vx = 5 - r.nextInt(11);
    		int vy = 5 - r.nextInt(11);
    		if(vx == 0)
    			vx = 1;
    		if(vy == 0)
    			vy = 1;
    		GOval ball = new GOval(x, y, BALLWIDTH, BALLWIDTH);
    		ball.setFillColor(Color.RED);
    		ball.setFilled(true);
    		this.add(ball);
     
    		List<GRect> l = GraphicsTools.createPlayfield();
    		for(GRect e : l)
    			this.add(e);
     
    		for(;;)
    		{
    		    x = x + vx;
    		    y = y + vy;
    		    if(x < 0) 
    		    {
    		    	x = 0;
    		    	vx = -vx;
    		    }
    		    if(y < 0)
    		    {
    		    	y = 0;
    		    	vy = -vy;
    		    }
    		    if(x > GraphicsTools.WIDTH-BALLWIDTH)
    		    {
    		    	x = GraphicsTools.WIDTH-BALLWIDTH;
    		    	vx = -vx;
    		    }
    		    if(y > GraphicsTools.HEIGHT - BALLWIDTH)
    		    {
    		    	y = GraphicsTools.HEIGHT - BALLWIDTH;
    		    	vy = -vy;
    		    }
     
    		    GRect hit = GraphicsTools.intersect(ball, l);
    		    if(hit != null)
    		    {
    		    	GRectangle bb = ball.getBounds();
    		    	GRectangle bo = hit.getBounds();
    		    	double bxo = bb.getX() + bb.getWidth() / 2;
    		    	double byo = bb.getY() + bb.getHeight() / 2;
    		    	double oxo = bo.getX() + bo.getWidth() / 2;
    		    	double oyo = bo.getY() + bo.getHeight() / 2;
    		    	if(bxo < oxo) vx = -Math.abs(vx);
    		    	if(bxo > oxo) vx = Math.abs(vx);
    		    	if(byo < oyo) vy = -Math.abs(vy);
    		    	if(byo > oyo) vy = Math.abs(vy);
    		    	l.remove(hit);
    		    	this.remove(hit);
    		    }
    		    ball.setLocation(x, y);
     
    			this.pause(10);
     
    		}
    	}
     
    }

  21. #21
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

     
    public static java.util.List<acm.graphics.GRect> createPlayfield()
    {
         Random r = new Random();
         List<GRect> l = null;     
         int x = 0;
         int y = ROWZERO - BLOCKHEIGHT;
         Color c = new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256));
     
         for (i = 0; i <= BLOCKROWS; i++) 
         {
            i = i * ROWSEPARATION + ROWZERO;
              for (j = 0; j <= NBLOCKS; j++) 
              {
                 j = j * BLOCKWIDTH;
                addBrick(l.getX(),l.getY(),BLOCKWIDTH,BLOCKHEIGHT,l.getColor(c));        
              } 
          }
            return l;
    }

    Ok so, I did what you described but now, the problem is, i and j are just there and their doing nothing, and there is no grid, how would I add an grid because there is BLOCKROW x NBLOCK for grid.
    Last edited by geforce; May 21st, 2012 at 06:06 PM.

  22. #22
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    Also since the grid has a size would I need to put a loop for that?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a method to move blocks in a grid

    Do you have the steps the program needs to do now? You keep posting lines of code.
    What is the logic for placing the shapes for #4?
    Does the GRect constructor take the size? That would be for #3. It also would have a method to take the results of #2.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    4) I'm not sure.. I have to place the GRects onto the grid, probably would be l.add(i,j) or l.add(x,y) because of the coordinates? Maybe l.setLocation(x,y)...
    3) it would be addBrick(l,x,y,BLOCKWIDTH,BLOCKHEIGHT,c) but.. the compiler says I must intialize it first so i end up doing List<GRect> = null, x = 0, y = something above, etc.. and then I don't know where to put the constructor because it has to be remade, or do I put the list? not sure..

  25. #25
    Member
    Join Date
    May 2012
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a method to move blocks in a grid

    oh maybe for 4) I do for(GRect e : l) this.add(e);

Page 1 of 3 123 LastLast

Similar Threads

  1. BufferedReader.read() blocks,when checking if there's any input for some URLs
    By redateksystem in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 27th, 2012, 09:33 AM
  2. Need help making an image move from one side of screen to the other.
    By Xillius200 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 25th, 2011, 12:46 PM
  3. [SOLVED] Things Won't Move
    By zeraxis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 2nd, 2011, 08:46 AM
  4. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  5. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 08:12 AM