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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;
public class DemoDessin {
private static final int NBLINES = 5;
private static final int HEADER_HEIGHT = 100;
private static final int NBCOLUMNS = NBLINES;
private static final int STONE_WIDTH = 60;
private static final int WIDTH = STONE_WIDTH+4;
private static final int HEIGHT = WIDTH;
private static final Font LEGEND_FONT = new Font("Arial", Font.BOLD, 36);
private static final Font NUMBER_FONT_1 = new Font("Arial", Font.BOLD, 24);
private static final Font NUMBER_FONT_2 = new Font("Arial", Font.BOLD, 36);
public static void main(String[] args) {
Stone[][] plateau = new Stone[NBLINES][NBCOLUMNS];
Random random = new Random();
for(int i=0; i<plateau.length; i++) {
for(int j=0; j<plateau[i].length; j++) {
switch(random.nextInt(10)) {
case 1:
plateau[i][j]=new Stone(true, random.nextInt(999)+1);
break;
case 2:
plateau[i][j]=new Stone(false, random.nextInt(999)+1);
break;
}
}
}
int width = (NBCOLUMNS+2)*WIDTH;
int height = (NBLINES+2)*HEIGHT + HEADER_HEIGHT;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics.setBackground(Color.WHITE);
graphics.clearRect(0, 0, width, height);
// draw header...
// end header
graphics.translate(0, HEADER_HEIGHT); // on décale tout le reste de la hauteur du header
graphics.setColor(Color.BLACK);
graphics.setFont(LEGEND_FONT);
// ligne des lettres en haut
for(int i=0; i<NBCOLUMNS; i++) {
String letter = String.valueOf( (char)('A'+i) );
Rectangle2D bounds = new Rectangle2D.Double(WIDTH + i*WIDTH, 0, WIDTH, HEIGHT);
Point2D.Float location = getStringLocation(graphics, bounds, letter, SwingConstants.CENTER, SwingConstants.CENTER);
graphics.drawString(letter, location.x, location.y);
}
// ligne des lettres en bas
for(int i=0; i<NBCOLUMNS; i++) {
String letter = String.valueOf( (char)('A'+i) );
Rectangle2D bounds = new Rectangle2D.Double(WIDTH + i*WIDTH, HEIGHT+NBLINES*HEIGHT, WIDTH, HEIGHT);
Point2D.Float location = getStringLocation(graphics, bounds, letter, SwingConstants.CENTER, SwingConstants.CENTER);
graphics.drawString(letter, location.x, location.y);
}
// ligne des chiffres à gauche
for(int i=0; i<NBLINES; i++) {
String letter = String.valueOf( NBLINES-i );
Rectangle2D bounds = new Rectangle2D.Double(0, HEIGHT + i*HEIGHT, WIDTH, HEIGHT);
Point2D.Float location = getStringLocation(graphics, bounds, letter, SwingConstants.CENTER, SwingConstants.CENTER);
graphics.drawString(letter, location.x, location.y);
}
// ligne des chiffres à droite
for(int i=0; i<NBLINES; i++) {
String letter = String.valueOf( NBLINES-i );
Rectangle2D bounds = new Rectangle2D.Double(WIDTH+NBCOLUMNS*WIDTH, HEIGHT + i*HEIGHT, WIDTH, HEIGHT);
Point2D.Float location = getStringLocation(graphics, bounds, letter, SwingConstants.CENTER, SwingConstants.CENTER);
graphics.drawString(letter, location.x, location.y);
}
// lignes de grille horizontales
for(int i=0; i<NBLINES; i++) {
int x1=WIDTH+WIDTH/2;
int x2=WIDTH/2+WIDTH*NBCOLUMNS;
int y=HEIGHT+HEIGHT/2+i*HEIGHT;
graphics.drawLine(x1, y, x2, y);
if ( i==0 ) {
graphics.drawLine(x1-1, y-1, x2+1, y-1);
}
else if ( i==NBLINES-1 ) {
graphics.drawLine(x1-1, y+1, x2+1, y+1);
}
}
// lignes de grille verticales
for(int i=0; i<NBCOLUMNS; i++) {
int y1=HEIGHT+HEIGHT/2;
int y2=HEIGHT/2+HEIGHT*NBLINES;
int x=WIDTH+WIDTH/2+i*WIDTH;
graphics.drawLine(x, y1, x, y2);
if ( i==0 ) {
graphics.drawLine(x-1, y1-1, x-1, y2+1);
}
else if ( i==NBCOLUMNS-1 ) {
graphics.drawLine(x+1, y1-1, x+1, y2+1);
}
}
for(int i=0; i<plateau.length; i++) {
for(int j=0; j<plateau[i].length; j++) {
Stone stone = plateau[i][j];
if ( stone!=null ) {
int x = WIDTH + WIDTH*j + (WIDTH - STONE_WIDTH)/2;
int y = HEIGHT + HEIGHT*i + (HEIGHT - STONE_WIDTH)/2;
if ( stone.getNumber()>99 ) {
graphics.setFont(NUMBER_FONT_1);
}
else {
graphics.setFont(NUMBER_FONT_2);
}
boolean color = stone.getColor();
graphics.setColor(color?Color.BLACK:Color.WHITE);
graphics.fillOval(x, y, STONE_WIDTH, STONE_WIDTH);
graphics.setColor(Color.BLACK);
graphics.drawOval(x, y, STONE_WIDTH, STONE_WIDTH);
graphics.setColor(color?Color.WHITE:Color.BLACK);
Rectangle2D bounds = new Rectangle2D.Double(x, y, STONE_WIDTH, STONE_WIDTH);
Point2D.Float location = getStringLocation(graphics, bounds, stone.getLabel(), SwingConstants.CENTER, SwingConstants.CENTER);
graphics.drawString(stone.getLabel(), location.x, location.y);
}
}
}
graphics.dispose();
showImage(image);
}
private static void showImage(BufferedImage image) {
JFrame frame = new JFrame("Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel(new ImageIcon(image)));
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Save");
button.addActionListener(e-> saveImage(button, image));
buttonPanel.add(button);
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void saveImage(Component parent, BufferedImage image) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Sauvegarde image");
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("Fichiers image", ImageIO.getWriterFileSuffixes()));
int result = fileChooser.showSaveDialog(parent);
File file = fileChooser.getSelectedFile();
if ( result==JFileChooser.APPROVE_OPTION && file!=null ) {
final boolean saveIt;
String extension = getExtension(file.getName());
if ( file.isDirectory() ) {
JOptionPane.showMessageDialog(parent, "Vous avez choisi un dossier existant. Sauvegarde annulée");
saveIt=false;
}
else {
if ( extension==null ) {
JOptionPane.showMessageDialog(parent, "Type d'image indéterminable. Sauvegarde annulée");
saveIt=false;
}
else if ( file.isFile() ) {
if ( JOptionPane.showConfirmDialog(parent, "Le fichier existe déjà. Voulez-vous le remplacer ?")==JOptionPane.OK_OPTION ) {
saveIt=true;
}
else {
saveIt=false;
}
}
else {
saveIt=true;
}
}
if ( saveIt ) {
try {
ImageIO.write(image, extension, file);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(parent, "Erreur ("+e.getLocalizedMessage()+"). Sauvegarde annulée");
}
}
}
}
private static String getExtension(String name) {
final int pos = name.lastIndexOf(".");
if (pos<0 ) return null;
return name.substring(pos+1);
}
public static class Stone {
private final boolean color;
private final int nb;
public Stone(boolean color, int nb) {
this.color=color;
this.nb=nb;
}
public int getNumber() {
return nb;
}
public boolean getColor() {
return color;
}
public String getLabel() {
return String.valueOf(nb);
}
}
public static Point2D.Float getStringLocation(Graphics2D g2d, double x, double y, double width, double height, String string, int halign, int valign) {
return getStringLocation(g2d, new Rectangle2D.Double(x, y, width, height), string, halign, valign);
}
public static Point2D.Float getStringLocation(Graphics2D g2d, Rectangle2D bounds, String string, int halign, int valign) {
final FontMetrics fontMetrics = g2d.getFontMetrics();
final Rectangle2D textBounds = fontMetrics.getStringBounds(string, g2d);
final double textWidth = textBounds.getWidth();
double x,y;
switch (halign) {
case SwingConstants.LEFT:
x = bounds.getX();
break;
case SwingConstants.RIGHT:
x = bounds.getX() + bounds.getWidth() - textWidth;
break;
case SwingConstants.CENTER:
default:
x = bounds.getX()
+ (bounds.getWidth() - textWidth) / 2;
break;
}
switch (valign) {
case SwingConstants.TOP:
y = bounds.getY() + fontMetrics.getAscent() - fontMetrics.getDescent();
break;
case SwingConstants.BOTTOM:
y = bounds.getY()
+ bounds.getHeight() - fontMetrics.getDescent() ;
break;
case SwingConstants.CENTER:
default:
y = bounds.getY()
+ bounds.getHeight()/ 2 - ( ( fontMetrics.getAscent() + fontMetrics.getDescent() ) / 2 ) + fontMetrics.getAscent() ;
break;
}
return new Point2D.Float((float)x, (float)y);
}
} |
Partager