bonjour
bon je voulais créer un petit jeu java , qui consiste a boujer un objet en utilisant les keyListner (clavier ) mais il me semble qu'il y a un petit probleme care le programme ne réponds pas aux touche haut bas ....
voila mon code :
package jeu;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Graph extends JPanel implements KeyListener {
int posix;// position de notre personnage par rapport a
int posiy;
// int diametre=
static final long serialVersionUID = 1;
private int[][][] Coordonnepoint; // voila le tableau de coordonn
private int[][] coordonneMure;// por le mure //
private boolean possedeDisque = true;
private int px;
private int py;
private int prayon=110;
private int x = 1500;
private int y = 150;
private int z = 0;
private int w = 0;
private Graphics g;
public Graph() {
px=0;
py=0;
setBackground(Color.BLACK);
setPreferredSize(new Dimension(2000, 2000));
}
public void setPossedeDisque(boolean possedeDisque) {
this.possedeDisque = possedeDisque;
}
public void dessiner(Graphics g) {
g.setColor(Color.YELLOW);
// g.fillOval(500,500, 20, 20);
g.fillOval(45, 150, 20, 20);
g.fillOval(45, 250, 20, 20);
g.fillOval(45, 350, 20, 20);
g.fillOval(45, 450, 20, 20);
g.fillOval(45, 550, 20, 20);
g.fillOval(45, 650, 20, 20);
g.fillOval(145, 780, 20, 20);
g.fillOval(255, 50, 20, 20);
g.fillOval(255, 150, 20, 20);
g.fillOval(255, 250, 20, 20);
g.fillOval(255, 350, 20, 20);
g.fillOval(255, 450, 20, 20);
g.fillOval(255, 550, 20, 20);
g.fillOval(255, 650, 20, 20);
g.fillOval(350, 50, 20, 20);
g.fillOval(500, 50, 20, 20);
g.fillOval(500, 150, 20, 20);
g.fillOval(500, 250, 20, 20);
g.fillOval(500, 350, 20, 20);
g.fillOval(500, 450, 20, 20);
g.fillOval(500, 550, 20, 20);
g.fillOval(500, 650, 20, 20);
g.fillOval(500, 750, 20, 20);
g.fillOval(600, 750, 20, 20);
g.fillOval(700, 750, 20, 20);
g.fillOval(800, 750, 20, 20);
g.fillOval(900, 750, 20, 20);
g.fillOval(1000, 750, 20, 20);
g.fillOval(1100, 750, 20, 20);
g.fillOval(1200, 750, 20, 20);
g.fillOval(1300, 750, 20, 20);
g.fillOval(1400, 750, 20, 20);
g.fillOval(1500, 750, 20, 20);
g.fillOval(1500, 650, 20, 20);
g.fillOval(1500, 550, 20, 20);
g.fillOval(1500, 450, 20, 20);
g.fillOval(1500, 350, 20, 20);
g.fillOval(1500, 250, 20, 20);
g.fillOval(1500, 150, 20, 20);
g.fillOval(1500, 50, 20, 20);
}
public void Encaisse() {
}
// g.fillOval(500, 50, 20, 20);
// g.fillOval(500, 50, 20, 20);
// g.fillOval(x, y, width, height)
/**
* methode qui permet de mettre monterer que les points jaunes , sont
* concommées , on les rendant noires
*
* @param g
*/
public void mettreEnNoire(Graphics g) {
g.setColor(Color.black);
g.fillOval(x, y, z, w);
}
/**
* methode qui nous permet de dessiner le joueur a chaque moment , par
* variation des position X, Y
*
* @param g
*/
public void dessinerPer(Graphics g) {
g.setColor(Color.RED);
g.fillOval(px, py, prayon, prayon);
}
// TODO verifier ces classes //
/**
* la methode qui nous permet de dessiner les mures dans nos parties de jeux
*
* @param g
*/
public void dessinerMure(Graphics g) {
// String color="GRAY";
g.setColor(Color.GRAY);
g.fillRect(110, 0, 100, 745);
g.fillRect(330, 110, 100, 850);
g.fillRect(640, 0, 100, 400);
// g.fillRect(640,400, 300, 100);
g.fillRect(1040, 0, 100, 400);
// g.fillRect(110,750, 100, 100);
}
// public void dessinerPoint(Graphics g){
// g.setColor(Color.yellow);
// g.fillOval(x, y, width, height);
//
// }
/**
* Methode implementé de Jcomponment qui nous permet de dessiner tous les
* Labyrinth
*
* @param Graphics
* g
*/
@Override
// public void paintComponent(Graphics g) {
public void paint(Graphics g) {
super.paintComponent(g);
if (possedeDisque) {
dessiner(g);
dessinerMure(g);
dessinerPer(g);
mettreEnNoire(g);
// this.paintImmediately(100,
// 100 ,
// 100,
// 100);
}
}
@Override
public void keyPressed(KeyEvent e) {
// super.keyPressed(e);
// enter = sélectionner l'option
// if (e.getKeyCode() == KeyEvent.VK_SPACE) {
// //TODO pause en jeux
// }
// se déplacer dans les options vers le bas
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
this.px=px+100;
// this.dessiner(g);
// this.paintComponent(g);
}
// se déplacer vers le haut
if (e.getKeyCode() == KeyEvent.VK_UP) {
this.px=px+100;
// this.dessiner(g);
// this.paintComponent(g);
// this.repaint();
}// se deplacer vers l'adroite
if(e.getKeyCode()==KeyEvent.VK_LEFT){
this.py=py+100;
// this.dessiner(g);
// this.paintComponent(g);
// this.repaint();
}
// se deplacer vers l'agauche
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
this.py=py-100;
// this.dessiner(g);
this.repaint();
// this.paintComponent(g);
}
repaint();
}
// public void paint(Graphics g){
// dessiner(g);
// dessinerMure(g);
// dessinerPer(g);
//
// }
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
le programme test
// public class VoirArdoise {
// public static void main(String[] arg) {
// Frame cadre = new javax.swing.JFrame("PROTOTYPE1");
// ((JFrame) cadre).setContentPane(new Graph());
// cadre.setLocation(400, 300);
// cadre.pack();
// cadre.setVisible(true);
// ((JFrame) cadre).setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// }
// }
Partager