1 /* J2dBenchmark.java -- Benchmarking utility for java2d,
2 based on the Aicas AWT benchmarker
3 Copyright (C) 2006 Free Software Foundation, Inc.
5 This file is part of GNU Classpath examples.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 package gnu.classpath.examples.java2d;
24 import java.awt.AlphaComposite;
25 import java.awt.BasicStroke;
26 import java.awt.BorderLayout;
27 import java.awt.Canvas;
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Frame;
31 import java.awt.GradientPaint;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.Image;
35 import java.awt.Insets;
36 import java.awt.Label;
37 import java.awt.MediaTracker;
38 import java.awt.Panel;
39 import java.awt.Rectangle;
40 import java.awt.RenderingHints;
41 import java.awt.TexturePaint;
42 import java.awt.Toolkit;
43 import java.awt.event.WindowAdapter;
44 import java.awt.event.WindowEvent;
45 import java.awt.geom.AffineTransform;
46 import java.awt.geom.Arc2D;
47 import java.awt.geom.CubicCurve2D;
48 import java.awt.geom.Ellipse2D;
49 import java.awt.geom.GeneralPath;
50 import java.awt.geom.Line2D;
51 import java.awt.geom.QuadCurve2D;
52 import java.awt.geom.Rectangle2D;
53 import java.awt.geom.RoundRectangle2D;
54 import java.awt.image.BufferedImage;
56 import java.util.Iterator;
58 import java.util.StringTokenizer;
59 import java.util.TreeMap;
60 import java.util.logging.Level;
61 import java.util.logging.Logger;
63 public class J2dBenchmark
67 * Default number of test-iterations.
69 protected static final int DEFAULT_TEST_SIZE = 1000;
72 * Default screen size.
74 protected static final int DEFAULT_SCREEN_WIDTH = 320;
76 protected static final int DEFAULT_SCREEN_HEIGHT = 240;
81 protected static final int J2DTEST_ARC = 1 << 0;
83 protected static final int J2DTEST_CUBICCURVE = 1 << 1;
85 protected static final int J2DTEST_ELLIPSE = 1 << 2;
87 protected static final int J2DTEST_GENERALPATH = 1 << 3;
89 protected static final int J2DTEST_LINE = 1 << 4;
91 protected static final int J2DTEST_QUADCURVE = 1 << 5;
93 protected static final int J2DTEST_RECTANGLE = 1 << 6;
95 protected static final int J2DTEST_ROUNDRECTANGLE = 1 << 7;
97 protected static final int J2DTEST_IMAGE = 1 << 8;
99 protected static final int J2DTEST_NONE = 0;
102 private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE
104 | J2DTEST_GENERALPATH | J2DTEST_LINE
107 | J2DTEST_ROUNDRECTANGLE
110 private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE
115 | J2DTEST_ROUNDRECTANGLE
120 protected int screenWidth = DEFAULT_SCREEN_WIDTH;
122 protected int screenHeight = DEFAULT_SCREEN_HEIGHT;
124 protected boolean noClippingFlag = true;
126 protected boolean withClippingFlag = true;
128 protected boolean zeroClippingFlag = true;
130 protected boolean singleBufferFlag = true;
132 protected boolean doubleBufferFlag = true;
134 protected boolean gradientFlag = false;
136 protected String texture = null;
138 protected boolean strokeFlag = false;
140 protected float composite = 1;
142 protected int xtranslate = 0;
144 protected int ytranslate = 0;
146 protected double xshear = 0;
148 protected double yshear = 0;
150 protected double rotate = 0;
152 protected boolean antialiasFlag = false;
154 protected AffineTransform affineTransform = null;
156 protected int awtTests = J2DTEST_ALL;
158 protected int testSize = DEFAULT_TEST_SIZE;
160 private Label testLabel;
162 private String testContext = "";
164 Logger logger = Logger.getLogger("J2dGraphicsBenchmark");
166 private Image pngTestImage;
168 private Image gifTestImage;
170 protected BufferedImage textureImage;
172 protected TestSet testSetMap = new TestSet();
176 boolean loadError = false;
177 pngTestImage = loadImage("../icons/aicas.png");
178 gifTestImage = loadImage("../icons/palme.gif");
182 textureImage = loadBufferedImage(texture);
184 if (textureImage == null)
186 logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "init",
187 "Unable to load texture - defaulting "
188 + "to solid colours");
194 setLayout(new BorderLayout());
195 testLabel = new Label();
196 add(testLabel, BorderLayout.NORTH);
197 add(new GraphicsTest(), BorderLayout.CENTER);
200 return "Unable to load image";
205 void setTestContext(String testName)
207 logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest",
208 "--- Starting new test context: " + testName);
209 testContext = testName;
210 testLabel.setText(testName);
213 private void recordTest(String testName, long time)
215 logger.logp(Level.INFO, "J2dGraphicsBenchmark", "recordTest",
216 testContext + ": " + testName + " duration (ms): " + time);
217 TestRecorder recorder = testSetMap.getTest(testName);
218 if (recorder == null)
220 recorder = new TestRecorder(testName);
221 testSetMap.putTest(testName, recorder);
223 recorder.addRun(time);
228 for (Iterator i = testSetMap.testIterator(); i.hasNext();)
230 TestRecorder recorder = testSetMap.getTest((String) i.next());
231 System.out.println("TEST " + recorder.getTestName() + ": average "
232 + recorder.getAverage() + "ms ["
233 + recorder.getMinTime() + "-"
234 + recorder.getMaxTime() + "]");
243 public static void main(String[] args)
247 boolean endOfOptionsFlag;
248 J2dBenchmark speed = new J2dBenchmark();
252 endOfOptionsFlag = false;
253 awtTests = J2DTEST_NONE;
254 while (i < args.length)
256 if (! endOfOptionsFlag)
258 if (args[i].equals("--help") || args[i].equals("-help")
259 || args[i].equals("-h"))
261 System.out.println("Usage: J2dBenchmark [<options>] [<test> ...]");
262 System.out.println("");
263 System.out.println("Options: -i|--iterations=<n|-1> - number of iterations (-1 is infinite; default "
264 + speed.iterations + ")");
265 System.out.println(" -w|--width=<n> - screen width; default "
266 + DEFAULT_SCREEN_WIDTH);
267 System.out.println(" -h|--height=<n> - screen height; default "
268 + DEFAULT_SCREEN_HEIGHT);
269 System.out.println(" -d|--noDoubleBuffer - disable double-buffering test");
270 System.out.println(" -s|--testsize=<n> - size of each test; default "
271 + DEFAULT_TEST_SIZE);
272 System.out.println(" -c|--noClipping - disable clipping test");
273 System.out.println(" -z|--noZeroClipping - disable clipping to zero test");
274 System.out.println("");
275 System.out.println("Additional options:");
276 System.out.println(" --with-gradients - enable gradients (not compatible with --texture)");
277 System.out.println(" --with-stroking - enable random stroking");
278 System.out.println(" --texture=<file> - enable texturing with this file (not compatible with --with-gradients)");
279 System.out.println(" --composite=<n|-1> - set alpha composite level; -1 for random; default 1.0 (no transparency)");
280 System.out.println(" --anti-alias=<on|off> - set anti-aliasing hint (not all implementations respect this); default off");
281 System.out.println(" --x-translate=<n> - set x-axis translation; default 0");
282 System.out.println(" --y-translate=<n> - set y-axis translation; default 0");
283 System.out.println(" --x-shear=<n> - set x-axis shear; default 0");
284 System.out.println(" --y-shear=<n> - set y-axis shear; default 0");
285 System.out.println(" --rotate=<n|-1> - set rotation (radians); -1 for random; default: 0 (none)");
286 System.out.println("");
287 System.out.println("Tests: arc");
288 System.out.println(" cubiccurve");
289 System.out.println(" ellipse");
290 // System.out.println(" generalpath");
291 System.out.println(" line");
292 System.out.println(" quadcurve");
293 System.out.println(" rectangle");
294 System.out.println(" roundrectangle");
295 System.out.println(" image");
298 else if ((args[i].startsWith("-i=") || args[i].startsWith("--iterations=")))
300 speed.iterations = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
304 else if ((args[i].equals("-i") || args[i].equals("--iterations")))
306 if ((i + 1) >= args.length)
308 System.err.println("ERROR: No argument given for option '"
312 speed.iterations = Integer.parseInt(args[i + 1]);
316 else if ((args[i].startsWith("-w=") || args[i].startsWith("--width=")))
318 speed.screenWidth = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
322 else if ((args[i].equals("-w") || args[i].equals("--width")))
324 if ((i + 1) >= args.length)
326 System.err.println("ERROR: No argument given for option '"
330 speed.screenWidth = Integer.parseInt(args[i + 1]);
334 else if ((args[i].startsWith("-h=") || args[i].startsWith("--height=")))
336 speed.screenHeight = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
340 else if ((args[i].equals("-h") || args[i].equals("--height")))
342 if ((i + 1) >= args.length)
344 System.err.println("ERROR: No argument given for option '"
348 speed.screenHeight = Integer.parseInt(args[i + 1]);
352 else if ((args[i].equals("-d") || args[i].equals("--noDoubleBuffer")))
354 speed.doubleBufferFlag = false;
358 else if ((args[i].startsWith("-s=") || args[i].startsWith("--testsize=")))
360 if ((i + 1) >= args.length)
362 System.err.println("ERROR: No argument given for option '"
366 speed.testSize = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
370 else if ((args[i].equals("-s") || args[i].equals("--testsize")))
372 if ((i + 1) >= args.length)
374 System.err.println("ERROR: No argument given for option '"
378 speed.testSize = Integer.parseInt(args[i + 1]);
382 else if ((args[i].equals("-c") || args[i].equals("--noClipping")))
384 speed.noClippingFlag = false;
388 else if ((args[i].equals("-z") || args[i].equals("--noZeroClipping")))
390 speed.zeroClippingFlag = false;
394 else if (args[i].equals("--with-gradients"))
396 speed.gradientFlag = true;
400 else if (args[i].equals("--with-stroking"))
402 speed.strokeFlag = true;
406 else if (args[i].startsWith("--texture="))
408 speed.texture = args[i].substring(args[i].indexOf('=') + 1);
412 else if (args[i].startsWith("--composite="))
414 speed.composite = Float.parseFloat(args[i].substring(args[i].indexOf('=') + 1));
415 if (speed.composite != - 1
416 && (speed.composite < 0 || speed.composite > 1))
418 System.err.println("ERROR: Invalid value for composite (must be between 0 and 1, or -1 for random)");
424 else if (args[i].startsWith("--anti-alias="))
426 speed.antialiasFlag = (args[i].substring(args[i].indexOf('=') + 1).equals("on"));
430 else if (args[i].startsWith("--x-translate="))
432 speed.xtranslate = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
436 else if (args[i].startsWith("--y-translate="))
438 speed.ytranslate = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1));
442 else if (args[i].startsWith("--x-shear="))
444 speed.xshear = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
448 else if (args[i].startsWith("--y-shear="))
450 speed.yshear = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
454 else if (args[i].startsWith("--rotate="))
456 speed.rotate = Double.parseDouble(args[i].substring(args[i].indexOf('=') + 1));
461 else if (args[i].equals("--"))
463 endOfOptionsFlag = true;
467 else if (args[i].startsWith("-"))
469 System.err.println("ERROR: Unknown option '" + args[i] + "'!");
473 StringTokenizer tokenizer = new StringTokenizer(args[i], " +,");
474 while (tokenizer.hasMoreTokens())
476 String s = tokenizer.nextToken().toLowerCase();
478 awtTests |= J2DTEST_ARC;
479 else if (s.equals("cubiccurve"))
480 awtTests |= J2DTEST_CUBICCURVE;
481 else if (s.equals("ellipse"))
482 awtTests |= J2DTEST_ELLIPSE;
483 else if (s.equals("generalpath"))
484 awtTests |= J2DTEST_GENERALPATH;
485 else if (s.equals("line"))
486 awtTests |= J2DTEST_LINE;
487 else if (s.equals("quadcurve"))
488 awtTests |= J2DTEST_QUADCURVE;
489 else if (s.equals("rectangle"))
490 awtTests |= J2DTEST_RECTANGLE;
491 else if (s.equals("roundrectangle"))
492 awtTests |= J2DTEST_ROUNDRECTANGLE;
493 else if (s.equals("image"))
494 awtTests |= J2DTEST_IMAGE;
497 System.err.println("Unknown AWT test '" + s + "'!");
503 if (awtTests != J2DTEST_NONE)
504 speed.awtTests = awtTests;
508 final Frame frame = new Frame("J2dGraphicsBenchmark");
510 frame.addWindowListener(new WindowAdapter()
512 public void windowClosing(WindowEvent e)
514 frame.setVisible(false);
519 frame.add(speed, BorderLayout.CENTER);
520 frame.setSize(speed.screenWidth, speed.screenHeight);
521 frame.setVisible(true);
523 // Insets are correctly set only after the native peer was created.
524 Insets insets = frame.getInsets();
525 // The internal size of the frame should be 320x240.
526 frame.setSize(320 + insets.right + insets.left, 240 + insets.top
530 private Image loadImage(String imageName)
533 logger.logp(Level.INFO, "J2dGraphicsBenchmark", "loadImage",
534 "Loading image: " + imageName);
535 URL url = getClass().getResource(imageName);
538 result = Toolkit.getDefaultToolkit().getImage(url);
539 prepareImage(result, this);
543 logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "loadImage",
544 "Could not locate image resource in class path: "
550 private BufferedImage loadBufferedImage(String imageName)
552 BufferedImage result = null;
553 logger.logp(Level.INFO, "J2dGraphicsBenchmark", "loadImage",
554 "Loading image: " + imageName);
556 // Try to load image out of classpath before trying an absolute filename
557 URL url = getClass().getResource(imageName);
560 img = Toolkit.getDefaultToolkit().getImage(url);
562 img = Toolkit.getDefaultToolkit().getImage(imageName);
566 // Wait for image to load
569 MediaTracker tracker = new MediaTracker(this);
570 tracker.addImage(img, 1);
571 tracker.waitForAll();
573 prepareImage(img, this);
574 result = new BufferedImage(img.getWidth(this), img.getHeight(this),
575 BufferedImage.TYPE_INT_RGB);
576 result.createGraphics().drawImage(img, 0, 0, this);
578 catch (InterruptedException e)
581 catch (IllegalArgumentException e)
588 logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "loadBufferedImage",
589 "Could not locate image resource in class path: "
596 * Executes the test methods.
598 * @param g The Graphics2D object that is used to paint.
599 * @param size The size of the canvas.
601 void runTestSet(Graphics2D g, Dimension size)
603 // Any user-specified options (ie set transforms, rendering hints)
606 if ((awtTests & J2DTEST_ARC) != 0)
608 test_drawArc(g, size);
609 test_fillArc(g, size);
612 if ((awtTests & J2DTEST_CUBICCURVE) != 0)
614 test_drawCubicCurve(g, size);
617 if ((awtTests & J2DTEST_ELLIPSE) != 0)
619 test_drawEllipse(g, size);
620 test_fillEllipse(g, size);
623 if ((awtTests & J2DTEST_GENERALPATH) != 0)
625 // Current implementation doesn't work
626 test_drawGeneralPath(g, size);
627 test_fillGeneralPath(g, size);
630 if ((awtTests & J2DTEST_LINE) != 0)
632 test_drawLine(g, size);
635 if ((awtTests & J2DTEST_QUADCURVE) != 0)
637 test_drawQuadCurve(g, size);
640 if ((awtTests & J2DTEST_RECTANGLE) != 0)
642 test_drawRectangle(g, size);
643 test_fillRectangle(g, size);
646 if ((awtTests & J2DTEST_ROUNDRECTANGLE) != 0)
648 test_drawRoundRectangle(g, size);
649 test_fillRoundRectangle(g, size);
652 if ((awtTests & J2DTEST_IMAGE) != 0)
654 test_drawImage(g, size);
655 test_drawTransparentImage(g, size);
660 * Reset all graphics settings to the standard, default values
662 * @param g the object to apply settings to
664 private void resetGraphics(Graphics2D g)
666 g.setTransform(new AffineTransform());
667 g.setStroke(new BasicStroke());
668 g.setComposite(AlphaComposite.SrcOut);
672 * Sets initial user graphics options
674 * @param g the object to apply settings to
676 private void prepareGraphics(Graphics2D g)
679 if (affineTransform != null)
680 g.setTransform(affineTransform);
682 else if (xtranslate != 0 || ytranslate != 0 || xshear != 0 || yshear != 0)
684 g.translate(xtranslate, ytranslate);
685 g.shear(xshear, yshear);
689 g.rotate(rotate * Math.PI, screenWidth / 2, screenHeight / 2);
691 // Composite (transparency)
694 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
700 g.setPaint(new TexturePaint(textureImage,
701 new Rectangle(0, 0, textureImage.getWidth(),
702 textureImage.getHeight())));
704 // Anti-alias setting
706 g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
707 RenderingHints.VALUE_ANTIALIAS_ON));
709 g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
710 RenderingHints.VALUE_ANTIALIAS_OFF));
714 * Gets new random settings
716 * @param g the object to set parameters for
717 * @param size the screen size
719 private void setRandom(Graphics2D g, Dimension size)
721 // Set colour / paint
724 Color c1 = new Color((int) (Math.random() * 254) + 1,
725 (int) (Math.random() * 254) + 1,
726 (int) (Math.random() * 254) + 1);
728 Color c2 = new Color((int) (Math.random() * 254) + 1,
729 (int) (Math.random() * 254) + 1,
730 (int) (Math.random() * 254) + 1);
732 g.setPaint(new GradientPaint(0, 0, c1, screenWidth / 5,
733 screenHeight / 5, c2, true));
736 else if (texture == null)
737 g.setPaint(new Color((int) (Math.random() * 254) + 1,
738 (int) (Math.random() * 254) + 1,
739 (int) (Math.random() * 254) + 1));
741 // Set stroke width and options
744 int cap = (int) (Math.random() * 3 + 1);
746 cap = BasicStroke.CAP_SQUARE;
748 cap = BasicStroke.CAP_BUTT;
750 cap = BasicStroke.CAP_ROUND;
752 int join = (int) (Math.random() * 3 + 1);
754 join = BasicStroke.JOIN_MITER;
756 join = BasicStroke.JOIN_BEVEL;
758 join = BasicStroke.JOIN_ROUND;
760 float[] dashes = { 10, 10 };
761 g.setStroke(new BasicStroke((int) (Math.random() * 10), cap, join, 10f,
765 // Composite / transparency
766 if (composite == - 1)
768 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
769 (float) Math.random()));
774 g.rotate(Math.random() * Math.PI * 2);
778 * Draws random arcs within the given dimensions.
780 * @param g The Graphics2D object that is used to paint.
781 * @param size The size of the canvas.
783 private void test_drawArc(Graphics2D g, Dimension size)
785 int maxTests = testSize;
790 startTime = System.currentTimeMillis();
791 for (int i = 0; i < maxTests; i += 1)
794 int x = (int) (Math.random() * (size.width - minSize + 1));
795 int y = (int) (Math.random() * (size.height - minSize + 1));
796 int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
797 int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
798 int startAngle = (int) (Math.random() * 360);
799 int arcAngle = (int) (Math.random() * 360 - startAngle);
801 Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle,
805 endTime = System.currentTimeMillis();
806 recordTest("draw(Arc2D.Double) " + maxTests + " times",
807 (endTime - startTime));
811 * Draws random filled arcs within the given dimensions.
813 * @param g The Graphics2D object that is used to paint.
814 * @param size The size of the canvas.
816 private void test_fillArc(Graphics2D g, Dimension size)
818 int maxTests = testSize;
823 startTime = System.currentTimeMillis();
824 for (int i = 0; i < maxTests; i += 1)
827 int x = (int) (Math.random() * (size.width - minSize + 1));
828 int y = (int) (Math.random() * (size.height - minSize + 1));
829 int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
830 int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
831 int startAngle = (int) (Math.random() * 360);
832 int arcAngle = (int) (Math.random() * 360);
834 Arc2D arc = new Arc2D.Double(x, y, width, height, startAngle, arcAngle,
838 endTime = System.currentTimeMillis();
839 recordTest("fill(Arc2D.Double) " + maxTests + " times",
840 (endTime - startTime));
844 * Draws random cubic curves within the given dimensions.
846 * @param g The Graphics2D object that is used to paint.
847 * @param size The size of the canvas.
849 private void test_drawCubicCurve(Graphics2D g, Dimension size)
851 int maxTests = testSize;
853 long startTime = System.currentTimeMillis();
854 for (int i = 0; i < maxTests; i += 1)
857 int x1 = (int) (Math.random() * (size.width - minSize));
858 int y1 = (int) (Math.random() * (size.height - minSize));
859 int xc1 = (int) (Math.random() * (size.width - minSize));
860 int yc1 = (int) (Math.random() * (size.height - minSize));
861 int xc2 = (int) (Math.random() * (size.width - minSize));
862 int yc2 = (int) (Math.random() * (size.height - minSize));
863 int x2 = (int) (Math.random() * (size.width - minSize));
864 int y2 = (int) (Math.random() * (size.height - minSize));
866 CubicCurve2D curve = new CubicCurve2D.Double(x1, y1, xc1, yc1, xc2,
870 long endTime = System.currentTimeMillis();
871 recordTest("draw(CubicCurve2D.Double) " + maxTests + " times",
872 (endTime - startTime));
876 * Draws random ellipses within the given dimensions.
878 * @param g The Graphics2D object that is used to paint.
879 * @param size The size of the canvas.
881 private void test_drawEllipse(Graphics2D g, Dimension size)
883 int maxTests = testSize;
885 long startTime = System.currentTimeMillis();
886 for (int i = 0; i < maxTests; i += 1)
889 int x1 = (int) (Math.random() * (size.width - minSize));
890 int y1 = (int) (Math.random() * (size.height - minSize));
891 int x2 = (int) (Math.random() * (size.width - minSize));
892 int y2 = (int) (Math.random() * (size.height - minSize));
893 Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2);
896 long endTime = System.currentTimeMillis();
897 recordTest("draw(Ellipse.Double) " + maxTests + " times",
898 (endTime - startTime));
902 * Draws random ellipses within the given dimensions.
904 * @param g The Graphics2D object that is used to paint.
905 * @param size The size of the canvas.
907 private void test_fillEllipse(Graphics2D g, Dimension size)
909 int maxTests = testSize;
911 long startTime = System.currentTimeMillis();
912 for (int i = 0; i < maxTests; i += 1)
915 int x1 = (int) (Math.random() * (size.width - minSize));
916 int y1 = (int) (Math.random() * (size.height - minSize));
917 int x2 = (int) (Math.random() * (size.width - minSize));
918 int y2 = (int) (Math.random() * (size.height - minSize));
919 Ellipse2D ellipse = new Ellipse2D.Double(x1, y1, x2, y2);
922 long endTime = System.currentTimeMillis();
923 recordTest("fill(Ellipse.Double) " + maxTests + " times",
924 (endTime - startTime));
927 // TODO: fix the GeneralPath methods.
929 * Draws random polygons within the given dimensions.
931 * @param g The Graphics2D object that is used to paint.
932 * @param size The size of the canvas.
934 private void test_drawGeneralPath(Graphics2D g, Dimension size)
936 int maxTests = testSize;
937 long startTime = System.currentTimeMillis();
939 for (int i = 0; i < maxTests; i += 1)
942 int points = (int) (Math.random() * 6) + 2;
943 GeneralPath shape = new GeneralPath();
944 shape.moveTo((float) Math.random() * (size.width),
945 (float) Math.random() * (size.height));
946 for (int j = 0; j < points; j += 1)
948 shape.lineTo((float) (Math.random() * (size.width)),
949 (float) (Math.random() * (size.height)));
953 long endTime = System.currentTimeMillis();
954 recordTest("draw(GeneralPath) " + maxTests + " times",
955 (endTime - startTime));
959 * Draws random filled polygons within the given dimensions.
961 * @param g The Graphics2D object that is used to paint.
962 * @param size The size of the canvas.
964 private void test_fillGeneralPath(Graphics2D g, Dimension size)
966 int maxTests = testSize;
967 long startTime = System.currentTimeMillis();
969 GeneralPath shape = new GeneralPath();
970 shape.moveTo((float) Math.random() * (size.width), (float) Math.random()
973 for (int i = 0; i < maxTests; i += 1)
976 int points = (int) (Math.random() * 6) + 2;
977 for (int j = 0; j < points; j += 1)
979 shape.lineTo((float) (Math.random() * (size.width)),
980 (float) (Math.random() * (size.height)));
984 long endTime = System.currentTimeMillis();
985 recordTest("fill(GeneralPath) " + maxTests + " times",
986 (endTime - startTime));
990 * Draws random lines within the given dimensions.
992 * @param g The Graphics2D object that is used to paint.
993 * @param size The size of the canvas.
995 private void test_drawLine(Graphics2D g, Dimension size)
997 int maxTests = testSize;
999 long startTime = System.currentTimeMillis();
1000 for (int i = 0; i < maxTests; i += 1)
1003 int x1 = (int) (Math.random() * (size.width - minSize));
1004 int y1 = (int) (Math.random() * (size.height - minSize));
1005 int x2 = (int) (Math.random() * (size.width - minSize));
1006 int y2 = (int) (Math.random() * (size.height - minSize));
1007 Line2D line = new Line2D.Double(x1, y1, x2, y2);
1010 long endTime = System.currentTimeMillis();
1011 recordTest("draw(Line2D.Double) " + maxTests + " times",
1012 (endTime - startTime));
1016 * Draws random quadratic curves within the given dimensions.
1018 * @param g The Graphics2D object that is used to paint.
1019 * @param size The size of the canvas.
1021 private void test_drawQuadCurve(Graphics2D g, Dimension size)
1023 int maxTests = testSize;
1025 long startTime = System.currentTimeMillis();
1026 for (int i = 0; i < maxTests; i += 1)
1029 int x1 = (int) (Math.random() * (size.width - minSize));
1030 int y1 = (int) (Math.random() * (size.height - minSize));
1031 int xc = (int) (Math.random() * (size.width - minSize));
1032 int yc = (int) (Math.random() * (size.height - minSize));
1033 int x2 = (int) (Math.random() * (size.width - minSize));
1034 int y2 = (int) (Math.random() * (size.height - minSize));
1036 QuadCurve2D curve = new QuadCurve2D.Double(x1, y1, xc, yc, x2, y2);
1039 long endTime = System.currentTimeMillis();
1040 recordTest("draw(QuadCurve2D.Double) " + maxTests + " times",
1041 (endTime - startTime));
1045 * Draws random rectangles within the given dimensions.
1047 * @param g The Graphics2D object that is used to paint.
1048 * @param size The size of the canvas.
1050 private void test_drawRectangle(Graphics2D g, Dimension size)
1052 int maxTests = testSize;
1054 long startTime = System.currentTimeMillis();
1055 for (int i = 0; i < maxTests; i += 1)
1058 int x1 = (int) (Math.random() * (size.width - minSize));
1059 int y1 = (int) (Math.random() * (size.height - minSize));
1060 int x2 = (int) (Math.random() * (size.width - minSize));
1061 int y2 = (int) (Math.random() * (size.height - minSize));
1062 Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2);
1065 long endTime = System.currentTimeMillis();
1066 recordTest("draw(Rectangle.Double) " + maxTests + " times",
1067 (endTime - startTime));
1071 * Draws random rectangles within the given dimensions.
1073 * @param g The Graphics2D object that is used to paint.
1074 * @param size The size of the canvas.
1076 private void test_fillRectangle(Graphics2D g, Dimension size)
1078 int maxTests = testSize;
1080 long startTime = System.currentTimeMillis();
1081 for (int i = 0; i < maxTests; i += 1)
1084 int x1 = (int) (Math.random() * (size.width - minSize));
1085 int y1 = (int) (Math.random() * (size.height - minSize));
1086 int x2 = (int) (Math.random() * (size.width - minSize));
1087 int y2 = (int) (Math.random() * (size.height - minSize));
1088 Rectangle2D rect = new Rectangle2D.Double(x1, y1, x2, y2);
1091 long endTime = System.currentTimeMillis();
1092 recordTest("fill(Rectangle.Double) " + maxTests + " times",
1093 (endTime - startTime));
1097 * Draws random rounded rectangles within the given dimensions.
1099 * @param g The Graphics2D object that is used to paint.
1100 * @param size The size of the canvas.
1102 private void test_drawRoundRectangle(Graphics2D g, Dimension size)
1104 int maxTests = testSize;
1109 startTime = System.currentTimeMillis();
1110 for (int i = 0; i < maxTests; i += 1)
1113 int x = (int) (Math.random() * (size.width - minSize + 1));
1114 int y = (int) (Math.random() * (size.height - minSize + 1));
1115 int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
1116 int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
1117 int arcWidth = (int) (Math.random() * (width - 1) + 1);
1118 int arcHeight = (int) (Math.random() * (height - 1) + 5);
1119 RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width,
1124 endTime = System.currentTimeMillis();
1125 recordTest("draw(RoundRectangle.Double) " + maxTests + " times",
1126 (endTime - startTime));
1130 * Draws random filled rounded rectangles within the given dimensions.
1132 * @param g The Graphics2D object that is used to paint.
1133 * @param size The size of the canvas.
1135 private void test_fillRoundRectangle(Graphics2D g, Dimension size)
1137 int maxTests = testSize;
1142 startTime = System.currentTimeMillis();
1143 for (int i = 0; i < maxTests; i += 1)
1146 int x = (int) (Math.random() * (size.width - minSize + 1));
1147 int y = (int) (Math.random() * (size.height - minSize + 1));
1148 int width = (int) (Math.random() * (size.width - x - minSize) + minSize);
1149 int height = (int) (Math.random() * (size.height - y - minSize) + minSize);
1150 int arcWidth = (int) (Math.random() * (width - 1) + 1);
1151 int arcHeight = (int) (Math.random() * (height - 1) + 5);
1152 RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, width,
1157 endTime = System.currentTimeMillis();
1158 recordTest("fill(RoundRectangle.Double) " + maxTests + " times",
1159 (endTime - startTime));
1163 * Draws random images within the given dimensions.
1165 * @param g The Graphics2D object that is used to paint.
1166 * @param size The size of the canvas.
1168 private void test_drawImage(Graphics2D g, Dimension size)
1170 if (gifTestImage == null)
1172 logger.logp(Level.WARNING, "J2dGraphicsBenchmark", "runTestSet",
1173 "Skipping 'test_drawImage' due to missing resource.");
1177 int maxTests = testSize / 2;
1180 int imageWidth = gifTestImage.getWidth(this);
1181 int imageHeight = gifTestImage.getHeight(this);
1182 long startTime = System.currentTimeMillis();
1183 for (int i = 0; i < maxTests; i += 1)
1186 int x = (int) (Math.random() * (size.width - imageWidth + 1));
1187 int y = (int) (Math.random() * (size.height - imageHeight + 1));
1188 g.drawImage(gifTestImage, x, y, this);
1190 long endTime = System.currentTimeMillis();
1191 recordTest("drawImage " + maxTests + " times", (endTime - startTime));
1195 * Draws random transparent images within the given dimensions.
1197 * @param g The Graphics object that is used to paint.
1198 * @param size The size of the canvas.
1200 private void test_drawTransparentImage(Graphics2D g, Dimension size)
1202 if (pngTestImage == null)
1204 logger.logp(Level.WARNING, "AicasGraphicsBenchmark", "runTestSet",
1205 "Skipping 'drawTransparentImage' due to missing resource.");
1209 int maxTests = testSize / 5;
1212 int imageWidth = pngTestImage.getWidth(this);
1213 int imageHeight = pngTestImage.getHeight(this);
1214 long startTime = System.currentTimeMillis();
1215 for (int i = 0; i < maxTests; i += 1)
1218 int x = (int) (Math.random() * (size.width - imageWidth + 1));
1219 int y = (int) (Math.random() * (size.height - imageHeight + 1));
1220 g.drawImage(pngTestImage, x, y, this);
1222 long endTime = System.currentTimeMillis();
1223 recordTest("draw transparent image " + maxTests + " times",
1224 (endTime - startTime));
1227 private class GraphicsTest
1233 boolean done = false;
1235 boolean doPaint = false;
1237 boolean withClipping = false;
1239 public GraphicsTest()
1241 paintThread = new Thread(this);
1242 paintThread.start();
1262 catch (InterruptedException exception)
1269 // if (iterations != 0)
1270 // System.out.println("--- run...("
1274 // + ") ------------------------------------------------------");
1276 Graphics g = getGraphics();
1277 Dimension size = getSize();
1279 if (singleBufferFlag)
1281 logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest",
1283 "Start testing non-double-buffered drawing");
1286 runSet_noClipping((Graphics2D) g, size, runCount);
1288 if (withClippingFlag)
1289 runSet_withClipping((Graphics2D) g, size, runCount);
1291 if (zeroClippingFlag)
1292 runSet_zeroClipping((Graphics2D) g, size, runCount);
1297 if (doubleBufferFlag)
1299 logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest",
1300 "run", "Start testing double-buffered drawing");
1301 Graphics canvas = getGraphics();
1302 Image doublebuffer = createImage(size.width, size.height);
1306 g = doublebuffer.getGraphics();
1307 runSet_noClipping((Graphics2D) g, size,
1308 "double buffering", runCount);
1310 canvas.drawImage(doublebuffer, 0, 0, this);
1313 if (withClippingFlag)
1315 g = doublebuffer.getGraphics();
1316 runSet_withClipping((Graphics2D) g, size,
1317 "double buffering", runCount);
1319 canvas.drawImage(doublebuffer, 0, 0, this);
1322 if (zeroClippingFlag)
1324 g = doublebuffer.getGraphics();
1325 runSet_zeroClipping((Graphics2D) g, size,
1326 "double buffering", runCount);
1328 canvas.drawImage(doublebuffer, 0, 0, this);
1335 if (iterations != 1)
1337 if (iterations != - 1)
1342 // System.out.println("--- done
1343 // --------------------------------------------------------");
1353 System.err.println("Error: " + error);
1360 private void runSet_zeroClipping(Graphics2D g, Dimension size, int runCount)
1362 runSet_zeroClipping(g, size, "", runCount);
1365 private void runSet_zeroClipping(Graphics2D g, Dimension size,
1366 String context, int runCount)
1375 clipped_x = (size.width) / 2;
1376 clipped_y = (size.height) / 2;
1378 // Reset any transforms from past tests
1381 Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
1382 g.setClip(fullWindow);
1383 g.setPaint(Color.BLACK);
1386 Rectangle windowBorder = new Rectangle(0, 0, size.width - 1,
1388 g.setPaint(Color.WHITE);
1389 g.draw(windowBorder);
1391 Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1,
1393 clipped_height + 2);
1394 g.fill(innerBorder);
1396 Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width,
1399 g.setPaint(Color.BLACK);
1402 if (context.equals(""))
1403 setTestContext("(" + runCount + ") clipping to zero");
1405 setTestContext("(" + runCount + ") clipping to zero (" + context + ")");
1407 runTestSet(g, size);
1410 private void runSet_withClipping(Graphics2D g, Dimension size, int runCount)
1412 runSet_withClipping(g, size, "", runCount);
1415 private void runSet_withClipping(Graphics2D g, Dimension size,
1416 String context, int runCount)
1418 int clipped_width = 2 * size.width / 3;
1419 int clipped_height = 2 * size.height / 3;
1420 int clipped_x = (size.width - clipped_width) / 2;
1421 int clipped_y = (size.height - clipped_height) / 2;
1423 // Reset any transforms from past tests
1426 Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
1427 g.setClip(fullWindow);
1429 g.setPaint(Color.BLACK);
1432 Rectangle windowBorder = new Rectangle(0, 0, size.width - 1,
1434 g.setPaint(Color.GREEN);
1435 g.draw(windowBorder);
1437 Rectangle innerBorder = new Rectangle(clipped_x - 1, clipped_y - 1,
1439 clipped_height + 2);
1440 g.setPaint(Color.WHITE);
1441 g.fill(innerBorder);
1443 Rectangle innerBox = new Rectangle(clipped_x, clipped_y, clipped_width,
1447 g.setPaint(Color.BLACK);
1450 if (context.equals(""))
1451 setTestContext("(" + runCount + ") with clipping ");
1453 setTestContext("(" + runCount + ") with clipping (" + context + ")");
1455 runTestSet(g, size);
1458 private void runSet_noClipping(Graphics2D g, Dimension size, int runCount)
1460 runSet_noClipping(g, size, "", runCount);
1463 private void runSet_noClipping(Graphics2D g, Dimension size,
1464 String context, int runCount)
1466 // Reset any transforms from past tests
1469 Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
1470 g.setPaint(Color.BLACK);
1473 if (context.equals(""))
1474 setTestContext("(" + runCount + ") without clipping");
1476 setTestContext("(" + runCount + ") without clipping (" + context + ")");
1478 runTestSet(g, size);
1481 public void paint(Graphics g)
1498 private Map testsMap = new TreeMap();
1500 public void putTest(String testName, TestRecorder recoder)
1502 testsMap.put(testName, recoder);
1505 public TestRecorder getTest(String testName)
1507 return (TestRecorder) testsMap.get(testName);
1510 public Iterator testIterator()
1512 return testsMap.keySet().iterator();
1522 long minTime = Long.MAX_VALUE;
1524 long maxTime = Long.MIN_VALUE;
1529 * @return Returns the maxTime.
1531 public final long getMaxTime()
1537 * @return Returns the minTime.
1539 public final long getMinTime()
1545 * @return Returns the test name.
1547 public final String getTestName()
1552 public final long getAverage()
1554 return (totalTime / runCount);
1557 public TestRecorder(String testName)
1562 public void addRun(long time)