J'ai finit la création aléatoire de map, et le déplacement sur celle ci.
Je voudrais désormais placer une couche supérieur ou je pourrais afficher, objets, monstres, et mon personnage.
Cette couche ne doit pas cacher celle du dessous.
J'ai tout essayé, bidouillé de partout, mangé de la doc, mais décidement, les JlayeredPane ce n'est pas mon fort.. :'(
Merci d'avance :D
Voici mon code :
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main extends JFrame{
/**
* @param args
* @throws InterruptedException
*/
private static int h = 250;
private static int l = 250;
private static boolean down = false;
private static boolean up = false;
private static boolean left = false;
private static boolean right = false;
public static void main(String[] args) throws InterruptedException {
JLabel map [][] = new JLabel[10][10];
long grille [][] = new long [5000][5000];
for (int a = 0;a<10;a++){
for (int b = 0;b<10;b++){ // on remplit le tableau d'affichage de JLABELs
map[a][b] = new JLabel();
}
}
for (int a = 0;a<5000;a++){
for (int b = 0;b<5000;b++){
grille [a][b] = (int) (Math.random()*(8-1)+1); // On genere la map au hasard.
}
}
JFrame fenetre = new JFrame();
fenetre.setTitle("Dude! ");
fenetre.setSize(350, 350);
fenetre.setLocationRelativeTo(null);
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setVisible(true);
fenetre.setResizable(true);
fenetre.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) { //KeyListener \o/
if (e.getKeyCode()==KeyEvent.VK_DOWN) {
down = true;
}
if (e.getKeyCode()==KeyEvent.VK_UP) {
up = true;
}
if (e.getKeyCode()==KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
right = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_DOWN) {
down = false;
}
if (e.getKeyCode()==KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode()==KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
right = false;
}
}
public void keyTyped(KeyEvent arg0) {
}
});
ImageIcon Grass = new ImageIcon("grass.jpeg");
ImageIcon Dirt = new ImageIcon("dirt.jpeg");
ImageIcon Human = new ImageIcon("human.jpeg");
ImageIcon Sand = new ImageIcon("sand.jpeg");
GridLayout gl = new GridLayout();
gl.setColumns(10);
gl.setRows(10);
fenetre.setLayout(gl);
for (int ii = 0; ii<10;ii++){
for(int jj = 0; jj<10;jj++){
fenetre.getContentPane().add(map[ii][jj]);
}
}
while (true){
if (up)h--;
if(down)h++;
if(left)l--;
if(right)l++;
for (int i = 0; i<10;i++){
for(int j = 0; j<10;j++){
if (grille[i+h][j+l] == 1) {
map[i][j].setIcon( Dirt );
}
if (grille[i+h][j+l] == 2 ||grille[i+h][j+l] == 3 ||grille[i+h][j+l] == 4 ||grille[i+h][j+l] == 5 ||grille[i+h][j+l] == 6 ) {
map[i][j].setIcon( Grass );
}
if (grille[i+h][j+l] == 7) {
map[i][j].setIcon( Dirt );
}
}
}
Thread.sleep(550);
}
}
}