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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
package main;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Jeu_Fenetre extends JFrame {
//
private Jeu_Panneau JPan1 =new Jeu_Panneau();
JPanel pan =new JPanel();
public Jeu_Fenetre(){
this.setTitle("TP-Calculette");
this.setSize(550, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan.setLayout(new BorderLayout());//indique qu'il faut prendre compte le positionnemt des touches
getContentPane().add(pan);//ajout du JPanel pour le rendre visible
//Apparence
affichage01();
pan.add(JPan1);//fond et autres
//rendre visible
this.setVisible(true);
//
go01();
}
//---------------------------------------------Animation du joueur---------------------------------------------
//---------------------------------------------Action lors du clic---------------------------------------------
protected void affichage01(){
//cretaion du JPanel qui contiendra des boutons
JPanel pan_b=new JPanel();//b=bouton
pan.add(pan_b , BorderLayout.SOUTH);
//Creation des boutons
JButton bHaut=new JButton ("Haut");
JButton bBas=new JButton ("Bas");
//Ajout d'action au boutons
bHaut.addActionListener(new bH_Listener());
bBas.addActionListener(new bB_Listener());
//Ajout des boutonss dans le Panel
pan_b.setLayout(new GridLayout(1,0,5,2));//ds l'ordre (ligne,colonne,espacement de 5px(x;y)
pan_b.add(bHaut);
pan_b.add(bBas);
}
//Var
//"Thread"Variable qui permettra de lancer l'action
private Thread t1;//vers le haut
private Thread t2;//vers le bas
//Va ns permettre de savoir quel bouton est cliqué
boolean animated_bH;
boolean animated_bB;
//
//---------Fct des animations-------
//Animation vers le haut
public void Jgo01(){
//Va nous permettre de savoir la position du joueur
int x_j=JPan1.getPosX_j();
int y_j=JPan1.getPosY_j();
for(;;){
y_j--;
JPan1.setPosY_j(y_j);
JPan1.repaint();
if (Math.abs( y_j ) == 0){
JPan1.setPosY_j(30);
break;
}try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//Animation vers le bas
public void Jgo02(){
//Va nous permettre de savoir la position du joueur
int x_j=JPan1.getPosX_j();//j,pour joueur
int y_j=JPan1.getPosY_j();
int y_P=JPan1.getHeight()-70;//P,pour panel//Cette variable nous permet de replacer l'icon qd elle quitte l'ecran
for(;;){
y_j++;
JPan1.setPosY_j(y_j);
JPan1.repaint();
if (Math.abs( y_j ) == JPan1.getHeight()){
JPan1.setPosY_j(y_P);
break;
}try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class bH_Listener implements ActionListener{//(*)Ici les classes ineternes st decrites
public void actionPerformed(ActionEvent arg0) {
//
animated_bH=true;
animated_bB=false;
//
if(animated_bH=true){
t1 = new Thread(new PlayAnimation());
t1.start();
}
if(animated_bH=false){
t1.stop();
}
}
}
public class bB_Listener implements ActionListener{//(*)Ici les classes ineternes st decrites
public void actionPerformed(ActionEvent arg0) {
//
animated_bH=false;
animated_bB=true;
//
if(animated_bB=true){
t2 = new Thread(new PlayAnimation());
t2.start();
}
if(animated_bB=false){
t2.stop();
}
}
}
//
class PlayAnimation implements Runnable{
public void run() {
while(animated_bH=true){
Jgo01();
}
while(animated_bB=true){
Jgo02();
}
}
}
//---------------------------------------------FIN Action lors du clic---------------------------------------------
//---------------------------------------------Animation des obstacles ---------------------------------------------
public void go01(){
int i;
for(i=0;;i++){
//
int x = JPan1.getPosX(), y = JPan1.getPosY();
int xl =JPan1.getPosXl(),yl=JPan1.getPosYl();
//
x--;
JPan1.setPosX(x);
JPan1.setPosY(y);
JPan1.repaint();
if(Math.abs( x ) <=0){
JPan1.setPosX(1000);
JPan1.setPosY(200);
//
JPan1.setPosYl(900);
}try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
i=0;
}
}
//---------------------------------------------FIN Animation des Obstacles---------------------------------------------
} |
Partager