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.

    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

Do you really want to draw these rectangles at (x, y)? That's only going to put one pixel between the top-left corner of the first rectangle and the top-left corner of the second rectangle. I think you would get better results by drawing each rectangle at (x * 200, y * 200). At least then there would be less overlap between rectangles.

ahh ic thanks man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.