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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package animation;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Jeu extends JPanel {
private int x, y;
private int vx, vy;
private Timer timer;
ActionListener moveBall;
public Jeu(int x, int y, int vx, int vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
moveBall = new ActionListener() {
/**
* {@inheritDoc}
*/
@Override
public void actionPerformed(ActionEvent e) {
int margin = 2;
// Effacement de l'ancienne position de la boule.
repaint(Jeu.this.x - margin, Jeu.this.y - margin, 40 + 2 * margin, 40 + 2 * margin);
calculerPosition();
// Affichage du nouvel emplacement de la boule.
repaint(Jeu.this.x - margin, Jeu.this.y - margin, 40 + 2 * margin, 40 + 2 * margin);
}
};
}
/**
* {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Shape clip = g.getClip();
if (clip.intersects(x, y, 40, 40)) {
Graphics2D g2d = (Graphics2D) g.create();
//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
g2d.fillOval(x, y, 40, 40);
} finally {
g2d.dispose();
}
}
}
public void init() {
this.setBackground(Color.BLACK);
this.setForeground(Color.BLUE);
timer = new Timer(40, moveBall);
timer.start();
}
private void calculerPosition() {
if (x < 0 || x >= 800 - 10) {
vx = -vx;
}
if (y < 0 || y >= 600 - 10) {
vy = -vy;
}
x += vx;
y += vy;
}
} |
Partager