So I have been working on this tutorial for a couple of days, and I noticed that when I wrote a class that extended Applet I had to click the X twice.
When I click it the first time the "canvas" disappears but the window remains. Then with a second click to the X the window disappears.
I have looked around and found little help. I know how to do this with JFrame and Frame, but I don't have much experience with Applet.
I have tried to do it the same as I would with Frame, but thats not right. Before writing this I tried to get a hold of the parent of my Applet hoping I could add a WindowListener, but I had little to no success.
Any help would be great. Heres my code...I'm thinking the problem is in the init(), but there might be something else I'm missing. And heres the link to the tutorial if you would like a reference for 2D game java development.
package edu.KiloBotGame.njh;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Renderer extends Applet{
static int [][] map;
static int rows, cols;
public Renderer() {
// TODO Auto-generated constructor stub
}
@Override
public void init() {
setSize(800,480);
setBackground(Color.BLACK);
createTileMap();
}
private void createTileMap() {
map = new int[50][30];
rows = map.length;
cols = map[0].length;
Random r = new Random();
for(int i = 0; i < rows; i++){
for(int j = 0;j < cols; j++){
map[i][j] = r.nextInt(5);
}
}
}
@Override
public void paint(Graphics g) {
for(int i =0; i < rows; i ++){
for(int j =0; j < cols;j++){
int mod_i = i*16;
int mod_j = j*16;
switch(map[i][j]){
case 0:
g.setColor(Color.RED);
g.fillRect(mod_i, mod_j, 16, 16);
break;
case 1:
g.setColor(Color.BLUE);
g.fillRect(mod_i, mod_j, 16, 16);
break;
case 2:
g.setColor(Color.YELLOW);
g.fillRect(mod_i, mod_j, 16, 16);
break;
case 3:
g.setColor(Color.GREEN);
g.fillRect(mod_i, mod_j, 16, 16);
break;
case 4:
g.setColor(Color.ORANGE);
g.fillRect(mod_i, mod_j, 16, 16);
break;
}
}
}
}
}