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
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import test.Joueur.*;
/**
*
* @author Ivelios
*/
public class Affichage extends JPanel implements KeyListener{
private Joueur joueur1, joueur2, joueur3, joueur4;
//Constructeur
public Affichage(){
joueur1 = new Joueur();
joueur2 = new Joueur();
joueur3 = new Joueur();
joueur4 = new Joueur();
new Thread(new Deplacement(joueur1,joueur2,joueur3,joueur4,this)).start();
this.setFocusable(true);
this.addKeyListener(this);
}
/************* Affichage ****************/
public void paintComponent(Graphics g) {
//fond
g.setColor(Color.WHITE);g.fillRect(0,0, 400, 400);
//Joueurs
g.setColor(Color.BLUE);g.fillOval(joueur1.getPosX(), joueur1.getPosY(), 40, 40);
g.setColor(Color.ORANGE);g.fillOval(joueur2.getPosX(), joueur2.getPosY(), 40, 40);
g.setColor(Color.BLACK);g.fillOval(joueur3.getPosX(), joueur3.getPosY(), 40, 40);
g.setColor(Color.RED);g.fillOval(joueur4.getPosX(), joueur4.getPosY(), 40, 40);
}
/************ Gestion de l'appuie sur une touche ************************/
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
int touche = e.getKeyCode();
this.appuieTouche(90,81,83,68,touche,joueur1);//zqsd
this.appuieTouche(38,37,40,39,touche,joueur2);//fleches directionnelles
this.appuieTouche(85,72,74,75,touche,joueur3);//uhjk
this.appuieTouche(104,100,101,102,touche,joueur4);//8456
}
public void keyReleased(KeyEvent e) {
int touche = e.getKeyCode();
this.relacherTouche(90,81,83,68,touche,joueur1);//zqsd
this.relacherTouche(38,37,40,39,touche,joueur2);//fleches directionnelles
this.relacherTouche(85,72,74,75,touche,joueur3);//uhjk
this.relacherTouche(104,100,101,102,touche,joueur4);//8456
}
private void appuieTouche(int haut,int gauche,int bas,int droite,int touche,Joueur j){
if(touche == gauche){ j.setDx( DEPLACEMENT_X.GAUCHE );}
else if(touche == droite){ j.setDx( DEPLACEMENT_X.DROITE );}
else if(touche == haut){j.setDy( DEPLACEMENT_Y.HAUT );}
else if(touche == bas){j.setDy( DEPLACEMENT_Y.BAS ); }
}
private void relacherTouche(int haut,int gauche,int bas,int droite,int touche,Joueur j){
if( (touche == haut && j.getDy() == DEPLACEMENT_Y.HAUT) || (touche == bas && j.getDy() == DEPLACEMENT_Y.BAS) ){
j.setDy( DEPLACEMENT_Y.RIEN );
}else if( (touche == gauche && j.getDx() == DEPLACEMENT_X.GAUCHE) || (touche == droite && j.getDx() == DEPLACEMENT_X.DROITE) ){
j.setDx( DEPLACEMENT_X.RIEN );
}
}
/******************** Lanceur ******************************/
public static void main(String[] args){
JFrame f = new JFrame("Test");
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Affichage panel = new Affichage();
f.add(panel,BorderLayout.CENTER);
f.setVisible(true);
}
} |
Partager