Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix window closing (again).
authorArnaud Giersch <arnaud.giersch@free.fr>
Tue, 14 Oct 2014 19:33:11 +0000 (21:33 +0200)
committerArnaud Giersch <arnaud.giersch@free.fr>
Tue, 14 Oct 2014 20:18:38 +0000 (22:18 +0200)
DrawingWindow.java
Test.java

index 18a7f5a..b73cf7e 100644 (file)
@@ -639,10 +639,10 @@ public class DrawingWindow {
     private BufferedImage image; // the image we draw into
     private Graphics2D graphics; // graphics associated with image
     private Color bgColor;       // background color, for clearGraph()
+    private boolean isClosed;    // is the window closed ?
 
     // To be run on the Event Dispatching Thread
     void createGUI() {
-        DrawingWindow.instances++;
         panel = new DWPanel();
         frame = new JFrame(title);
         frame.add(panel);
@@ -656,10 +656,23 @@ public class DrawingWindow {
     }
 
     private class DWWindowHandler extends WindowAdapter {
+        public void windowOpened(WindowEvent ev) {
+            DrawingWindow w = DrawingWindow.this;
+            DrawingWindow.instances++;
+            w.isClosed = false;
+        }
+
         public void windowClosed(WindowEvent ev) {
-            // System.err.println("CLOSED: " + DrawingWindow.instances);
-            if (--DrawingWindow.instances == 0)
-                System.exit(0);
+            DrawingWindow w = DrawingWindow.this;
+            if (!w.isClosed) {
+                w.isClosed = true;
+                if (DrawingWindow.instances <= 0)
+                    throw new AssertionError("Bad instance counter: " +
+                                             DrawingWindow.instances);
+                DrawingWindow.instances--;
+                if (DrawingWindow.instances == 0)
+                    System.exit(0);
+            }
         }
     }
 
index 74038e2..bcc7f92 100644 (file)
--- a/Test.java
+++ b/Test.java
@@ -1,52 +1,48 @@
 class Test{
     public static void main(String[] args) {
-        DrawingWindow w;
-
-        w = new DrawingWindow("Test!", 400, 400);
-
-        w.setColor("lawngreen");
+        DrawingWindow w1 = new DrawingWindow("Test!", 400, 400);
+        w1.setColor("lawngreen");
         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);
+            w1.drawLine(p, 0, p, 175);
+            w1.drawLine(p + i, 0, p + i, 175);
         }
 
-        w.setColor("black");
+        w1.setColor("black");
         for (int i = 0; i < 12; i++) {
             int p = 10 * i + 10;
 
-            w.drawCircle(p, 25, i);
-            w.fillCircle(p, 50, i);
+            w1.drawCircle(p, 25, i);
+            w1.fillCircle(p, 50, i);
 
-            w.drawRect(p, 75, p + i, 75 + i);
-            w.fillRect(p, 100, p + i, 100 + i);
+            w1.drawRect(p, 75, p + i, 75 + i);
+            w1.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);
+            w1.drawTriangle(p, 125, p + i, 125 + i/2, p, 125 + i);
+            w1.fillTriangle(p, 150, p + i, 150 + i/2, p, 150 + i);
         }
 
-        w = new DrawingWindow("Test!", 800, 600);
-
-        w.setBgColor("red");
-        w.setColor("blue");
-        for (int i = 0; i < 10; i++) {
-            w.clearGraph();
-            for (int y = 0; y < w.height; y++) {
-                for (int x = 0; x < w.width; x++) {
-                    w.drawPoint(x, y);
+        DrawingWindow w2 = new DrawingWindow("Test!", 800, 600);
+        w2.setBgColor("red");
+        w2.setColor("blue");
+        for (int i = 0; i < 3; i++) {
+            w2.clearGraph();
+            for (int y = 0; y < w2.height; y++) {
+                for (int x = 0; x < w2.width; x++) {
+                    w2.drawPoint(x, y);
                 }
             }
         }
-        w.setColor("white");
-        for (int i = 0; i < 10; i++) {
-            w.clearGraph();
-            for (int y = 0; y < w.height; y++) {
-                for (int x = 0; x < w.width; x++) {
-                    w.drawPoint(x, y);
+        w2.setColor("white");
+        for (int i = 0; i < 3; i++) {
+            w2.clearGraph();
+            for (int y = 0; y < w2.height; y++) {
+                for (int x = 0; x < w2.width; x++) {
+                    w2.drawPoint(x, y);
                 }
-                w.sync();
+                w2.sync();
             }
         }
-        w.closeGraph();
+        w2.closeGraph();
     }
 }