@Override
	public void paintComponent(Graphics g)
	{
		// Create an offscreen BufferedImage to simplify drawing the level view.
		BufferedImage img = new BufferedImage(VIEW_W, VIEW_H,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2D = img.createGraphics();
		g2D.drawImage(background, 0, 0, null);
		// Calculate the view offset.
		int offsetX = -(viewX % Tile.TILE_WIDTH);
		int offsetY = -(viewY % Tile.TILE_WIDTH);
		int firstR = viewY / Tile.TILE_WIDTH;
		int firstC = viewX / Tile.TILE_WIDTH;

		// Draw any tiles that are fully or partially in the view of the screen.
		for (int row = 0; row <= Level.VIEW_ROWS && row + firstR < grid.length; row++)
			for (int col = 0; col <= Level.VIEW_COLS
					&& col + firstC < grid[row].length; col++) {
				grid[row + firstR][col + firstC].drawTile(g2D, offsetX
						+ Tile.TILE_WIDTH * col, offsetY + Tile.TILE_WIDTH
						* row);
			}

		// Draw a selection box around the selected tile.
		g2D.drawImage(selectorBox,
				(selectCol * Tile.TILE_WIDTH - viewX - offsetX)
						/ Tile.TILE_WIDTH * Tile.TILE_WIDTH + offsetX,
				(selectRow * Tile.TILE_WIDTH - viewY - offsetY)
						/ Tile.TILE_WIDTH * Tile.TILE_WIDTH + offsetY,
				Tile.TILE_WIDTH, Tile.TILE_WIDTH, null);

		// Draw the image to the screen and add the border.
		g.drawImage(img, VIEW_X, VIEW_Y, null);
		if (borderFrame != null)
			((Graphics2D) g).drawImage(borderFrame, 0, 0, null);

		// Draw the sidebar.
		drawPrimaryControls(g);
		drawColourPalette(g);
		drawTilePalette(g);

		((Graphics2D) g).drawImage(saveButton, SAVE_X, SAVE_Y, null);
		((Graphics2D) g).drawImage(exitButton, EXIT_X, EXIT_Y, null);
		((Graphics2D) g).drawImage(renameButton, RENAME_X, RENAME_Y, null);
		((Graphics2D) g).drawImage(removeButton, REMOVE_X, REMOVE_Y, null);

		// Draw the dragged icon.
		if (draggedIcon >= 0 && draggedIcon < 10 && dragPos != null) {
			BufferedImage image = tileDragIcons[draggedIcon];
			int xPos = dragPos.x - dragOffsetX;
			int yPos = dragPos.y - dragOffsetY;
			((Graphics2D) g).drawImage(image, xPos, yPos, null);
		}
	}