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 169
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Board extends JPanel {
private static final int SIZE = 32; // largeur et hauteur d'un carré
private static final int RAY = 3; // rayon des coins arrondis
private static final int GAP = 4; // espace entre les carré
private static final int WIDTH = 10; // dimension du losange (demi-diagonale)
private static final int NB_COLUMNS = 8; // nombre de colonnes
private static final int NB_LINES = 8; // nombre de lignes
private static final Dimension PREFERRED_SIZE = new Dimension( (SIZE+GAP)*NB_COLUMNS, (SIZE+GAP)*NB_LINES );
private static final Shape CLIP_SQUARE = new RoundRectangle2D.Double(0, 0, SIZE, SIZE, RAY, RAY);
private static final Shape CLIP_DIAMOND = createDiamondClip();
// je crée les images dynamiquement (mais on peut partir d'images chargées depuis le disque
private static final Color[] COLORS = {Color.RED, Color.YELLOW, Color.GREEN};
private static final Map<Color, Image> IMAGES = Arrays.asList(COLORS)
.stream()
.collect(Collectors.toMap(c->c, c-> createSquare(c)));
private static final Image OVERLAY = createOverlay();
private final Square[][] tab;
public Board() {
tab = new Square[NB_LINES][NB_COLUMNS];
// remplissage aléatoire pour la démo
Random random = new Random();
for(int i=0; i<NB_LINES; i++) {
for(int j=0; j<NB_COLUMNS; j++) {
Color color = COLORS[random.nextInt(3)];
boolean overlay = random.nextBoolean();
tab[i][j]=new Square(color, overlay);
}
}
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int line= e.getY()/(SIZE+GAP);
int col= e.getX()/(SIZE+GAP);
clicCase(col, line, tab[line][col]);
}
});
setPreferredSize(PREFERRED_SIZE);
setBackground(Color.BLACK);
}
protected void clicCase(int col, int line, Square square) {
// ici on peut réagir au clic
System.out.println("Clic sur "+col+" "+line +"; "+square.getColor());
}
@Override
protected void paintComponent(Graphics g) {
// dessin de toutes les pièces
super.paintComponent(g);
g.setClip(CLIP_DIAMOND);
for(int i=0; i<NB_LINES; i++) {
for(int j=0; j<NB_COLUMNS; j++) {
int x=i*(SIZE+GAP)+GAP/2;
int y=j*(SIZE+GAP)+GAP/2;
tab[i][j].draw(g, x,y);
}
}
}
private static Image createSquare(Color color) {
BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setPaint(new RadialGradientPaint(
new Rectangle2D.Double(0,0,SIZE,SIZE),
new float[]{0.2f,1f},
new Color[]{Color.WHITE, color},
CycleMethod.NO_CYCLE
));
g.fill(CLIP_SQUARE);
g.dispose();
return image;
}
private static Image createOverlay() {
BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.setPaint(new Color(255,255,255,192));
g.fill(CLIP_SQUARE);
g.dispose();
return image;
}
private static Shape createDiamondClip() {
final Rectangle2D bounds = new Rectangle2D.Double(0,0,PREFERRED_SIZE.width,PREFERRED_SIZE.height);
final Area area = new Area(bounds);
final Path2D path = new Path2D.Double();
path.moveTo(bounds.getCenterX()-WIDTH, bounds.getCenterY());
path.lineTo(bounds.getCenterX(), bounds.getCenterY()-WIDTH);
path.lineTo(bounds.getCenterX()+WIDTH, bounds.getCenterY());
path.lineTo(bounds.getCenterX(), bounds.getCenterY()+WIDTH);
path.closePath();
area.subtract(new Area(path));
return area;
}
public static class Square {
private Color color;
private boolean overlay;
public Square(Color color, boolean overlay) {
this.color=color;
this.overlay=overlay;
}
public Color getColor() {
return color;
}
public void draw(Graphics g, int x, int y) {
Image image = IMAGES.get(color);
g.drawImage(image, x, y, null);
if ( overlay ) {
g.drawImage(OVERLAY, x, y, null);
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Board());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager