salut tout le monde
j essaye de me faire un ti environnement graphique pr un jeu mais je me trouve confronté a un ti probleme la.je n arrive pas a afficher 2 elements simultanement dans le meme JFramesoit ca m affiche mon joueur soit ca m affiche mon decor comment faire pr associer les 2...Etant debutant jm y suis peut etre mal pris donc n hesité pas a me dire ce qui ne va pas
je vous fais par de mon code :
ma classe joueur
ma classe Map :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class Joueur extends JPanel{ protected String nom; //Nom du Joueur protected int px ; //Coordonnées X protected int py; // Coordonnées Y protected int vitess; // Vitesse de deplacement du Joueur public String tof = "pics\\mage.jpg"; // Image du Joueur protected Image img; /** Constructeur du Joueur */ public Joueur(int x,int y) { this.px = x; this.py = y; this.vitess = 3; //this.nom = name; //recupération et initialistion de l'objet image this.img = Toolkit.getDefaultToolkit().getImage(this.tof); } /** Methode permettant l affichage du joueur */ public void paint(Graphics g) { Graphics g2d = (Graphics2D) g; g2d.clearRect(0,0,this.getWidth(),this.getHeight()); // vide l'ecran g2d.drawImage(this.img,this.px,this.py,this); } /** Methodes de deplacement du joueur */ public void move_left() { this.px -= this.vitess; repaint(); } public void move_right() { this.px += this.vitess; repaint(); } public void move_up() { this.py -= this.vitess; repaint(); } public void move_down() { this.py += this.vitess; repaint(); } }
et ma classe censé afficher tout ca implementant JFrame
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class Map extends JPanel{ public int[][] Mat = {{1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; private Image rok; private int ImgSize = 16; public Map() { rok = Toolkit.getDefaultToolkit().getImage("pics\\roche.jpg"); }; public void paint(Graphics g) { int i,j; for(i=0;i<this.Mat.length;i++) { for(j=0;j<this.Mat.length;j++) { if(this.Mat[i][j] == 1) g.drawImage(this.rok,i*this.ImgSize,j*this.ImgSize,this); } } } }
merci pour votre aide
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class FrameMain extends JFrame implements KeyListener{ private JPanel Mypane; public Joueur player = new Joueur(30,30); public Map Mymap = new Map(); public FrameMain() { super(); build(); } public void build() { this.setTitle("Move My Fucking Pics"); this.setSize(new Dimension(200,200)); //this.setContentPane(getMyPane()); this.setLocationRelativeTo(null); this.addKeyListener(this); this.add(player); this.add(Mymap); } public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode() == KeyEvent.VK_DOWN) { System.out.println("DOWN"); player.move_down(); } if(e.getKeyCode() == KeyEvent.VK_UP) { System.out.println("UP"); player.move_up(); } if(e.getKeyCode() == KeyEvent.VK_LEFT) { System.out.println("LEFT"); player.move_left(); } if(e.getKeyCode() == KeyEvent.VK_RIGHT) { System.out.println("RIGHT"); player.move_right(); } } public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } }![]()
Partager