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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| public class Champignon {
public static final int SPRITE_SIZE = 64;
private int x;
private int y;
private final Image image;
private boolean visible;
public Champignon() {
image = loadImage();
}
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;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
private Image loadImage() {
// ici je construis l'image en mémoire pour que la classe puisse marcher sans fichier,
// mais c'est juste pour l'exemple
// ne fait pas attention à ce code et imagine que ça charge un fichier .png
BufferedImage image = new BufferedImage(SPRITE_SIZE, SPRITE_SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(SPRITE_SIZE/4, SPRITE_SIZE/2, SPRITE_SIZE/2, SPRITE_SIZE/2);
g.setColor(Color.RED);
g.fillArc(0, 0, SPRITE_SIZE, SPRITE_SIZE, 0, 180);
}
finally {
g.dispose();
}
return image;
}
public void drawSprite(Graphics g) {
g.drawImage(image, x, y, null);
}
// exemple de programme utilisant ce sprite de champignon
public static void main(String[] args) {
final int nbChamp = 10;
// l'écran de jeu où on va afficher les champignons
class GameBoard extends JPanel {
private Champignon[] champignons; // l'ensemble des champignons à afficher
public GameBoard() {
super(true);
setBackground(Color.BLACK);
// initialise les champignons
champignons = new Champignon[nbChamp];
for(int i=0; i<champignons.length; i++) {
Champignon champignon = new Champignon();
champignon.setX(SPRITE_SIZE/2+i*SPRITE_SIZE*2);
champignons[i] = champignon;
}
}
// on redéfinit la méthode de dessin de JPanel pour qu'il dessine les champignons
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for(Champignon champignon : champignons) { // on parcourt tous les champignons
if ( champignon.isVisible() ) { // si le champignon est visible
champignon.drawSprite(g); // je l'affiche
}
}
}
/**
* permet de manipuler un champignon par son index
*/
public Champignon getChampignon(int i) {
return champignons[i];
}
}
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
GameBoard gameBoard = new GameBoard();
panel.add(gameBoard);
// on créé une checkbox pour chaque champignon
JPanel panelCheckbox = new JPanel(new FlowLayout(FlowLayout.CENTER));
for(int i=0; i<nbChamp; i++) {
JCheckBox checkbox = new JCheckBox(String.valueOf(i+1));
int index = i;
// permet à la boite à cocher de modifier la visibilité du champignon auquel elle est associée
checkbox.addActionListener(e-> {
gameBoard.getChampignon(index).setVisible( checkbox.isSelected() ); // mettre la visibilité du champignon à jour par rapport à l'état de la checkbox
gameBoard.repaint(); // mettre à jour l'affichage
});
panelCheckbox.add(checkbox);
}
panel.add(panelCheckbox, BorderLayout.SOUTH);
// une petit timer pour l'animation, à la limite, tu ignores ça si tu ne comprends pas comment ça fonctionne
int[] dy = new int[nbChamp];
for(int i=0; i<dy.length; i++) {
dy[i]=Math.max(SPRITE_SIZE/10,2);
}
Timer timer = new Timer(33,e-> {
boolean anim=false;
for(int i=0; i<nbChamp; i++) {
Champignon champignon = gameBoard.getChampignon(i);
if ( champignon.isVisible() ) { // on ne bouge que les champignons visibles
anim=true;
int y = champignon.getY() + dy[i];
if ( y+SPRITE_SIZE > gameBoard.getHeight() ) {
y = gameBoard.getHeight()-SPRITE_SIZE;
dy[i]=-dy[i];
}
else if ( y<0 ) {
y = 0;
dy[i]=-dy[i];
}
champignon.setY(y);
}
}
if ( anim ) {
gameBoard.repaint();
}
});
timer.start();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
timer.stop();
}
});
// fin de la partie animation
frame.add(panel);
frame.setResizable(false);
frame.setSize(nbChamp*SPRITE_SIZE*2, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager