โTileโ in a 2D game simply means โthe image is the smallest screen that you can reuse several times to create the background.โ
Here is a working example in which four tiles are created (adding some random noise to each pixel). Each tile has a size of 50x50 pixels.
Then a โmapโ (which you call a โgridโ in your case) will appear, representing which fragments you want to place where.
A larger BufferedImage is created from this map (note that this is just an example; in a real program, you will want to use a copy of BufferedImage rather than a pixel copy).
The map is 9x7, each tile is 50x50 pixels, so the resulting image is 9 * 50 x 7 * 50 (i.e. 450 by 350).
Note that the following is actually just a simple example, as short as possible, showing how to create a larger BufferedImage using a few fragments: the goal is not to give a tutorial on the best use of Swing, nor how to compress each bit performances from BufferedImages etc.
import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; public class ToyTiled extends JFrame { private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB; private BufferedImage img; public static void main( String[] args ) { new ToyTiled(); } public ToyTiled() { super(); this.add(new JPanel() { @Override protected void paintComponent(Graphics g) { g.drawImage(img, 0, 0, null); } }); img = new BufferedImage( 450, 350, IMAGE_TYPE ); // here you should create a compatible BufferedImage this.setSize(img.getWidth(), img.getHeight()); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); final int NB_TILES = 4; BufferedImage[] tiles = new BufferedImage[NB_TILES]; tiles[0] = createOneTile( new Color( 255, 255, 255 ) ); tiles[1] = createOneTile( new Color( 255, 0, 255 ) ); tiles[2] = createOneTile( new Color( 0, 0, 255 ) ); tiles[3] = createOneTile( new Color( 0, 255, 255 ) ); final int[][] map = new int[][] { { 1, 0, 2, 3, 0, 1, 2, 2, 2 }, { 0, 2, 3, 0, 1, 2, 2, 2, 3 }, { 1, 0, 2, 3, 0, 1, 2, 2, 2 }, { 2, 1, 0, 1, 2, 3, 2, 0, 0 }, { 1, 0, 2, 3, 0, 1, 2, 2, 3 }, { 1, 0, 2, 2, 1, 1, 2, 2, 3 }, { 1, 0, 2, 3, 0, 1, 2, 2, 3 }, }; for (int i = 0; i < map[0].length; i++) { for (int j = 0; j < map.length; j++) { final BufferedImage tile = tiles[map[j][i]]; for (int x = 0; x < tile.getWidth(); x++) { for (int y = 0; y < tile.getHeight(); y++) { img.setRGB( x + i * 50, y + j * 50, tile.getRGB(x,y) ); } } } } this.setVisible( true ); } private BufferedImage createOneTile( final Color c ) { final Random r = new Random(); final BufferedImage res = new BufferedImage( 50, 50, IMAGE_TYPE ); for (int x = 0; x < res.getWidth(); x++) { for (int y = 0; y < res.getHeight(); y++) { res.setRGB( x, y, c.getRGB() - r.nextInt(150) ); } } return res; } }