Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add draw/fillTriangle.
[graphlib_java.git] / DrawingWindow.java
index 9970753..267aabf 100644 (file)
@@ -1,6 +1,3 @@
-/**
- */
-
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
@@ -24,21 +21,17 @@ import java.lang.reflect.*;
  *
  * <p>Télécharger le code: <a href="DrawingWindow.java">DrawingWindow.java</a>
  *
- * <p>Télécharger les exemples d'utilisation:
+ * <p>Télécharger des exemples d'utilisation:
  * <a href="Hello.java">Hello.java</a>
  * <a href="Exemple1.java">Exemple1.java</a>
  * <a href="Exemple2.java">Exemple2.java</a>
  * <a href="Exemple3.java">Exemple3.java</a>
  *
- * @author Arnaud Giersch <arnaud.giersch@univ-fcomte.fr>
- * @version 20141008
- */
-public class DrawingWindow
-    extends JPanel
-    implements KeyListener {
+ * @author Arnaud Giersch &lt;arnaud.giersch@univ-fcomte.fr&gt;
+ * @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;
@@ -57,42 +50,28 @@ public class DrawingWindow
      * @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();
     }
 
@@ -211,7 +190,7 @@ public class DrawingWindow
             graphics.fillRect(0, 0, width, height);
             graphics.setColor(c);
         }
-        repaint();
+        panel.repaint();
     }
 
     /** Dessine un point.
@@ -225,7 +204,7 @@ public class DrawingWindow
         synchronized (image) {
             image.setRGB(x, y, graphics.getColor().getRGB());
         }
-        repaint(x, y, 1, 1);
+        panel.repaint(x, y, 1, 1);
     }
 
     /**
@@ -240,8 +219,8 @@ public class DrawingWindow
         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.
@@ -261,7 +240,7 @@ public class DrawingWindow
         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.
@@ -276,12 +255,12 @@ public class DrawingWindow
     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);
     }
 
     /**
@@ -297,7 +276,7 @@ public class DrawingWindow
         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);
     }
 
     /**
@@ -311,9 +290,54 @@ public class DrawingWindow
      */
     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);
+    }
+
+    /**
+     * Dessine un triangle.
+     *
+     * Dessine un triangle défini par les coordonnées de ses sommets:
+     * (x1, y1), (x2, y2) et (x3, y3).  Utilise la couleur de dessin
+     * courante.
+     *
+     * @see #fillTriangle
+     * @see #setColor
+     */
+
+    public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
+        Polygon poly = new Polygon();
+        poly.addPoint(x1, y1);
+        poly.addPoint(x2, y2);
+        poly.addPoint(x3, y3);
+        synchronized (image) {
+            graphics.drawPolygon(poly);
+        }
+        panel.repaint(poly.getBounds());
+    }
+
+    /**
+     * Dessine un triangle plein.
+     *
+     * Dessine un triangle plein défini par les coordonnées de ses
+     * sommets: (x1, y1), (x2, y2) et (x3, y3).  Utilise la couleur de
+     * dessin courante.
+     *
+     * @see #drawTriangle
+     * @see #setColor
+     */
+    public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3) {
+        Polygon poly = new Polygon();
+        poly.addPoint(x1, y1);
+        poly.addPoint(x2, y2);
+        poly.addPoint(x3, y3);
+        synchronized (image) {
+            graphics.drawPolygon(poly);
+            graphics.fillPolygon(poly);
+        }
+        panel.repaint(poly.getBounds());
     }
 
     /**
@@ -325,7 +349,7 @@ public class DrawingWindow
         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
     }
 
     /**
@@ -414,28 +438,83 @@ public class DrawingWindow
         }
     }
 
-    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 {
+
+        private static final long serialVersionUID = 0;
+
+        final DrawingWindow w;
+
+        DWPanel(DrawingWindow w) {
+            this.w = w;
+            Dimension dimension = new Dimension(w.width, w.height);
+            super.setMinimumSize(dimension);
+            super.setMaximumSize(dimension);
+            super.setPreferredSize(dimension);
+        }
+
+        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) { }
+
     }
 
-    public void keyReleased(KeyEvent e) { }
-    public void keyTyped(KeyEvent e) { }
+    // Sample tests
+    public static void main(String[] args) {
+        DrawingWindow w = new DrawingWindow("Test!", 400, 400);
 
-    /* PRIVATE STUFF FOLLOW */
+        w.setColor("green");
+        for (int i = 0; i < 12; i++) {
+            int p = 10 * i + 10;
+            w.drawLine(p, 0, p, 175);
+            w.drawLine(p + i, 0, p + i, 175);
+        }
 
-    private static final long serialVersionUID = 0;
+        w.setColor("black");
+        for (int i = 0; i < 12; i++) {
+            int p = 10 * i + 10;
 
-    private JFrame frame;
-    private BufferedImage image;
-    private Graphics2D graphics;
-    private Color bgColor;
+            w.drawCircle(p, 25, i);
+            w.fillCircle(p, 50, i);
 
+            w.drawRect(p, 75, p + i, 75 + i);
+            w.fillRect(p, 100, p + i, 100 + i);
+
+            w.drawTriangle(p, 125, p + i, 125 + i/2, p, 125 + i);
+            w.fillTriangle(p, 150, p + i, 150 + i/2, p, 150 + i);
+        }
+    }
 }