/*
* Created on 06.02.2005@14:00:31
*
* TODO Licence info
*/
package de.tutorials;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
* @author Administrator
*
* TODO Explain me
*/
public class TilesTest extends JFrame {
int screenWidth, screenHeight;
final int TILE_WIDTH = 32;
final int TILE_HEIGHT = TILE_WIDTH;
int tilePosX;
int tilePosY;
final int TILE_COLUMN_CNT = 7;
final int TILE_ROW_CNT = 6;
VolatileImage[] tiles;
BufferStrategy strategy;
private Thread renderer = new Thread() {
public void run() {
int i = 0;
while (true) {
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.clipRect(0, 0, screenWidth, screenHeight);
g.drawImage(tiles[i], tilePosX, tilePosY, TilesTest.this);
i = ++i % tiles.length;
g.dispose();
strategy.show();
try {
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
public TilesTest() {
super("TilesTest");
setDefaultCloseOperation(EXIT_ON_CLOSE);
screenWidth = 320;
screenHeight = 240;
tilePosX = (screenWidth - TILE_WIDTH) / 2;
tilePosY = (screenHeight - TILE_HEIGHT) / 2;
setSize(screenWidth, screenHeight);
setResizable(false);
setVisible(true);
initGFX();
createBufferStrategy(2);
strategy = getBufferStrategy();
}
/**
*
*/
private void initGFX() {
try {
BufferedImage tilesMap = ImageIO
.read(new File("c:/dg_humans32.gif"));
int width = tilesMap.getWidth();
int height = tilesMap.getHeight();
tiles = new VolatileImage[TILE_ROW_CNT * TILE_COLUMN_CNT];
for (int i = 0, y = 0; i < TILE_ROW_CNT; i++) {
for (int j = 0, x = 0; j < TILE_COLUMN_CNT; j++) {
VolatileImage vImg = this.createVolatileImage(TILE_WIDTH,
TILE_HEIGHT);
vImg.getGraphics()
.drawImage(
tilesMap.getSubimage(x, y, TILE_WIDTH,
TILE_HEIGHT), 0, 0, this);
tiles[i * TILE_ROW_CNT + j] = vImg;
x += TILE_WIDTH;
}
y += TILE_HEIGHT;
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TilesTest().start();
}
/**
*
*/
private void start() {
renderer.start();
}
}