This Java tip shows how to use tiled layers in mobile programming. The TiledLayer is a little like Sprite but it is mainly meant for backgrounds, roads or other larger areas. TiledLayer consists of a grid of cells, which can be filled with images or tiles.This can be used by game developers for developing tiled layers in their aplications.

 TiledLayer tiledLayer;
Image bg;
int tileData[][]={
         
{11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11},

{14,11,14,11,14,11,11,14,11,14,11,11,14,11,14,11,14,14,11,11,
14,11,11,11,14,11,14,11,14,11,14,14,14,14,11,11,11,11,11,11,
11,14,11,11,14,11,11,14,11,11,14,11,11,14,11,14,14,11,14,11,
14,11,14,11,14,11,14,14,11,14},

{13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,11,11,11,11,
11,13,13,13,13,13,11,11,11,11,11,13,13,13,13,11,11,11,11,11,
13,13,13,13,13,13,13,13,13,13,13, 9, 9, 9, 9, 9,11, 9,11, 9, 
9, 9, 9, 9, 9, 9,11,11,11,11},

{9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,17,17,17,
17,17,9,9,9,9,9,17,17,17,17,17,9,9,9,9,11,11,11,11,
11,9,9,9,9,9,9,9,9,9,9,9,9,8,8,8,8,11,14,11,
8,8,9,9,8,8,8,9,11,11,11},

{8,8,8,9,9,8,8,8,8,8,8,14,8,8,8,8,10,10,10,
10,10,8,8,14,8,8,10,10,10,10,10,8,8,8,8,10,9,10,9,10,
8,8,14,8,8,8,8,14,8,8,8,8,8,8,8,8,10,8,10,9,9,
8,8,9,9,8,8,9,11,11},

{9,9,8,8,8,8,8,9,8,8,8,8,8,8,8,8,10,10,10,
10,10,8,8,8,8,8,10,10,10,10,10,8,8,8,8,10,10,10,10,
10,8,8,9,9,9,9,9,9,8,8,8,8,8,8,8,8,10,8,10,
8,8,8,8,8,8,8,8,8,9,11},

{16,16,10,16,16,10,10,16,10,10,10,16,10,10,10,10,10,10,10,10,
10,16,16,16,16,16,10,10,10,10,10,16,16,16,16,10,10,10,10,10,
10,10,10,10,10,16,16,16,16,16,16,16,16,16,16,16,10,16,10,16,
16,16,16,16,16,16,16,16,16,16},

{10,10,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,10,10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
10,10,10,10,10,10,10,10}
               
};

boolean toggle=false;

public init() {
    
    try {
        bg=Image.createImage("/bg.png");
        
    }catch(Exception e){}
    
    //Initialize the TIle Set
    tiledLayer=new TiledLayer(tileData[0].length,
            tileData.length, bg, 30, 30);
    
    //Set the tile data
    for(int i=0;i<set.length;i++) {
        for(int j=0;j<tileData[0].length;j++) {
            tiledLayer.setCell(j,i, set[i][j]) ;
        }
    }
    
}

// Now paint the Tiled Layer
void paint(Graphics g) {
    tiledLayer.paint(g);
}

public void run() {
    while(true) {
        if(toggle)
            tiledLayer.setAnimatedTile(-1, 7);
        else
            tiledLayer.setAnimatedTile(-1, 10);
        toggle=toggle?false:true;
        
        repaint();
        try {
            Thread.sleep(100);
        }catch(Exception e){}
        
    }
}