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.

Results 1 to 6 of 6

Thread: Problem with array values

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Problem with array values

    I'm trying to get java to draw 100 squares in a grid but it's not working properly. It compiles and runs without error but it only prints 1 square. Uncommenting the println() in makeGrid() shows the array is filled with the right values but the println() in paint() shows every entry is exactly the same (the last entry entered is repeated)
    I have no idea how the values are changed or why.

    import java.awt.*;
    import java.awt.event.*;
     
    public class GridTest extends Canvas
    {
        private int squaresize;
        private int[] grid = new int[100];
        private Rectangle[] rects = new Rectangle[100];
        private int offset = 25;
     
        public Dimension getPreferredSize() {
            return new Dimension(300,300);
        }
     
        public void makeGrid()
        {
            squaresize = 10;
            for(int h=0;h<100;h++)
            {
                grid[h] = h;
                for(int i=0;i<10;i++)
                {
                    for(int j=0;j<100;j+=10)
                    {
                        this.rects[h] = new Rectangle(offset + j, offset + (i*10), squaresize, squaresize);                    
                        //System.out.println(rects[h].toString());
                    }
                }
            }
        }
     
        public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.BLACK);        
            for(int i=0;i<rects.length;i++)
            {
                g2.draw(rects[i]);
                //System.out.println(rects[i].toString());
            }
        }
     
        public static void main(String[] args)
        {
            GridTest gt = new GridTest();
            gt.makeGrid();
            Frame f = new Frame("GridTestFrame");
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            f.add(gt);
            f.pack();
            f.setVisible(true);
        }
    }
    Last edited by Harry Blargle; September 17th, 2011 at 04:05 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with array values

    Did you look at the 100 lines of print outs?
    Did it look like there were different rectangles?
    What was the difference between one rectange and the next one?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Problem with array values

    Yes, like i said, the first println shows the proper x and y values on everything, the second one shows the same x and y value for everything (115)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with array values

    Which is the first println? The one in makeGrid? How many lines does it print? Add a separate counter variable and print that with the rects value:
    System.out.println("cntr=" + cntr++ + " " +rects[h].toString() + ", h=" + h);
    Define cntr as an int outside of the loops.

    The second println in the paint method shows that the contents of the array are all the same.
    Look at how the index to the array changes as you are creating the elements and storing them in the array.
    Last edited by Norm; September 17th, 2011 at 03:29 PM.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    29
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Problem with array values

    i changed it to:
    int h = 0;
    for(int i=0;i<10;i++)
    {
            for(int j=0;j<100;j+=10)
            {
                    rects[h] = new Rectangle(offset + j, offset + (i*10), squaresize, squaresize);
                    h++;
            }
    }

    now it works! thanks for the help Norm

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with array values

    Yep. That looks like it'd work.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Harry Blargle (September 17th, 2011)

Similar Threads

  1. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  2. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  3. Populating a 2D array with textfield values
    By drixnak in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 19th, 2010, 01:20 PM
  4. Assgining values to array indexes
    By chronoz13 in forum Collections and Generics
    Replies: 3
    Last Post: December 28th, 2009, 11:09 PM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM