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);
}
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);
+ }
}
}
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();
}
}