So my project is to create a shoot'em up type of game and i'd like that when i press arrows my square (that will after be the spaceship) change x;y position. We managed to have a test that the keylistener works so we're sure that the problem comes from the repaint function.

Pls help, here's our code :

Class n°1

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 java.awt.Dimension; 

import javax.swing.JFrame;

public class Fenetre extends JFrame implements KeyListener {
Panneau pan = new Panneau();
  public Fenetre(){             
    //nom de la fenetre
    this.setTitle("Projet");
    //taille
    this.setSize(450, 600);
    //centrer la fenetre
    this.setLocationRelativeTo(null);
    //Fenetre toujours en premier plan
    this.setAlwaysOnTop(true);
    //arrêt du processus à la fermeture de la fenêtre
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Fenetre non redimensionnable
    this.setResizable(false);
    //Couleur du fond
    this.setBackground(Color.DARK_GRAY);   

    //Instanciation d'un objet JPanel
    JPanel pan = new JPanel();
    JPanel tir = new JPanel();
    //On prévient notre JFrame que notre JPanel sera son content pane
    this.setContentPane(new Panneau());

    this.setVisible(true);
    this.addKeyListener(this);
  }
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}


@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    int c = e.getKeyCode();
        if(c==KeyEvent.VK_RIGHT){
            int x = pan.getPosX();
            x=x+10;
            pan.setPosX(x);
            pan.repaint();
            System.out.print("Droite ");}
        if(c==KeyEvent.VK_LEFT){
            int x = pan.getPosX();
            x=x-10;
            pan.setPosX(x);
            pan.repaint();
            System.out.print("Gauche ");}
        if(c==KeyEvent.VK_UP){
            int y = pan.getPosY();
            y=y+10;
            pan.setPosY(y);
            pan.repaint();
            System.out.print("Haut ");}
        if(c==KeyEvent.VK_DOWN){
            int y = pan.getPosY();
            y=y-10;
            pan.setPosY(y);
            pan.repaint();
            System.out.print("Bas ");}
        if(c==KeyEvent.VK_SPACE){
             this.setBackground(Color.CYAN);



        }


}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int c = e.getKeyCode();
    if(c==KeyEvent.VK_SPACE){
         this.setBackground(Color.DARK_GRAY);}
}





 }

Class n°2

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

import java.awt.Graphics;

import javax.swing.JPanel;

import java.awt.Graphics;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Panneau extends JPanel {

  public int posX = 450/2-15;
  public int posY = 500;

public void paintComponent(Graphics g){
    g.setColor(Color.red);
    g.fillRect(posX, posY, 30, 30);
    System.out.print("Carré ");
 }
  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;
  }        

}

And our main :

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;


public class Main extends JFrame {
public static void main (String [] Args){
    Fenetre fen = new Fenetre();


}}

PS: We're beginners so we might well have done a awful lots of things wrong

有帮助吗?

解决方案

With

Panneau pan = new Panneau();

you are creating the instance of Panneau which is modified in the keyPressed method. But with

this.setContentPane(new Panneau());

you are adding a new instance of Panneau to the frame - this instance is not modified in the keyPressed method.

There are some other issues with the code, but the minimal change that is necessary to achieve the desired effect is to change this line to

this.setContentPane(this.pan);

In order to clear the background of the panel (that is, remove the previously painted rectangle) you also have to call super.paintComponent(g) in the first line of your paintComponent method:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.red);
    g.fillRect(posX, posY, 30, 30);
    System.out.print("Carré ");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top