Toujours dans le cadre du même projet, il faut maintenant que je trouve comment faire en sorte qu'il n'ajoute 1 a x et/ou y que en appuyant sur des touches, voici mon nouveau code :
Classe Test :
import java.awt.Dimension;
import javax.swing.JFrame;
public class Fenetre extends JFrame {
public static void main(String[] args) {
new Fenetre();
}
private Panneau pan = new Panneau();
public Fenetre() {
this.setTitle("Animation");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setContentPane(pan);
this.setVisible(true);
go();
}
private void go() {
int x = pan.getPosX(), y = pan.getPosY();
boolean backX = false;
boolean backY = false;
while (true) {
if (x < 1)
backX = false;
if (x > pan.getWidth() - 50)
backX = true;
if (y < 1)
backY = false;
if (y > pan.getHeight() - 50)
backY = true;
if (!backX)
pan.setPosX(++x);
else
pan.setPosX(--x);
if (!backY)
pan.setPosY(++y);
else
pan.setPosY(--y);
pan.repaint();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Classe Panneau :
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
private int posX = -50;
private int posY = -50;
public void paintComponent(Graphics g) {
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.fillOval(posX, posY, 50, 50);
}
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;
}
}
Détecter un appui de touche
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.