I am not trying to make a game with an applet, just do some simple tile rendering things. I have come up with a nice effect, animated tiles too. The only problem is sometimes it loads in my browser and sometimes it does not. I am currenly using firefox, if I open the applet with ie it loads more often but still not all the time. I posted my full source and was wondering if someone can tell me why this is happening. Thanks.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Tile_engine extends Applet implements KeyListener, Runnable {
private int keyPressed = -1;
private int screenX = 0;
private int screenY = 0;
private final int TILE_MAX = 11;
private final int TILE_SIZE = 32;
private final int APPLET_WIDTH = 640;
private final int APPLET_HEIGHT = 480;
private final int SKY = 0;
private final int CLOUD_1 = 1;
private final int CLOUD_2 = 2;
private final int RED_BRICKS = 3;
private final int GRASS = 4;
private final int DIRT = 5;
private final int STONE = 6;
private final int QBOX1 = 7;
private final int QBOX2 = 8;
private final int QBOX3 = 9;
private final int QBOX4 = 10;
private final int MAP_ROWS = 15;
private final int MAP_COLS = 65;
private Image background = null;
private Image buffer = null;
private Graphics bgBuff;
private Graphics bufferg;
private Image[] tile = new Image[TILE_MAX];
private final int map[][] =
{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,6,6,6,6,6,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0},
{0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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0,0,0,0,0,0,0,0,3,6,6,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,5,0,0,0,0,0,0,0,0,0,3,6,6,0,0,0,0,5,5,5,5,5,5,5,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0},
{4,4,4,4,4,4,4,4,0,0,4,4,4,4,4,4,4,4,4,4,4,4,0,0,5,0,5,0,0,4,4,4,0,0,0,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,4,4,4,4,6,6,6,4,4,4,4},
{5,5,5,5,5,5,5,5,0,0,5,5,5,5,5,5,5,5,5,5,5,5,0,0,5,0,5,0,0,5,5,5,0,0,0,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0,0,0,5,5,5,5}};
public void init( ) {
tile[SKY] = getImage(getDocumentBase(), "sky.png");
tile[CLOUD_1] = getImage(getDocumentBase(), "cloud1.png");
tile[CLOUD_2] = getImage(getDocumentBase(), "cloud2.png");
tile[RED_BRICKS] = getImage(getDocumentBase(), "redBricks.png");
tile[GRASS] = getImage(getDocumentBase(), "grass.png");
tile[DIRT] = getImage(getDocumentBase(), "dirt.png");
tile[STONE] = getImage(getDocumentBase(), "stone.png");
tile[QBOX1] = getImage(getDocumentBase(), "qbox1.png");
tile[QBOX2] = getImage(getDocumentBase(), "qbox2.png");
tile[QBOX3] = getImage(getDocumentBase(), "qbox3.png");
tile[QBOX4] = getImage(getDocumentBase(), "qbox4.png");
buffer = createImage(TILE_SIZE * MAP_COLS, TILE_SIZE * MAP_ROWS);
bufferg = buffer.getGraphics();
addKeyListener(this);
if(buffer != null) {
int tile_id;
for(int y=0;y<MAP_ROWS;y++) {
for(int x=0;x<MAP_COLS;x++) {
tile_id = map[y][x];
bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
}
}
}
}
public void start( ) {
Thread th = new Thread(this);
// start this thread
th.start();
}
public void run( ) {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true) {
animate_tiles();
try {
Thread.sleep(200);
} catch(InterruptedException ex) {
}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void animate_tiles( ) {
int tile_id;
for(int y=0;y<MAP_ROWS;y++) {
for(int x=0;x<MAP_COLS;x++) {
tile_id = map[y][x];
switch(tile_id) {
case QBOX1: map[y][x] = QBOX2; break;
case QBOX2: map[y][x] = QBOX3; break;
case QBOX3: map[y][x] = QBOX4; break;
case QBOX4: map[y][x] = QBOX1; break;
default: break; //not needed
}
}
}
}
public void paint(Graphics g) {}
public void update(Graphics g) {
//animate_tiles();
if(buffer != null) {
int tile_id;
for(int y=0;y<MAP_ROWS;y++) {
for(int x=0;x<MAP_COLS;x++) {
tile_id = map[y][x];
bufferg.drawImage(tile[tile_id], x * TILE_SIZE, y * TILE_SIZE, this);
}
}
}
paint(bufferg);
g.drawImage(buffer, screenX, screenY, this);
repaint();
}
public void keyReleased( KeyEvent e ) { }
public void keyPressed( KeyEvent e ) { }
public void keyTyped( KeyEvent e ) {
char c = e.getKeyChar();
if(c == 'z') { screenX+=5; }
else if(c == 'c') { screenX-=5; }
}
}