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

Dani AI

Generated

Short note: the map looks "random" because the loops use array indices as pixel coordinates. Each cell index (x, y) must be converted to pixels by multiplying by a tile size. There's also a compile bug in the posted code (the g.drawRect(x, , 200, 200); line is missing the y argument). Using drawRect(x,y,...) with tiny x/y values will overlap tiles; use a TILE_SIZE constant and fillRect(...) to paint solid tiles, and override paintComponent (call super.paintComponent(g)) so the background is cleared.

A concise, correct pattern:

private static final int TILE_SIZE = 32;

@Override
protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  for (int row = 0; row < map.length; row++) {
    for (int col = 0; col < map[row].length; col++) {
      int value = map[row][col];
      int px = col * TILE_SIZE;
      int py = row * TILE_SIZE;
      g.setColor(value == 0 ? Color.GREEN : value == 1 ? Color.RED : Color.BLACK);
      g.fillRect(px, py, TILE_SIZE, TILE_SIZE);
    }
  }
}

Extra practical tips: compute TILE_SIZE from the panel size if you want tiles to scale (TILE_SIZE = Math.min(getWidth()/cols, getHeight()/rows)). If you want outlines, draw a thin border after filling each tile. Don’t forget Swing best-practices: update UI on the EDT, call repaint() when the map changes, and avoid heavy work inside paintComponent (pre-render repeating tiles to BufferedImage for better performance).

Credit to for the multiplication hint — that’s the key fix for the layout issue reported by .

Recommended Answers

All 2 Replies

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.