* <a href="Exemple3.java">Exemple3.java</a>
*
* @author Arnaud Giersch <arnaud.giersch@univ-fcomte.fr>
- * @version 20141008
- */
-public class DrawingWindow
- extends JPanel
- implements KeyListener {
+ * @version Wed, 08 Oct 2014 21:29:23 +0200
- /** Titre de la fenĂȘtre */
- public final String title;
+ */
+public class DrawingWindow {
/** Largeur de la fenĂȘtre */
public final int width;
* @see javax.swing.JPanel
*/
public DrawingWindow(String title, int width, int height) {
- this.title = new String(title);
+
+ this.title = new String(title);
this.width = width;
this.height = height;
- Dimension dimension = new Dimension(width, height);
- super.setMinimumSize(dimension);
- super.setMaximumSize(dimension);
- super.setPreferredSize(dimension);
-
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
graphics = image.createGraphics();
- setColor(Color.BLACK);
- setBgColor(Color.WHITE);
- clearGraph();
-
try {
javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
- public void run() {
- frame = new JFrame(DrawingWindow.this.title);
-
- frame.add(DrawingWindow.this);
- frame.pack();
- frame.setResizable(false);
-
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.addKeyListener(DrawingWindow.this);
-
- frame.setLocationByPlatform(true);
- frame.setVisible(true);
- }
+ public void run() { createGUI(); }
});
}
catch (Exception e) {
- System.err.println("Warning: caught spurious exception: " + e);
+ System.err.println("Error: interrupted while creating GUI");
+ System.err.println("Got exception: " + e);
+ System.exit(1);
}
+
+ setColor(Color.BLACK);
+ setBgColor(Color.WHITE);
+ clearGraph();
sync();
}
graphics.fillRect(0, 0, width, height);
graphics.setColor(c);
}
- repaint();
+ panel.repaint();
}
/** Dessine un point.
synchronized (image) {
image.setRGB(x, y, graphics.getColor().getRGB());
}
- repaint(x, y, 1, 1);
+ panel.repaint(x, y, 1, 1);
}
/**
synchronized (image) {
graphics.drawLine(x1, y1, x2, y2);
}
- repaint(Math.min(x1, x2), Math.min(y1, y2),
- Math.abs(x1 - x2) + 1, Math.abs(y1 - y2) + 1);
+ panel.repaint(Math.min(x1, x2), Math.min(y1, y2),
+ Math.abs(x1 - x2) + 1, Math.abs(y1 - y2) + 1);
}
/** Dessine un rectangle.
synchronized (image) {
graphics.drawRect(x, y, w, h);
}
- repaint(x, y, w, h);
+ panel.repaint(x, y, w + 1, h + 1);
}
/** Dessine un rectangle plein.
public void fillRect(int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
- int w = Math.abs(x1 - x2);
- int h = Math.abs(y1 - y2);
+ int w = Math.abs(x1 - x2) + 1;
+ int h = Math.abs(y1 - y2) + 1;
synchronized (image) {
graphics.fillRect(x, y, w, h);
}
- repaint(x, y, w, h);
+ panel.repaint(x, y, w, h);
}
/**
synchronized (image) {
graphics.drawOval(x - r, y - r, 2 * r, 2 * r);
}
- repaint(x - r, y - r, 2 * r, 2 * r);
+ panel.repaint(x - r, y - r, 2 * r + 1, 2 * r + 1);
}
/**
*/
public void fillCircle(int x, int y, int r) {
synchronized (image) {
+ graphics.drawOval(x - r, y - r, 2 * r, 2 * r);
graphics.fillOval(x - r, y - r, 2 * r, 2 * r);
}
- repaint(x - r, y - r, 2 * r, 2 * r);
+ panel.repaint(x - r, y - r, 2 * r + 1, 2 * r + 1);
}
/**
synchronized (image) {
graphics.drawString(text, x, y);
}
- repaint(); // don't know how to calculate tighter bounding box
+ panel.repaint(); // don't know how to calculate tighter bounding box
}
/**
}
}
- public void paint(Graphics g) {
- synchronized (image) {
- g.drawImage(image, 0, 0, null);
- }
+ /* PRIVATE STUFF FOLLOWS */
+
+ private final String title; // window's title
+ private JFrame frame; // the frame (window)
+ private DWPanel panel; // the panel showing the image
+ private BufferedImage image; // the image we draw into
+ private Graphics2D graphics; // graphics associated with image
+ private Color bgColor; // background color, for clearGraph()
+
+ // To be run on the Event Dispatching Thread
+ void createGUI() {
+ panel = new DWPanel(this);
+
+ frame = new JFrame(title);
+ frame.add(panel);
+ frame.pack();
+ frame.setResizable(false);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.addKeyListener(panel);
+ frame.setLocationByPlatform(true);
+ frame.setVisible(true);
}
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
- closeGraph();
- }
- }
+ private class DWPanel extends JPanel implements KeyListener {
- public void keyReleased(KeyEvent e) { }
- public void keyTyped(KeyEvent e) { }
+ private static final long serialVersionUID = 0;
- /* PRIVATE STUFF FOLLOW */
+ final DrawingWindow w;
- private static final long serialVersionUID = 0;
+ DWPanel(DrawingWindow w) {
+ this.w = w;
+ Dimension dimension = new Dimension(w.width, w.height);
+ super.setMinimumSize(dimension);
+ super.setMaximumSize(dimension);
+ super.setPreferredSize(dimension);
+ }
- private JFrame frame;
- private BufferedImage image;
- private Graphics2D graphics;
- private Color bgColor;
+ public void paint(Graphics g) {
+ synchronized (w.image) {
+ g.drawImage(w.image, 0, 0, null);
+ }
+ }
+ public void keyPressed(KeyEvent e) {
+ if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
+ w.closeGraph();
+ }
+ }
+
+ public void keyReleased(KeyEvent e) { }
+ public void keyTyped(KeyEvent e) { }
+
+ }
}