Problème animation d'une balle en Java
Utilisateur anonyme
-
Utilisateur anonyme -
Utilisateur anonyme -
Bonjour à tous.
J'ai essayé de faire une animation d'une balle qui avance en diagonale dans une fenêtre. Mon problème est le suivant : ma balle s'écrase au milieu de ma fenêtre...
Voici mon code(bien entendu, mes classes sont en réalité séparé :p ) :
::::::::::::::::::::::::::::::Main::::::::::::::::::::::::::
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Fen f = new Fen();
}
}
:::::::::::::::::::::::::::Classe "Fen":::::::::::::::::::::
import javax.swing.JFrame;
public class Fen extends JFrame {
Panneau pan = new Panneau();
public Fen(){
this.setTitle("Ma fenetre");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(pan);
this.setVisible(true);
go();
}
private void go(){
//boolean x = true;
//boolean y = true;
while(true){
int x1 = pan.getX();
x1++;
int y1 = pan.getX();
y1++;
pan.setX(x1);
pan.setY(y1);
pan.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
::::::::::::::::::::::Classe "Panneau":::::::::::::::::::::
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
int x = 0;
int y = 0;
public void paintComponent(Graphics g){
g.setColor(Color.pink);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(x,y,50,50);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Pouvez me dire ce qui cloche?
Merci d'avance et merci de votre attention
griviere42
J'ai essayé de faire une animation d'une balle qui avance en diagonale dans une fenêtre. Mon problème est le suivant : ma balle s'écrase au milieu de ma fenêtre...
Voici mon code(bien entendu, mes classes sont en réalité séparé :p ) :
::::::::::::::::::::::::::::::Main::::::::::::::::::::::::::
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Fen f = new Fen();
}
}
:::::::::::::::::::::::::::Classe "Fen":::::::::::::::::::::
import javax.swing.JFrame;
public class Fen extends JFrame {
Panneau pan = new Panneau();
public Fen(){
this.setTitle("Ma fenetre");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(pan);
this.setVisible(true);
go();
}
private void go(){
//boolean x = true;
//boolean y = true;
while(true){
int x1 = pan.getX();
x1++;
int y1 = pan.getX();
y1++;
pan.setX(x1);
pan.setY(y1);
pan.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
::::::::::::::::::::::Classe "Panneau":::::::::::::::::::::
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Panneau extends JPanel {
int x = 0;
int y = 0;
public void paintComponent(Graphics g){
g.setColor(Color.pink);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.green);
g.fillOval(x,y,50,50);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
Pouvez me dire ce qui cloche?
Merci d'avance et merci de votre attention
griviere42
A voir également:
- Problème animation d'une balle en Java
- Jeux java itel - Télécharger - Jeux vidéo
- Waptrick java football - Télécharger - Jeux vidéo
- Waptrick java voiture - Télécharger - Jeux vidéo
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages