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
|
package hex;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;
/**
*
* @author Gabriel
*/
public class Hexagones extends JComponent {
private int xHex[] = {0, 16, 32, 32, 16, 0};
private int yHex[] = {10, 0, 10, 30, 40, 30};
private int xHex2[] = new int[6];
private int xHexInt2[] = new int[6];
private int xHexInt[] = {1, 16, 31, 31, 16, 1};
private int yHexInt[] = {10, 1, 10, 30, 39, 30};
private int yHex2[] = new int[6];
private int yHexInt2[] = new int[6];
private Hexagone hexagone = new Hexagone();
private Color[][] couleur = new Color[11][11];
private boolean firstTime = true;
public Hexagones() {
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
couleur[i][j] = Color.WHITE;
}
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(hints);
for (int i = 0; i < 11; i++) {
for (int k = 0; k < xHex.length; k++) {
yHex2[k] = yHex[k] + i * 30;
yHexInt2[k] = yHexInt[k] + i * 30;
}
for (int j = 0; j < 11; j++) {
for (int k = 0; k < xHex.length; k++) {
xHex2[k] = xHex[k] + j * 32 + i * 16;
xHexInt2[k] = xHexInt[k] + j * 32 + i * 16;
}
g2d.setColor(Color.black);
g2d.drawPolygon(xHex2, yHex2, 6);
g2d.setColor(couleur[i][j]);
g2d.fillPolygon(xHexInt2, yHexInt2, 6);
}
}
}
public void changerHexagone(int i, int j, Color couleur) {
this.couleur[i][j] = couleur;
repaint();
}
} |