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
| import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Ivelios
*/
public class CoupEpee extends JPanel implements Runnable{
private int coup = 0;//Progression du coup
private boolean coupEnCours;//Le joueur est en train de faire un coup
public CoupEpee(){
super();//Appel du constructeur de JFrame; super("titre") pour mettre le titre
this.setLayout(null);
//La fenêtre
JFrame f = new JFrame("frame");
f.setSize(200,200);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Le boutton
JButton action = new JButton("Action");
action.setBounds(100, 100, 70, 35);
action.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!CoupEpee.this.coupEnCours)//Si aucun coup n'est en cours
new Thread(CoupEpee.this).start();//Démarrage du coup d'épée
}
});
this.add(action);
f.setContentPane(this);
f.setVisible(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Arrière plan
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, 400, 400);
//Le coup d'épée
g.setColor(Color.white);
if (coup == 1) g.fillRect(10, 10, 30, 30);
else if(coup == 2) g.fillRect(10, 50, 30, 30);
else if(coup == 3) g.fillRect(10, 90, 30, 30);
else if(coup == 4) g.fillRect(10, 130, 30, 30);
}
public void run() {
//Début du coup
coupEnCours = true;
//Progression du coup et actualisation du panel
for(int i =1;i<5;i++){
coup = i;
this.repaint();
try {Thread.sleep(300);} catch (InterruptedException e) {}
}
//Fin du coup
coup = 0;
this.repaint();
coupEnCours = false;
}
/** Lanceur */
public static void main(String[] args){ new CoupEpee();}
} |
Partager