summaryrefslogtreecommitdiff
path: root/examples/gnu/classpath/examples/java2d/J2dBenchmark.java
blob: a0b51f0e23b4cf26b2ee64a80283205950faf9ed (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
/* J2dBenchmark.java -- Benchmarking utility for java2d,
   based on the Aicas AWT benchmarker
 Copyright (C) 2006 Free Software Foundation, Inc.

 This file is part of GNU Classpath examples.

 GNU Classpath is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 GNU Classpath is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNU Classpath; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301 USA. */

package gnu.classpath.examples.java2d;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class J2dBenchmark
    extends Panel
{
  /**
   * Default number of test-iterations.
   */
  protected static final int DEFAULT_TEST_SIZE = 1000;

  /**
   * Default screen size.
   */
  protected static final int DEFAULT_SCREEN_WIDTH = 320;

  protected static final int DEFAULT_SCREEN_HEIGHT = 240;

  /**
   * Java2D tests.
   */
  protected static final int J2DTEST_ARC = 1 << 0;

  protected static final int J2DTEST_CUBICCURVE = 1 << 1;

  protected static final int J2DTEST_ELLIPSE = 1 << 2;

  protected static final int J2DTEST_GENERALPATH = 1 << 3;

  protected static final int J2DTEST_LINE = 1 << 4;

  protected static final int J2DTEST_QUADCURVE = 1 << 5;

  protected static final int J2DTEST_RECTANGLE = 1 << 6;

  protected static final int J2DTEST_ROUNDRECTANGLE = 1 << 7;

  protected static final int J2DTEST_IMAGE = 1 << 8;

  protected static final int J2DTEST_NONE = 0;

  /*
  private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE
                                         | J2DTEST_ELLIPSE
                                         | J2DTEST_GENERALPATH | J2DTEST_LINE
                                         | J2DTEST_QUADCURVE
                                         | J2DTEST_RECTANGLE
                                         | J2DTEST_ROUNDRECTANGLE
                                         | J2DTEST_IMAGE;
  */
  private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE
                                         | J2DTEST_ELLIPSE
                                         | J2DTEST_LINE
                                         | J2DTEST_QUADCURVE
                                         | J2DTEST_RECTANGLE
                                         | J2DTEST_ROUNDRECTANGLE
                                         | J2DTEST_IMAGE;

  int iterations = 1;

  protected int screenWidth = DEFAULT_SCREEN_WIDTH;

  protected int screenHeight = DEFAULT_SCREEN_HEIGHT;

  protected boolean noClippingFlag = true;

  protected boolean withClippingFlag = true;

  protected boolean zeroClippingFlag = true;

  protected boolean singleBufferFlag = true;

  protected boolean doubleBufferFlag = true;

  protected boolean gradientFlag = false;

  protected String texture = null;

  protected boolean strokeFlag = false;

  protected float composite = 1;

  protected int xtranslate = 0;

  protected int ytranslate = 0;

  protected double xshear = 0;

  protected double yshear = 0;

  protected double rotate = 0;

  protected boolean antialiasFlag = false;

  protected AffineTransform affineTransform = null;

  protected int awtTests = J2DTEST_ALL;

  protected int testSize = DEFAULT_TEST_SIZE;

  private Label testLabel;

  private String testContext = "";

  Logger logger = Logger.getLogger("J2dGraphicsBenchmark");

  private Image pngTestImage;

  private Image gifTestImage;

  protected BufferedImage textureImage;

  protected TestSet testSetMap = new TestSet();

  public String init()
  {
    boolean loadError = false;
    pngTestImage = loadImage("../icons/aicas.png");
    gifTestImage = loadImage("../icons/palme.gif");

    if (texture != null)
      {
        textureImage = loadBufferedImage(texture);

        if (textureImage == null)
          {
            logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "init",
                        "Unable to load texture - defaulting "
                            + "to solid colours");
            texture = null;
            loadError = true;
          }
      }

    setLayout(new BorderLayout());
    testLabel = new Label();
    add(testLabel, BorderLayout.NORTH);
    add(new GraphicsTest(), BorderLayout.CENTER);

    if (loadError)
      return "Unable to load image";
    else
      return null;
  }

  void setTestContext(String testName)
  {
    logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest",
                "--- Starting new test context: " + testName);
    testContext = testName;
    testLabel.setText(testName);
  }

  private void recordTest(String testName, long time)
  {
    logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest",
                testContext + ": " + testName + " duration (ms): " + time);
    TestRecorder recorder = testSetMap.getTest(testName);
    if (recorder == null)
      {
        recorder = new TestRecorder(testName);
        testSetMap.putTest(testName, recorder);
      }
    recorder.addRun(time);
  }

  void printReport()
  {
    for (Iterator i = testSetMap.testIterator(); i.hasNext();)
      {
        TestRecorder recorder = testSetMap.getTest((String) i.next());
        System.out.println("TEST " + recorder.getTestName() + ": average "
                           + recorder.getAverage() + "ms ["
                           + recorder.getMinTime() + "-"
                           + recorder.getMaxTime() + "]");
      }
  }

  void testComplete()
  {
    System.exit(0);
  }

  public static void main(String[] args)
  {
    int awtTests;
    int i;
    boolean endOfOptionsFlag;
    J2dBenchmark speed = new J2dBenchmark();

    // Parse arguments.
    i = 0;
    endOfOptionsFlag = false;
    awtTests = J2DTEST_NONE;
    while (i < args.length)
      {
        if (! endOfOptionsFlag)
          {
            if (args[i].equals("--help") || args[i].equals("-help")
                || args[i].equals("-h"))
              {
                System.out.println("Usage: J2dBenchmark [<options>] [<test>  ...]");
                System.out.println("");
                System.out.println("Options: -i|--iterations=<n|-1> - number of iterations (-1 is infinite; default "
                                   + speed.iterations + ")");
                System.out.println("         -w|--width=<n>         - screen width; default "
                                   + DEFAULT_SCREEN_WIDTH);
                System.out.println("         -h|--height=<n>        - screen height; default "
                                   + DEFAULT_SCREEN_HEIGHT);
                System.out.println("         -d|--noDoubleBuffer    - disable double-buffering test");
                System.out.println("         -s|--testsize=<n>      - size of each test; default "
                                   + DEFAULT_TEST_SIZE);
                System.out.println("         -c|--noClipping        - disable clipping test");
                System.out.println("         -z|--noZeroClipping    - disable clipping to zero test");
                System.out.println("");
                System.out.println("Additional options:");
                System.out.println("         --with-gradients       - enable gradients (not compatible with --texture)");
                System.out.println("         --with-stroking        - enable random stroking");
                System.out.println("         --texture=<file>       - enable texturing with this file (not compatible with --with-gradients)");
                System.out.println("         --composite=<n|-1>     - set alpha composite level; -1 for random; default 1.0 (no transparency)");
                System.out.println("         --anti-alias=<on|off>  - set anti-aliasing hint (not all implementations respect this); default off");
                System.out.println("         --x-translate=<n>      - set x-axis translation; default 0");
                System.out.println("         --y-translate=<n>      - set y-axis translation; default 0");
                System.out.println("         --x-shear=<n>          - set x-axis shear; default 0");
                System.out.println("         --y-shear=<n>          - set y-axis shear; default 0");
                System.out.println("         --rotate=<n|-1>        - set rotation (radians); -1 for random; default: 0 (none)");
                System.out.println("");
                System.out.println("Tests: arc");
                System.out.println("       cubiccurve");
                System.out.println("       ellipse");
                // System.out.println(" generalpath");
                System.out.println("       line");
                System.out.println("       quadcurve");
                System.out.println("       rectangle");
                System.out.println("       roundrectangle");
                System.out.println("       image");
                System.exit(1);
              }
            else if ((args[i].startsWith("-i=") || args[i].startsWith("--iterations=")))
              {
                speed.iterations = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if ((args[i].equals("-i") || args[i].equals("--iterations")))
              {
                if ((i + 1) >= args.length)
                  {
                    System.err.println("ERROR: No argument given for option '"
                                       + args[i] + "'!");
                    System.exit(2);
                  }
                speed.iterations = Integer.parseInt(args[i + 1]);
                i += 2;
                continue;
              }
            else if ((args[i].startsWith("-w=") || args[i].startsWith("--width=")))
              {
                speed.screenWidth = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if ((args[i].equals("-w") || args[i].equals("--width")))
              {
                if ((i + 1) >= args.length)
                  {
                    System.err.println("ERROR: No argument given for option '"
                                       + args[i] + "'!");
                    System.exit(2);
                  }
                speed.screenWidth = Integer.parseInt(args[i + 1]);
                i += 2;
                continue;
              }
            else if ((args[i].startsWith("-h=") || args[i].startsWith("--height=")))
              {
                speed.screenHeight = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if ((args[i].equals("-h") || args[i].equals("--height")))
              {
                if ((i + 1) >= args.length)
                  {
                    System.err.println("ERROR: No argument given for option '"
                                       + args[i] + "'!");
                    System.exit(2);
                  }
                speed.screenHeight = Integer.parseInt(args[i + 1]);
                i += 2;
                continue;
              }
            else if ((args[i].equals("-d") || args[i].equals("--noDoubleBuffer")))
              {
                speed.doubleBufferFlag = false;
                i += 1;
                continue;
              }
            else if ((args[i].startsWith("-s=") || args[i].startsWith("--testsize=")))
              {
                if ((i + 1) >= args.length)
                  {
                    System.err.println("ERROR: No argument given for option '"
                                       + args[i] + "'!");
                    System.exit(2);
                  }
                speed.testSize = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if ((args[i].equals("-s") || args[i].equals("--testsize")))
              {
                if ((i + 1) >= args.length)
                  {
                    System.err.println("ERROR: No argument given for option '"
                                       + args[i] + "'!");
                    System.exit(2);
                  }
                speed.testSize = Integer.parseInt(args[i + 1]);
                i += 2;
                continue;
              }
            else if ((args[i].equals("-c") || args[i].equals("--noClipping")))
              {
                speed.noClippingFlag = false;
                i += 1;
                continue;
              }
            else if ((args[i].equals("-z") || args[i].equals("--noZeroClipping")))
              {
                speed.zeroClippingFlag = false;
                i += 1;
                continue;
              }
            else if (args[i].equals("--with-gradients"))
              {
                speed.gradientFlag = true;
                i += 1;
                continue;
              }
            else if (args[i].equals("--with-stroking"))
              {
                speed.strokeFlag = true;
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--texture="))
              {
                speed.texture = args[i].substring(args[i].indexOf('=') + 1);
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--composite="))
              {
                speed.composite = Float.parseFloat(args[i].substring(args[i].indexOf('=') + 1));
                if (speed.composite != - 1
                    && (speed.composite < 0 || speed.composite > 1))
                  {
                    System.err.println("ERROR: Invalid value for composite (must be between 0 and 1, or -1 for random)");
                    System.exit(2);
                  }
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--anti-alias="))
              {
                speed.antialiasFlag = (args[i].substring(args[i].indexOf('=') + 1).equals("on"));
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--x-translate="))
              {
                speed.xtranslate = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--y-translate="))
              {
                speed.ytranslate = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--x-shear="))
              {
                speed.xshear = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--y-shear="))
              {
                speed.yshear = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }
            else if (args[i].startsWith("--rotate="))
              {
                speed.rotate = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
                i += 1;
                continue;
              }

            else if (args[i].equals("--"))
              {
                endOfOptionsFlag = true;
                i += 1;
                continue;
              }
            else if (args[i].startsWith("-"))
              {
                System.err.println("ERROR: Unknown option '" + args[i] + "'!");
                System.exit(2);
              }
          }
        StringTokenizer tokenizer = new StringTokenizer(args[i], " +,");
        while (tokenizer.hasMoreTokens())
          {
            String s = tokenizer.nextToken().toLowerCase();
            if (s.equals("arc"))
              awtTests |= J2DTEST_ARC;
            else if (s.equals("cubiccurve"))
              awtTests |= J2DTEST_CUBICCURVE;
            else if (s.equals("ellipse"))
              awtTests |= J2DTEST_ELLIPSE;
            else if (s.equals("generalpath"))
              awtTests |= J2DTEST_GENERALPATH;
            else if (s.equals("line"))
              awtTests |= J2DTEST_LINE;
            else if (s.equals("quadcurve"))
              awtTests |= J2DTEST_QUADCURVE;
            else if (s.equals("rectangle"))
              awtTests |= J2DTEST_RECTANGLE;
            else if (s.equals("roundrectangle"))
              awtTests |= J2DTEST_ROUNDRECTANGLE;
            else if (s.equals("image"))
              awtTests |= J2DTEST_IMAGE;
            else
              {
                System.err.println("Unknown AWT test '" + s + "'!");
                System.exit(2);
              }
          }
        i += 1;
      }
    if (awtTests != J2DTEST_NONE)
      speed.awtTests = awtTests;

    // Create graphics.
    speed.init();
    final Frame frame = new Frame("J2dGraphicsBenchmark");

    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        frame.setVisible(false);
        System.exit(0);
      }
    });

    frame.add(speed, BorderLayout.CENTER);
    frame.setSize(speed.screenWidth, speed.screenHeight);
    frame.setVisible(true);

    // Insets are correctly set only after the native peer was created.
    Insets insets = frame.getInsets();
    // The internal size of the frame should be 320x240.
    frame.setSize(320 + insets.right + insets.left, 240 + insets.top
                                                    + insets.bottom);
  }

  private Image loadImage(String imageName)
  {
    Image result = null;
    logger.logp(Level.INFO, "J2dGraphicsBenchmark", "loadImage",
                "Loading image: " + imageName);
    URL url = getClass().getResource(imageName);
    if (url != null)
      {
        result = Toolkit.getDefaultToolkit().getImage(url);
        prepareImage(result, this);
      }
    else
      {
        logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "loadImage",
                    "Could not locate image resource in class path: "
                        + imageName);
      }
    return result;
  }

  private BufferedImage loadBufferedImage(String imageName)
  {
    BufferedImage result = null;
    logger.logp(Level.INFO, "J2dGraphicsBenchmark", "loadImage",
                "Loading image: " + imageName);

    // Try to load image out of classpath before trying an absolute filename
    URL url = getClass().getResource(imageName);
    Image img;
    if (url != null)
      img = Toolkit.getDefaultToolkit().getImage(url);
    else
      img = Toolkit.getDefaultToolkit().getImage(imageName);

    if (img != null)
      {
        // Wait for image to load
        try
          {
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img, 1);
            tracker.waitForAll();

            prepareImage(img, this);
            result = new BufferedImage(img.getWidth(this), img.getHeight(this),
                                       BufferedImage.TYPE_INT_RGB);
            result.createGraphics().drawImage(img, 0, 0, this);
          }
        catch (InterruptedException e)
          {
          }
        catch (IllegalArgumentException e)
          {
          }
      }

    if (result == null)
      {
        logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "loadBufferedImage",
                    "Could not locate image resource in class path: "
                        + imageName);
      }
    return result;
  }

  /**
   * Executes the test methods.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  void runTestSet(Graphics2D g, Dimension size)
  {
    // Any user-specified options (ie set transforms, rendering hints)
    prepareGraphics(g);

    if ((awtTests & J2DTEST_ARC) != 0)
      {
        test_drawArc(g, size);
        test_fillArc(g, size);
      }

    if ((awtTests & J2DTEST_CUBICCURVE) != 0)
      {
        test_drawCubicCurve(g, size);
      }

    if ((awtTests & J2DTEST_ELLIPSE) != 0)
      {
        test_drawEllipse(g, size);
        test_fillEllipse(g, size);
      }

    if ((awtTests & J2DTEST_GENERALPATH) != 0)
      {
        // Current implementation doesn't work
        test_drawGeneralPath(g, size);
        test_fillGeneralPath(g, size);
      }

    if ((awtTests & J2DTEST_LINE) != 0)
      {
        test_drawLine(g, size);
      }

    if ((awtTests & J2DTEST_QUADCURVE) != 0)
      {
        test_drawQuadCurve(g, size);
      }

    if ((awtTests & J2DTEST_RECTANGLE) != 0)
      {
        test_drawRectangle(g, size);
        test_fillRectangle(g, size);
      }

    if ((awtTests & J2DTEST_ROUNDRECTANGLE) != 0)
      {
        test_drawRoundRectangle(g, size);
        test_fillRoundRectangle(g, size);
      }

    if ((awtTests & J2DTEST_IMAGE) != 0)
      {
        test_drawImage(g, size);
        test_drawTransparentImage(g, size);
      }
  }

  /**
   * Reset all graphics settings to the standard, default values
   * 
   * @param g the object to apply settings to
   */
  private void resetGraphics(Graphics2D g)
  {
    g.setTransform(new AffineTransform());
    g.setStroke(new BasicStroke());
    g.setComposite(AlphaComposite.SrcOut);
  }

  /**
   * Sets initial user graphics options
   * 
   * @param g the object to apply settings to
   */
  private void prepareGraphics(Graphics2D g)
  {
    // Transforms
    if (affineTransform != null)
      g.setTransform(affineTransform);

    else if (xtranslate != 0 || ytranslate != 0 || xshear != 0 || yshear != 0)
      {
        g.translate(xtranslate, ytranslate);
        g.shear(xshear, yshear);
      }

    if (rotate > 0)
      g.rotate(rotate * Math.PI, screenWidth / 2, screenHeight / 2);

    // Composite (transparency)
    if (composite > 0)
      {
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                  composite));
      }

    // Textures
    if (texture != null)
      g.setPaint(new TexturePaint(textureImage,
                                  new Rectangle(0, 0, textureImage.getWidth(),
                                                textureImage.getHeight())));

    // Anti-alias setting
    if (antialiasFlag)
      g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                                             RenderingHints.VALUE_ANTIALIAS_ON));
    else
      g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                                             RenderingHints.VALUE_ANTIALIAS_OFF));
  }

  /**
   * Gets new random settings
   * 
   * @param g the object to set parameters for
   * @param size the screen size
   */
  private void setRandom(Graphics2D g, Dimension size)
  {
    // Set colour / paint
    if (gradientFlag)
      {
        Color c1 = new Color((int) (Math.random() * 254) + 1,
                             (int) (Math.random() * 254) + 1,
                             (int) (Math.random() * 254) + 1);

        Color c2 = new Color((int) (Math.random() * 254) + 1,
                             (int) (Math.random() * 254) + 1,
                             (int) (Math.random() * 254) + 1);

        g.setPaint(new GradientPaint(0, 0, c1, screenWidth / 5,
                                     screenHeight / 5, c2, true));
      }

    else if (texture == null)
      g.setPaint(new Color((int) (Math.random() * 254) + 1,
                           (int) (Math.random() * 254) + 1,
                           (int) (Math.random() * 254) + 1));

    // Set stroke width and options
    if (strokeFlag)
      {
        int cap = (int) (Math.random() * 3 + 1);
        if (cap == 1)
          cap = BasicStroke.CAP_SQUARE;
        else if (cap == 2)
          cap = BasicStroke.CAP_BUTT;
        else
          cap = BasicStroke.CAP_ROUND;

        int join = (int) (Math.random() * 3 + 1);
        if (join == 1)
          join = BasicStroke.JOIN_MITER;
        else if (join == 2)
          join = BasicStroke.JOIN_BEVEL;
        else
          join = BasicStroke.JOIN_ROUND;

        float[] dashes = { 10, 10 };
        g.setStroke(new BasicStroke((int) (Math.random() * 10), cap, join, 10f,
                                    dashes, 0));
      }

    // Composite / transparency
    if (composite == - 1)
      {
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
                                                  (float) Math.random()));
      }

    // Transformations
    if (rotate == - 1)
      g.rotate(Math.random() * Math.PI * 2);
  }

  /**
   * Draws random arcs within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawArc(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize;
    long startTime;
    long endTime;
    minSize = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - minSize + 1));
        int y = (int) (Math.random() * (size.height - minSize + 1));
        int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
        int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
        int startAngle = (int) (Math.random() * 360);
        int arcAngle = (int) (Math.random() * 360 - startAngle);

        Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle,
                                     Arc2D.OPEN);
        g.draw(arc);
      }
    endTime = System.currentTimeMillis();
    recordTest("draw(Arc2D.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random filled arcs within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_fillArc(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize;
    long startTime;
    long endTime;
    minSize = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - minSize + 1));
        int y = (int) (Math.random() * (size.height - minSize + 1));
        int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
        int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
        int startAngle = (int) (Math.random() * 360);
        int arcAngle = (int) (Math.random() * 360);

        Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle,
                                     Arc2D.OPEN);
        g.fill(arc);
      }
    endTime = System.currentTimeMillis();
    recordTest("fill(Arc2D.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random cubic curves within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawCubicCurve(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int xc1 = (int) (Math.random() * (size.width - minSize));
        int yc1 = (int) (Math.random() * (size.height - minSize));
        int xc2 = (int) (Math.random() * (size.width - minSize));
        int yc2 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));

        CubicCurve2D curve = new CubicCurve2D.Double(x1, y1, xc1, yc1, xc2,
                                                     yc2, x2, y2);
        g.draw(curve);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(CubicCurve2D.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random ellipses within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawEllipse(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));
        Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2);
        g.draw(ellipse);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(Ellipse.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random ellipses within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_fillEllipse(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));
        Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2);
        g.fill(ellipse);
      }
    long endTime = System.currentTimeMillis();
    recordTest("fill(Ellipse.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  // TODO: fix the GeneralPath methods.
  /**
   * Draws random polygons within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawGeneralPath(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    long startTime = System.currentTimeMillis();

    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int points = (int) (Math.random() * 6) + 2;
        GeneralPath shape = new GeneralPath();
        shape.moveTo((float) Math.random() * (size.width),
                     (float) Math.random() * (size.height));
        for (int j = 0; j < points; j += 1)
          {
            shape.lineTo((float) (Math.random() * (size.width)),
                         (float) (Math.random() * (size.height)));
          }
        g.draw(shape);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(GeneralPath) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random filled polygons within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_fillGeneralPath(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    long startTime = System.currentTimeMillis();

    GeneralPath shape = new GeneralPath();
    shape.moveTo((float) Math.random() * (size.width), (float) Math.random()
                                                       * (size.height));

    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int points = (int) (Math.random() * 6) + 2;
        for (int j = 0; j < points; j += 1)
          {
            shape.lineTo((float) (Math.random() * (size.width)),
                         (float) (Math.random() * (size.height)));
          }
        g.fill(shape);
      }
    long endTime = System.currentTimeMillis();
    recordTest("fill(GeneralPath) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random lines within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawLine(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));
        Line2D line = new Line2D.Double(x1, y1, x2, y2);
        g.draw(line);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(Line2D.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random quadratic curves within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawQuadCurve(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int xc = (int) (Math.random() * (size.width - minSize));
        int yc = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));

        QuadCurve2D curve = new QuadCurve2D.Double(x1, y1, xc, yc, x2, y2);
        g.draw(curve);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(QuadCurve2D.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random rectangles within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawRectangle(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));
        Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2);
        g.draw(rect);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw(Rectangle.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random rectangles within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_fillRectangle(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize = 10;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x1 = (int) (Math.random() * (size.width - minSize));
        int y1 = (int) (Math.random() * (size.height - minSize));
        int x2 = (int) (Math.random() * (size.width - minSize));
        int y2 = (int) (Math.random() * (size.height - minSize));
        Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2);
        g.fill(rect);
      }
    long endTime = System.currentTimeMillis();
    recordTest("fill(Rectangle.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random rounded rectangles within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawRoundRectangle(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize;
    long startTime;
    long endTime;
    minSize = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - minSize + 1));
        int y = (int) (Math.random() * (size.height - minSize + 1));
        int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
        int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
        int arcWidth = (int) (Math.random() * (width - 1) + 1);
        int arcHeight = (int) (Math.random() * (height - 1) + 5);
        RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width,
                                                            height, arcWidth,
                                                            arcHeight);
        g.draw(rect);
      }
    endTime = System.currentTimeMillis();
    recordTest("draw(RoundRectangle.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random filled rounded rectangles within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_fillRoundRectangle(Graphics2D g, Dimension size)
  {
    int maxTests = testSize;
    int minSize;
    long startTime;
    long endTime;
    minSize = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - minSize + 1));
        int y = (int) (Math.random() * (size.height - minSize + 1));
        int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
        int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
        int arcWidth = (int) (Math.random() * (width - 1) + 1);
        int arcHeight = (int) (Math.random() * (height - 1) + 5);
        RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width,
                                                            height, arcWidth,
                                                            arcHeight);
        g.fill(rect);
      }
    endTime = System.currentTimeMillis();
    recordTest("fill(RoundRectangle.Double) " + maxTests + " times",
               (endTime - startTime));
  }

  /**
   * Draws random images within the given dimensions.
   * 
   * @param g The Graphics2D object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawImage(Graphics2D g, Dimension size)
  {
    if (gifTestImage == null)
      {
        logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "runTestSet",
                    "Skipping 'test_drawImage' due to missing resource.");
        return;
      }

    int maxTests = testSize / 2;
    if (maxTests == 0)
      maxTests = 1;
    int imageWidth = gifTestImage.getWidth(this);
    int imageHeight = gifTestImage.getHeight(this);
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - imageWidth + 1));
        int y = (int) (Math.random() * (size.height - imageHeight + 1));
        g.drawImage(gifTestImage, x, y, this);
      }
    long endTime = System.currentTimeMillis();
    recordTest("drawImage " + maxTests + " times", (endTime - startTime));
  }

  /**
   * Draws random transparent images within the given dimensions.
   * 
   * @param g The Graphics object that is used to paint.
   * @param size The size of the canvas.
   */
  private void test_drawTransparentImage(Graphics2D g, Dimension size)
  {
    if (pngTestImage == null)
      {
        logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet",
                    "Skipping 'drawTransparentImage' due to missing resource.");
        return;
      }

    int maxTests = testSize / 5;
    if (maxTests == 0)
      maxTests = 1;
    int imageWidth = pngTestImage.getWidth(this);
    int imageHeight = pngTestImage.getHeight(this);
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < maxTests; i += 1)
      {
        setRandom(g, size);
        int x = (int) (Math.random() * (size.width - imageWidth + 1));
        int y = (int) (Math.random() * (size.height - imageHeight + 1));
        g.drawImage(pngTestImage, x, y, this);
      }
    long endTime = System.currentTimeMillis();
    recordTest("draw transparent image " + maxTests + " times",
               (endTime - startTime));
  }

  private class GraphicsTest
      extends Canvas
      implements Runnable
  {
    Thread paintThread;

    boolean done = false;

    boolean doPaint = false;

    boolean withClipping = false;

    public GraphicsTest()
    {
      paintThread = new Thread(this);
      paintThread.start();
    }

    public void run()
    {
      int runCount = 0;
      while (! done)
        {
          runCount++;

          try
            {
              synchronized (this)
                {
                  while (! doPaint)
                    {
                      try
                        {
                          wait(200);
                        }
                      catch (InterruptedException exception)
                        {
                          return;
                        }
                    }
                }

              // if (iterations != 0)
              // System.out.println("--- run...("
              // + runCount
              // + "/"
              // + iterations
              // + ") ------------------------------------------------------");

              Graphics g = getGraphics();
              Dimension size = getSize();

              if (singleBufferFlag)
                {
                  logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest",
                              "run",
                              "Start testing non-double-buffered drawing");

                  if (noClippingFlag)
                    runSet_noClipping((Graphics2D) g, size, runCount);

                  if (withClippingFlag)
                    runSet_withClipping((Graphics2D) g, size, runCount);

                  if (zeroClippingFlag)
                    runSet_zeroClipping((Graphics2D) g, size, runCount);

                  g.dispose();
                }

              if (doubleBufferFlag)
                {
                  logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest",
                              "run", "Start testing double-buffered drawing");
                  Graphics canvas = getGraphics();
                  Image doublebuffer = createImage(size.width, size.height);

                  if (noClippingFlag)
                    {
                      g = doublebuffer.getGraphics();
                      runSet_noClipping((Graphics2D) g, size,
                                        "double buffering", runCount);
                      g.dispose();
                      canvas.drawImage(doublebuffer, 0, 0, this);
                    }

                  if (withClippingFlag)
                    {
                      g = doublebuffer.getGraphics();
                      runSet_withClipping((Graphics2D) g, size,
                                          "double buffering", runCount);
                      g.dispose();
                      canvas.drawImage(doublebuffer, 0, 0, this);
                    }

                  if (zeroClippingFlag)
                    {
                      g = doublebuffer.getGraphics();
                      runSet_zeroClipping((Graphics2D) g, size,
                                          "double buffering", runCount);
                      g.dispose();
                      canvas.drawImage(doublebuffer, 0, 0, this);
                      canvas.dispose();
                    }
                }

              printReport();

              if (iterations != 1)
                {
                  if (iterations != - 1)
                    iterations--;
                }
              else
                {
                  // System.out.println("--- done
                  // --------------------------------------------------------");
                  synchronized (this)
                    {
                      doPaint = false;
                    }
                  done = true;
                }
            }
          catch (Error error)
            {
              System.err.println("Error: " + error);
              System.exit(129);
            }
        }
      testComplete();
    }

    private void runSet_zeroClipping(Graphics2D g, Dimension size, int runCount)
    {
      runSet_zeroClipping(g, size, "", runCount);
    }

    private void runSet_zeroClipping(Graphics2D g, Dimension size,
                                     String context, int runCount)
    {
      int clipped_width;
      int clipped_height;
      int clipped_x;
      int clipped_y;

      clipped_width = 0;
      clipped_height = 0;
      clipped_x = (size.width) / 2;
      clipped_y = (size.height) / 2;

      // Reset any transforms from past tests
      resetGraphics(g);

      Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
      g.setClip(fullWindow);
      g.setPaint(Color.BLACK);
      g.fill(fullWindow);

      Rectangle windowBorder = new Rectangle(0, 0, size.width - 1,
                                             size.width - 1);
      g.setPaint(Color.WHITE);
      g.draw(windowBorder);

      Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1,
                                            clipped_width + 2,
                                            clipped_height + 2);
      g.fill(innerBorder);

      Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width,
                                         clipped_height);
      g.clip(innerBox);
      g.setPaint(Color.BLACK);
      g.fill(fullWindow);

      if (context.equals(""))
        setTestContext("(" + runCount + ") clipping to zero");
      else
        setTestContext("(" + runCount + ") clipping to zero (" + context + ")");

      runTestSet(g, size);
    }

    private void runSet_withClipping(Graphics2D g, Dimension size, int runCount)
    {
      runSet_withClipping(g, size, "", runCount);
    }

    private void runSet_withClipping(Graphics2D g, Dimension size,
                                     String context, int runCount)
    {
      int clipped_width = 2 * size.width / 3;
      int clipped_height = 2 * size.height / 3;
      int clipped_x = (size.width - clipped_width) / 2;
      int clipped_y = (size.height - clipped_height) / 2;

      // Reset any transforms from past tests
      resetGraphics(g);

      Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
      g.setClip(fullWindow);

      g.setPaint(Color.BLACK);
      g.fill(fullWindow);

      Rectangle windowBorder = new Rectangle(0, 0, size.width - 1,
                                             size.height - 1);
      g.setPaint(Color.GREEN);
      g.draw(windowBorder);

      Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1,
                                            clipped_width + 2,
                                            clipped_height + 2);
      g.setPaint(Color.WHITE);
      g.fill(innerBorder);

      Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width,
                                         clipped_height);
      g.clip(innerBox);

      g.setPaint(Color.BLACK);
      g.fill(fullWindow);

      if (context.equals(""))
        setTestContext("(" + runCount + ") with clipping ");
      else
        setTestContext("(" + runCount + ") with clipping (" + context + ")");

      runTestSet(g, size);
    }

    private void runSet_noClipping(Graphics2D g, Dimension size, int runCount)
    {
      runSet_noClipping(g, size, "", runCount);
    }

    private void runSet_noClipping(Graphics2D g, Dimension size,
                                   String context, int runCount)
    {
      // Reset any transforms from past tests
      resetGraphics(g);

      Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
      g.setPaint(Color.BLACK);
      g.fill(fullWindow);

      if (context.equals(""))
        setTestContext("(" + runCount + ") without clipping");
      else
        setTestContext("(" + runCount + ") without clipping (" + context + ")");

      runTestSet(g, size);
    }

    public void paint(Graphics g)
    {
      synchronized (this)
        {
          doPaint = true;
          notify();
        }
    }
  }
}

class TestContext
{
}

class TestSet
{
  private Map testsMap = new TreeMap();

  public void putTest(String testName, TestRecorder recoder)
  {
    testsMap.put(testName, recoder);
  }

  public TestRecorder getTest(String testName)
  {
    return (TestRecorder) testsMap.get(testName);
  }

  public Iterator testIterator()
  {
    return testsMap.keySet().iterator();
  }
}

class TestRecorder
{
  String test;

  long totalTime = 0;

  long minTime = Long.MAX_VALUE;

  long maxTime = Long.MIN_VALUE;

  int runCount = 0;

  /**
   * @return Returns the maxTime.
   */
  public final long getMaxTime()
  {
    return maxTime;
  }

  /**
   * @return Returns the minTime.
   */
  public final long getMinTime()
  {
    return minTime;
  }

  /**
   * @return Returns the test name.
   */
  public final String getTestName()
  {
    return test;
  }

  public final long getAverage()
  {
    return (totalTime / runCount);
  }

  public TestRecorder(String testName)
  {
    test = testName;
  }

  public void addRun(long time)
  {
    totalTime += time;
    if (minTime > time)
      minTime = time;
    if (maxTime < time)
      maxTime = time;
    runCount += 1;
  }
}