[GEOM][TIMER] Ellipse animee
Bonjour,
je voudrais fair afficher les coordonnes (en texte) d'un Ellipse au fur et a mesure que celle-ci bouge dans un fenetre (frame).
J'ai deux classes:
AnimatedFrame.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public class AnimatedFrame{
public static void main(String[] args) {
final JFrame frame = new JFrame();
final int FRAME_WIDTH = 450;
final int FRAME_HEIGHT = 550;
final Ellipse2D.Double shape = new Ellipse2D.Double(10,10,18,15);
ViewerComponent picture = new ViewerComponent(shape);
frame.getContentPane().add(picture);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setTitle("Moving Ellipse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // allow frame to close
frame.setVisible(true);
class BoxMover implements ActionListener {
public void actionPerformed(ActionEvent event){
double x;
double y;
x=shape.getX();
y=shape.getY();
shape.setFrame(x+3,y+3,50,50);
if (shape.getX() >= FRAME_HEIGHT){
shape.setFrame(x+3,y+3,50,50);}
else if (shape.getY() >= FRAME_WIDTH)
shape.setFrame(10+3,10+3,50,50);
frame.repaint();
}
}
ActionListener listener = new BoxMover();
final int DELAY = 100;
Timer t = new Timer(DELAY, listener);
t.start();
}
} |
Et ViewerComponent.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public class ViewerComponent extends JComponent{
private Shape s;
public ViewerComponent(Shape s){
this.s = s;
}
AnimatedFrame ani = new AnimatedFrame();
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fill(s);
g2.setColor(Color.YELLOW);
g2.draw(s);
g2.setColor(Color.RED);
g2.drawString("The shape is: " **** , 20, 450);
}
} |
dans ****: je met quoi? un toString() ? accesseur method ?
P.S: tout les packages necessaires ont ete importes.
Merci.