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 2 of 2

Thread: java 2d array is not looping right. i dont understand whyy and how to fix it

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    133
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default java 2d array is not looping right. i dont understand whyy and how to fix it

    i need help with simple tile map. i have 2d array as map.
    so i want to replce numbers in array with rect's.
    0 = green rect
    1 = red rect
    2 = black rect
    and i want it so that the all tile are same size.

    when i run this it the drawing on screen doesnt match the drawing in array and it output random blocks. the code look fine to me but iam sure i am missing some thing.

    HTML Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class levels 
    {
         int level01[][] = {    {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
                                {2,1,2,2,2,2,2,2,2,2,2,2,2,2,2},
                                {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}};
    
    
    
        public levels()
        {
        }
    
    
    
        //@Override
        public void paint(Graphics g)
        {
            for(int y = 0; y < level01.length; y++) //rows
            {
                for(int x = 0; x < level01[y].length; x++) //cols
                {
                    if(level01[y][x] == 0)
                    {
                        g.setColor(Color.green);
                        g.drawRect(x, y, 200, 200);
                    }
                    if(level01[y][x] == 1)
                    {
                        g.setColor(Color.red);
                        g.drawRect(x, y, 200, 200);
                    }
                    if(level01[y][x] == 2)
                    {
                        g.setColor(Color.black);
                        g.drawRect(x, , 200, 200);
                    }
                }
            }//end of main for loop
            //super.paint(g);
        }//end of paint method
    }//end of level01 class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,190
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: java 2d array is not looping right. i dont understand whyy and how to fix it

    when i run this it the drawing on screen doesnt match the drawing in array and it output random blocks
    Can you describe what is drawn?

    Can you make a small simple program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: February 13th, 2013, 08:07 AM
  2. My code has error when its run....I dont understand what's wrong of it.
    By jacky@~ in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 11th, 2011, 07:48 AM
  3. Dont quite understand how Scanner.nextLine() works
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 14
    Last Post: September 21st, 2011, 05:27 PM
  4. I dont understand why this happens....
    By ashenwolf in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 09:31 PM
  5. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 08:31 PM