Probleme d'animation en swing
Bonjour a tous , travaillant sur un jeu en ligne développer en java , je rencontre un problème , je fais un bomberman like et j en suis arriver a l'instant ou je dois créer les bombes tout en laissant le joueur se déplacer , pour cela a chaque fois que le joueur appuie sur SPACE et bien une bombe apparait et on gére l'explosion et tout se qui va avec ,je travaille avec la fonction PaintComponent(Graphics g) je dessine un cercle qui represente le joueur au début et quand la condition du keylistenner et rempli /*barre espace enfoncer*/ et bien je redessine un autre rond dans la condition seul probleme le cercle disparait a chaque nouvelle appuie de la touche SPACE et donc je me demandais si il n existait pas une facon de rendre le cercle au prealable pour ensuite l'appeller au moment venu voila mon code sachant que c'est un brouillon c'est juste pour tester .
class panel
Code:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Panneau extends JPanel implements KeyListener {
private int posX = 50;
private int posY = 50;
private int pos1 = 50;
private int pos2 = 50;
private boolean Jet;
private int smooth=0;
//partie input
int x=KeyEvent.VK_RIGHT;
int y=KeyEvent.VK_LEFT;
int z=KeyEvent.VK_UP;
int t=KeyEvent.VK_DOWN;
int space=KeyEvent.VK_SPACE;
public void paintComponent(Graphics g){
g.setColor(Color.black);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.blue);
g.fillOval(posX, posY, 50, 50);
if(spaceB.isPressed())
{
System.out.println("Bombe poser");
pos1=posX+12;
pos2=posY-5;
Jet=true;
}
if(Jet)
{
for(int i=0;i<2;i++)
{
pos2-=3;
pos1+=0;
g.fillOval(pos1, pos2, 25, 25);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}
public class Key
{
public boolean pressed = false;
public void toggle(boolean isPressed)
{
pressed=isPressed;
}
public boolean isPressed()
{
return pressed;
}
}
public int getPosX()
{
return posX;
}
public void setPosX(int posX)
{
this.posX = posX;
}
public int getPosY()
{
return posY;
}
public void setPosY(int posY)
{
this.posY = posY;
}
public Key up = new Key();
public Key down = new Key();
public Key right = new Key();
public Key left = new Key();
public Key spaceB = new Key();
public void keyPressed(KeyEvent e)
{
//System.out.println("event fonctionne bien");
toggleKey(e.getKeyCode(),true);
}
public void keyReleased(KeyEvent e)
{
toggleKey(e.getKeyCode(),false);
}
@Override
public void keyTyped(KeyEvent e)
{
}
public void toggleKey(int KeyCode,boolean isPressed)
{
if(KeyCode==KeyEvent.VK_UP)
{System.out.println("event fonctionne bien " +isPressed);
up.toggle(isPressed);}
if(KeyCode==KeyEvent.VK_DOWN)
down.toggle(isPressed);
if(KeyCode==KeyEvent.VK_RIGHT)
right.toggle(isPressed);
if(KeyCode==KeyEvent.VK_LEFT)
left.toggle(isPressed);
if(KeyCode==KeyEvent.VK_SPACE)
spaceB.toggle(isPressed);
}
} |
class frame
Code:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Fenetre extends JFrame{
private Panneau pan = new Panneau();
public int pos2;
public int pos1;
public Fenetre(){
this.setTitle("Animation");
this.setSize(1400, 900);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setContentPane(pan);
addKeyListener(pan);
this.setVisible(true);
//go();
animate();
}
private void animate()
{
int x = pan.getPosX(), y = pan.getPosY();
boolean backX = false;
boolean backY = false;
while(true)
{
while(true){
System.out.println("je sais pas"+pan.up.pressed);
if(pan.right.isPressed())
{
pan.setPosX(x++);
}
if(pan.left.isPressed())
pan.setPosX(x--);
if(pan.down.isPressed())
{
pan.setPosY(y++);
//System.out.println("fonctionne correctement");
}
if(pan.up.isPressed())
pan.setPosY(y--);
if(pan.spaceB.isPressed())
{
System.out.println("touche espace toucher");
}
repaint();
try {
Thread.sleep(3);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
} |
j instancie le tout dans un main...merci do vos réponses.