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
| import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public final class Ardoise{
// des couleurs
public static final Color NOIR = Color.BLACK;
public static final Color BLANC = Color.WHITE;
// taille de la fenetre par defaut
private static final int TAILLE = 600;
private static int longueur = TAILLE;
private static int hauteur = TAILLE;
private static final double BORDURE = 0.07; // 5% de la bordure
private static double xmin, ymin, xmax, ymax;
// diametre de la mine
private static final double diametreMine = 0.005;
private static BufferedImage offscreenImage, onscreenImage;
private static Graphics2D offscreen, onscreen;
private static JFrame frame;
static{
init();
}
private static void init() {
if (frame != null) frame.setVisible(false);
frame = new JFrame();
offscreenImage = new BufferedImage(longueur, hauteur, BufferedImage.TYPE_INT_ARGB);
onscreenImage = new BufferedImage(longueur, hauteur, BufferedImage.TYPE_INT_ARGB);
offscreen = offscreenImage.createGraphics();
onscreen = onscreenImage.createGraphics();
setXscale(0.0,1.0);
setYscale(0.0,1.0);
offscreen.fillRect(0,0,longueur,hauteur);
offscreen.setColor(NOIR);
offscreen.setStroke(new BasicStroke((float) diametreMine));
ImageIcon icon = new ImageIcon(onscreenImage);
JLabel draw = new JLabel(icon);
frame.setContentPane(draw);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Ardoise");
frame.pack();
frame.requestFocusInWindow();
frame.setVisible(true);
}
public static void setXscale(double min, double max) {
double size = max - min;
xmin = min - BORDURE * size;
xmax = max + BORDURE * size;
}
public static void setYscale(double min, double max) {
double size = max - min;
ymin = min - BORDURE * size;
ymax = max + BORDURE * size;
}
private static double scaleX(double x){
return longueur * (x - xmin) / (xmax - xmin);
}
private static double scaleY(double y){
return hauteur * (ymax - y) / (ymax - ymin);
}
public static void line(double x0, double y0, double x1, double y1) {
offscreen.draw(new Line2D.Double(scaleX(x0), scaleY(y0),scaleX(x1), scaleY(y1)));
show();
}
public static void show() {
onscreen.drawImage(offscreenImage, 0, 0, null);
frame.repaint();
}
} |
Partager