Probleme avec la methode repaint ()

Fermé
schine1 Messages postés 1 Date d'inscription lundi 9 octobre 2017 Statut Membre Dernière intervention 9 octobre 2017 - 9 oct. 2017 à 22:18
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 - 10 oct. 2017 à 22:25
Bonjour


j'essaye d'annimer une boulle rouge mais la methode repaint ne semble pas fonctionner
est ce que quelqu'un pourrait m'aider et me dire ou je me suis trompé.
La boulle apparait mais ne bouge pas

la methode main


public class Test {

public static void main(String[] args) {


// TODO Auto-generated method stub
new Fenetre("Mes premieres bouton");
}

}



ma classe fenetre

import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fenetre extends JFrame {
Panel panel=new Panel();

public Fenetre (String titre)
{
this.setSize(300,300);
this.setLocationRelativeTo(null);
this.setTitle(titre);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(new Panel());
this.setVisible(true);


go();
}
public void go()

{

for (int i=0;i<100;i++)
{
int x=panel.getPosx();
int y=panel.getPosy();
x++;
y++;
panel.setPosx(x);
panel.setPosy(y);
panel.repaint();

try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}



la classe panel

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

import javax.swing.JPanel;

public class Panel extends JPanel {
int posx=0;
int posy=0;


public void paintComponent (Graphics g){

g.setColor(Color.red);
g.fillOval(posx, posy, 50, 50);

}


public int getPosx()
{
return posx;
}
public int getPosy()
{
return posy;
}
public void setPosx(int posx)
{
this.posx=posx;
}
public void setPosy(int posy)
{
this.posy=posy;
}


2 réponses

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
10 oct. 2017 à 13:26
Bonjour,

Tu as fait
this.setContentPane(new Panel());
donc tu affiches un Panel qui n'a rien à voir avec le
Panel panel=new Panel();
que tu modifies.

Il faudrait plutôt faire
setContentPane(panel);
0
Bonjour Kx

merci pour le retour
j'ai essaye la méthode
setContentPane(panel);

helas ca ne marche pas non plus
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
10 oct. 2017 à 22:25
J'ai testé le code chez moi et ton cercle se déplace bien avec juste cette modif.
0