Hi,
Attached is a patch for a GUI to the java2d benchmarker.
Francis
2006-06-20 Francis Kung <[EMAIL PROTECTED]>
* examples/gnu/classpath/examples/java2d/J2dBenchmark.java:
Changed many members to be protected.
(J2dBenchmark): moved to init() instead.
(init): New method.
(main): Call init() after creating object
(testComplete): New method.
(test_drawArc): Use maxTests varialbe instead of constant.
(test_drawCubicCurve): Likewise.
(test_drawEllipse): Likewise.
(test_drawGeneralPath): Likewise.
(test_drawImage): Likewise.
(test_drawLine): Likewise.
(test_drawQuadCurve): Likewise.
(test_drawRectangle): Likewise.
(test_drawRoundRectangle): Likewise.
(test_drawTransparentImage): Likewise.
(test_fillArc): Likewise.
(test_fillEllipse): Likewise.
(test_fillGeneralPath): Likewise.
(test_fillRectangle): Likewise.
(test_fillRoundRectangle): Likewise.
(GraphicsTest.runSetNoClipping): Added runCount parameter.
(GraphicsTest.runSetWithClipping): Likewise.
(GraphicsTest.runSetZeroClipping): Likewise.
(GraphicsTest.run): Added checks for more option flags.
* examples/gnu/classpath/examples/java2d/J2dBenchmarkGUI.java:
New file.
Index: examples/gnu/classpath/examples/java2d/J2dBenchmark.java
===================================================================
RCS file: /cvsroot/classpath/classpath/examples/gnu/classpath/examples/java2d/J2dBenchmark.java,v
retrieving revision 1.1
diff -u -r1.1 J2dBenchmark.java
--- examples/gnu/classpath/examples/java2d/J2dBenchmark.java 16 Jun 2006 20:46:54 -0000 1.1
+++ examples/gnu/classpath/examples/java2d/J2dBenchmark.java 20 Jun 2006 15:53:36 -0000
@@ -59,37 +59,37 @@
/**
* Default number of test-iterations.
*/
- private static final int DEFAULT_TEST_SIZE = 1000;
+ protected static final int DEFAULT_TEST_SIZE = 1000;
/**
* Default screen size.
*/
- private static final int DEFAULT_SCREEN_WIDTH = 320;
+ protected static final int DEFAULT_SCREEN_WIDTH = 320;
- private static final int DEFAULT_SCREEN_HEIGHT = 240;
+ protected static final int DEFAULT_SCREEN_HEIGHT = 240;
/**
* Java2D tests.
*/
- private static final int J2DTEST_ARC = 1 << 0;
+ protected static final int J2DTEST_ARC = 1 << 0;
- private static final int J2DTEST_CUBICCURVE = 1 << 1;
+ protected static final int J2DTEST_CUBICCURVE = 1 << 1;
- private static final int J2DTEST_ELLIPSE = 1 << 2;
+ protected static final int J2DTEST_ELLIPSE = 1 << 2;
- private static final int J2DTEST_GENERALPATH = 1 << 3;
+ protected static final int J2DTEST_GENERALPATH = 1 << 3;
- private static final int J2DTEST_LINE = 1 << 4;
+ protected static final int J2DTEST_LINE = 1 << 4;
- private static final int J2DTEST_QUADCURVE = 1 << 5;
+ protected static final int J2DTEST_QUADCURVE = 1 << 5;
- private static final int J2DTEST_RECTANGLE = 1 << 6;
+ protected static final int J2DTEST_RECTANGLE = 1 << 6;
- private static final int J2DTEST_ROUNDRECTANGLE = 1 << 7;
+ protected static final int J2DTEST_ROUNDRECTANGLE = 1 << 7;
- private static final int J2DTEST_IMAGE = 1 << 8;
+ protected static final int J2DTEST_IMAGE = 1 << 8;
- private static final int J2DTEST_NONE = 0;
+ protected static final int J2DTEST_NONE = 0;
/*
private static final int J2DTEST_ALL = J2DTEST_ARC | J2DTEST_CUBICCURVE
@@ -110,13 +110,23 @@
int iterations = 1;
- private int screenWidth = DEFAULT_SCREEN_WIDTH;
+ protected int screenWidth = DEFAULT_SCREEN_WIDTH;
- private int screenHeight = DEFAULT_SCREEN_HEIGHT;
+ protected int screenHeight = DEFAULT_SCREEN_HEIGHT;
- boolean doubleBufferFlag = true;
+ protected boolean noClippingFlag = true;
- private int awtTests = J2DTEST_ALL;
+ protected boolean withClippingFlag = true;
+
+ protected boolean zeroClippingFlag = true;
+
+ protected boolean singleBufferFlag = true;
+
+ protected boolean doubleBufferFlag = true;
+
+ protected int awtTests = J2DTEST_ALL;
+
+ protected int testSize = DEFAULT_TEST_SIZE;
private Label testLabel;
@@ -128,9 +138,9 @@
private Image gifTestImage;
- private TestSet testSetMap = new TestSet();
+ protected TestSet testSetMap = new TestSet();
- public J2dBenchmark()
+ public void init()
{
pngTestImage = loadImage("aicas.png");
gifTestImage = loadImage("palme.gif");
@@ -174,12 +184,18 @@
}
}
+ void testComplete()
+ {
+ System.exit(0);
+ }
+
public static void main(String[] args)
{
int awtTests;
int i;
boolean endOfOptionsFlag;
J2dBenchmark speed = new J2dBenchmark();
+ speed.init();
// Parse arguments.
i = 0;
@@ -442,7 +458,7 @@
*/
private void test_drawArc(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize;
long startTime;
long endTime;
@@ -475,7 +491,7 @@
*/
private void test_fillArc(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize;
long startTime;
long endTime;
@@ -508,7 +524,7 @@
*/
private void test_drawCubicCurve(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -540,7 +556,7 @@
*/
private void test_drawEllipse(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -566,7 +582,7 @@
*/
private void test_fillEllipse(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -593,7 +609,7 @@
*/
private void test_drawGeneralPath(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -623,7 +639,7 @@
*/
private void test_fillGeneralPath(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
long startTime = System.currentTimeMillis();
GeneralPath shape = new GeneralPath();
@@ -654,7 +670,7 @@
*/
private void test_drawLine(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -680,7 +696,7 @@
*/
private void test_drawQuadCurve(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -709,7 +725,7 @@
*/
private void test_drawRectangle(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -735,7 +751,7 @@
*/
private void test_fillRectangle(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize = 10;
long startTime = System.currentTimeMillis();
for (int i = 0; i < maxTests; i += 1)
@@ -761,7 +777,7 @@
*/
private void test_drawRoundRectangle(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize;
long startTime;
long endTime;
@@ -794,7 +810,7 @@
*/
private void test_fillRoundRectangle(Graphics2D g, Dimension size)
{
- int maxTests = DEFAULT_TEST_SIZE;
+ int maxTests = testSize;
int minSize;
long startTime;
long endTime;
@@ -834,7 +850,7 @@
return;
}
- int maxTests = DEFAULT_TEST_SIZE / 2;
+ int maxTests = testSize / 2;
if (maxTests == 0)
maxTests = 1;
int imageWidth = gifTestImage.getWidth(this);
@@ -866,7 +882,7 @@
return;
}
- int maxTests = DEFAULT_TEST_SIZE / 5;
+ int maxTests = testSize / 5;
if (maxTests == 0)
maxTests = 1;
int imageWidth = pngTestImage.getWidth(this);
@@ -926,21 +942,33 @@
}
}
- if (iterations != 0)
- System.out.println("--- run...("
- + runCount
- + "/"
- + iterations
- + ") ------------------------------------------------------");
+ // if (iterations != 0)
+ // System.out.println("--- run...("
+ // + runCount
+ // + "/"
+ // + iterations
+ // + ") ------------------------------------------------------");
Graphics g = getGraphics();
Dimension size = getSize();
- logger.logp(Level.INFO, "J2dGraphicsBenchmark.GraphicsTest",
- "run", "Start testing non-double-buffered drawing");
- runSet_noClipping((Graphics2D) g, size);
- runSet_zeroClipping((Graphics2D) g, size);
- runSet_withClipping((Graphics2D) g, size);
- g.dispose();
+
+ 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)
{
@@ -948,21 +976,34 @@
"run", "Start testing double-buffered drawing");
Graphics canvas = getGraphics();
Image doublebuffer = createImage(size.width, size.height);
- g = doublebuffer.getGraphics();
- runSet_noClipping((Graphics2D) g, size);
- g.dispose();
- canvas.drawImage(doublebuffer, 0, 0, this);
- g = doublebuffer.getGraphics();
- runSet_withClipping((Graphics2D) g, size);
- g.dispose();
- canvas.drawImage(doublebuffer, 0, 0, this);
+ if (noClippingFlag)
+ {
+ g = doublebuffer.getGraphics();
+ runSet_noClipping((Graphics2D) g, size,
+ "double buffering", runCount);
+ g.dispose();
+ canvas.drawImage(doublebuffer, 0, 0, this);
+ }
- g = doublebuffer.getGraphics();
- runSet_zeroClipping((Graphics2D) g, size);
- g.dispose();
- canvas.drawImage(doublebuffer, 0, 0, this);
- canvas.dispose();
+ 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();
@@ -974,7 +1015,8 @@
}
else
{
- System.out.println("--- done --------------------------------------------------------");
+ // System.out.println("--- done
+ // --------------------------------------------------------");
synchronized (this)
{
doPaint = false;
@@ -988,10 +1030,16 @@
System.exit(129);
}
}
- System.exit(0);
+ testComplete();
}
- private void runSet_zeroClipping(Graphics2D g, Dimension size)
+ 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;
@@ -1024,12 +1072,21 @@
g.setColor(Color.BLACK);
g.fill(fullWindow);
- setTestContext("clipping to zero");
+ 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)
+ 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;
@@ -1060,18 +1117,30 @@
g.setColor(Color.BLACK);
g.fill(fullWindow);
- setTestContext("with clipping");
+ if (context.equals(""))
+ setTestContext("(" + runCount + ") with clipping ");
+ else
+ setTestContext("(" + runCount + ") with clipping (" + context + ")");
runTestSet(g, size);
}
- public void runSet_noClipping(Graphics2D g, Dimension 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)
{
Rectangle fullWindow = new Rectangle(0, 0, size.width, size.height);
g.setColor(Color.BLACK);
g.fill(fullWindow);
- setTestContext("without clipping");
+ if (context.equals(""))
+ setTestContext("(" + runCount + ") without clipping");
+ else
+ setTestContext("(" + runCount + ") without clipping (" + context + ")");
runTestSet(g, size);
}
Index: examples/gnu/classpath/examples/java2d/J2dBenchmarkGUI.java
===================================================================
RCS file: examples/gnu/classpath/examples/java2d/J2dBenchmarkGUI.java
diff -N examples/gnu/classpath/examples/java2d/J2dBenchmarkGUI.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ examples/gnu/classpath/examples/java2d/J2dBenchmarkGUI.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,652 @@
+/* J2dBenchmarkGUI.java --
+ Copyright (C) 2006 Free Software Foundation, Inc.
+
+ This file is part of GNU Classpath.
+
+ 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.
+
+ Linking this library statically or dynamically with other modules is
+ making a combined work based on this library. Thus, the terms and
+ conditions of the GNU General Public License cover the whole
+ combination.
+
+ As a special exception, the copyright holders of this library give you
+ permission to link this library with independent modules to produce an
+ executable, regardless of the license terms of these independent
+ modules, and to copy and distribute the resulting executable under
+ terms of your choice, provided that you also meet, for each linked
+ independent module, the terms and conditions of the license of that
+ module. An independent module is a module which is not derived from
+ or based on this library. If you modify this library, you may extend
+ this exception to your version of the library, but you are not
+ obligated to do so. If you do not wish to do so, delete this
+ exception statement from your version. */
+
+package gnu.classpath.examples.java2d;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Container;
+import java.awt.GridLayout;
+import java.awt.Insets;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JButton;
+import javax.swing.JCheckBox;
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+
+/**
+ * Extends the J2dBenchmark to provide a GUI for selecting options and tests.
+ */
+public class J2dBenchmarkGUI
+ implements ActionListener
+{
+
+ JLabel errorLabel;
+
+ JCheckBox noClipping;
+
+ JCheckBox withClipping;
+
+ JCheckBox zeroClipping;
+
+ JCheckBox singleBuffer;
+
+ JCheckBox doubleBuffer;
+
+ public J2dBenchmarkGUI()
+ {
+ super();
+ }
+
+ public static void main(String[] args)
+ {
+ new J2dBenchmarkGUI().run();
+ }
+
+ /**
+ * Sets up the initial GUI
+ */
+ public void run()
+ {
+ // Store all elements in a hashtable so that they can be passed into the
+ // harness easily.
+ Hashtable elements = new Hashtable();
+
+ // Set up frame
+ final JFrame frame = new JFrame("Java2D benchmark");
+ errorLabel = new JLabel(" ");
+
+ JPanel panel = new JPanel();
+ panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
+ Container content = frame.getContentPane();
+
+ // Display options for dimensions, iterations, test size, etc
+ JPanel options = new JPanel(new GridLayout(0, 2));
+
+ options.add(new JLabel("Height: "));
+ JTextField heightField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_SCREEN_HEIGHT));
+ heightField.setColumns(5);
+ options.add(heightField);
+ elements.put("height", heightField);
+
+ options.add(new JLabel("Width: "));
+ JTextField widthField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_SCREEN_WIDTH));
+ widthField.setColumns(5);
+ options.add(widthField);
+ elements.put("width", widthField);
+
+ options.add(new JLabel("Iterations: "));
+ JTextField iterField = new JTextField("1");
+ iterField.setColumns(5);
+ options.add(iterField);
+ elements.put("iterations", iterField);
+
+ options.add(new JLabel("Test size: "));
+ JTextField testSizeField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_TEST_SIZE));
+ testSizeField.setColumns(5);
+ options.add(testSizeField);
+ elements.put("size", testSizeField);
+
+ options.add(new JLabel("Test without clipping: "));
+ noClipping = new JCheckBox("", true);
+ noClipping.addActionListener(this);
+ options.add(noClipping);
+ elements.put("noclip", noClipping);
+
+ options.add(new JLabel("Test with clipping: "));
+ withClipping = new JCheckBox("", true);
+ withClipping.addActionListener(this);
+ options.add(withClipping);
+ elements.put("withclip", withClipping);
+
+ options.add(new JLabel("Test with clipping to zero: "));
+ zeroClipping = new JCheckBox("", true);
+ zeroClipping.addActionListener(this);
+ options.add(zeroClipping);
+ elements.put("zeroclip", zeroClipping);
+
+ options.add(new JLabel("Run single-buffer test: "));
+ singleBuffer = new JCheckBox("", true);
+ singleBuffer.addActionListener(this);
+ options.add(singleBuffer);
+ elements.put("singlebuffer", singleBuffer);
+
+ options.add(new JLabel("Run double-buffer test: "));
+ doubleBuffer = new JCheckBox("", true);
+ doubleBuffer.addActionListener(this);
+ options.add(doubleBuffer);
+ elements.put("doublebuffer", doubleBuffer);
+
+ // Allow user to select tests to run
+ JPanel tests = new JPanel();
+ tests.setLayout(new BoxLayout(tests, BoxLayout.PAGE_AXIS));
+
+ JCheckBox test_arcDraw = new JCheckBox("Arc", true);
+ tests.add(test_arcDraw);
+ elements.put("test_arcDraw", test_arcDraw);
+
+ JCheckBox test_ccurveDraw = new JCheckBox("Cubic Curve", true);
+ tests.add(test_ccurveDraw);
+ elements.put("test_ccurveDraw", test_ccurveDraw);
+
+ JCheckBox test_ellipseDraw = new JCheckBox("Ellipse", true);
+ tests.add(test_ellipseDraw);
+ elements.put("test_ellipseDraw", test_ellipseDraw);
+
+ /*
+ JCheckBox test_pathDraw = new JCheckBox("General Path", true);
+ tests.add(test_pathDraw);
+ elements.put("test_pathDraw", test_pathDraw);
+ */
+
+ JCheckBox test_lineDraw = new JCheckBox("Line", true);
+ tests.add(test_lineDraw);
+ elements.put("test_lineDraw", test_lineDraw);
+
+ JCheckBox test_qcurveDraw = new JCheckBox("Quadratic Curve", true);
+ tests.add(test_qcurveDraw);
+ elements.put("test_qcurveDraw", test_qcurveDraw);
+
+ JCheckBox test_rectDraw = new JCheckBox("Rectangle", true);
+ tests.add(test_rectDraw);
+ elements.put("test_rectDraw", test_rectDraw);
+
+ JCheckBox test_rrectDraw = new JCheckBox("Round Rectangle", true);
+ tests.add(test_rrectDraw);
+ elements.put("test_rrectDraw", test_rrectDraw);
+
+ JCheckBox test_image = new JCheckBox("Images", true);
+ tests.add(test_image);
+ elements.put("test_image", test_image);
+
+ // Final submit button
+ JPanel submit = new JPanel();
+ submit.setLayout(new BoxLayout(submit, BoxLayout.PAGE_AXIS));
+
+ JButton rectButton = new JButton("Run benchmark");
+ rectButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
+ submit.add(rectButton, BorderLayout.CENTER);
+
+ errorLabel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
+ errorLabel.setForeground(Color.RED);
+ submit.add(errorLabel);
+
+ rectButton.addActionListener(new Harness(elements, errorLabel));
+
+ // Lay it all out
+ JPanel body = new JPanel();
+ body.setLayout(new BoxLayout(body, BoxLayout.LINE_AXIS));
+ options.setAlignmentX(JComponent.LEFT_ALIGNMENT);
+ body.add(options);
+ body.add(Box.createHorizontalStrut(50));
+ tests.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
+ body.add(tests);
+
+ body.setAlignmentX(JComponent.CENTER_ALIGNMENT);
+ panel.add(body);
+ submit.setAlignmentX(JComponent.CENTER_ALIGNMENT);
+ panel.add(submit);
+
+ content.add(panel, BorderLayout.CENTER);
+
+ // Leave some breathing space in the frame
+ frame.pack();
+
+ frame.addWindowListener(new WindowAdapter()
+ {
+ public void windowClosing(WindowEvent e)
+ {
+ frame.setVisible(false);
+ System.exit(0);
+ }
+ });
+
+ frame.show();
+ }
+
+ /**
+ * Handles user events on the options GUI, ensuring that user input is valid
+ */
+ public void actionPerformed(ActionEvent ev)
+ {
+ if (! noClipping.isSelected() && ! withClipping.isSelected()
+ && ! zeroClipping.isSelected())
+ errorLabel.setText("You must select at least one clipping option");
+
+ else if (! singleBuffer.isSelected() && ! doubleBuffer.isSelected())
+ errorLabel.setText("You must select at least one buffering option");
+
+ else
+ errorLabel.setText(" ");
+ }
+
+ /**
+ * Parses GUI input and sets options in the benchmarker
+ */
+ private class Harness
+ implements ActionListener
+ {
+ Hashtable elements;
+
+ JLabel errorLabel;
+
+ /**
+ * Creates a new Harness object
+ *
+ * @param elements Hashtable containing the swing elements from the GUI
+ * @param errorLabel JLabel on which to display any error messages
+ */
+ public Harness(Hashtable elements, JLabel errorLabel)
+ {
+ super();
+
+ this.elements = elements;
+ this.errorLabel = errorLabel;
+ }
+
+ /**
+ * Handles user button-clicks, parsing the form, setting options, and
+ * starting the J2dBenchmark
+ *
+ * @param ae event that triggered this action
+ */
+ public void actionPerformed(ActionEvent ae)
+ {
+ try
+ {
+ // Create benchmarker object
+ final JFrame frame = new JFrame("Java2D benchmark");
+ J2dBenchmarkWrapper speed = new J2dBenchmarkWrapper(frame);
+
+ // Set options
+ speed.setDimensions(Integer.parseInt(((JTextField) elements.get("width")).getText()),
+ Integer.parseInt(((JTextField) elements.get("height")).getText()));
+
+ speed.setIterations(Integer.parseInt(((JTextField) elements.get("iterations")).getText()));
+ speed.setTestSize(Integer.parseInt(((JTextField) elements.get("size")).getText()));
+
+ speed.setClipping(((JCheckBox) elements.get("noclip")).isSelected(),
+ ((JCheckBox) elements.get("withclip")).isSelected(),
+ ((JCheckBox) elements.get("zeroclip")).isSelected());
+
+ speed.setBuffers(((JCheckBox) elements.get("singlebuffer")).isSelected(),
+ ((JCheckBox) elements.get("doublebuffer")).isSelected());
+
+ // Set tests
+ int testSuite = 0;
+ if (((JCheckBox) elements.get("test_arcDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_ARC;
+ if (((JCheckBox) elements.get("test_ccurveDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_CUBICCURVE;
+ if (((JCheckBox) elements.get("test_ellipseDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_ELLIPSE;
+ //if (((JCheckBox)elements.get("test_pathDraw")).isSelected())
+ // testSuite |= J2dBenchmarkWrapper.J2DTEST_GENERALPATH;
+ if (((JCheckBox) elements.get("test_lineDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_LINE;
+ if (((JCheckBox) elements.get("test_qcurveDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_QUADCURVE;
+ if (((JCheckBox) elements.get("test_rectDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_RECTANGLE;
+ if (((JCheckBox) elements.get("test_rrectDraw")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_ROUNDRECTANGLE;
+ if (((JCheckBox) elements.get("test_image")).isSelected())
+ testSuite |= J2dBenchmarkWrapper.J2DTEST_IMAGE;
+
+ if (testSuite != 0)
+ {
+ speed.setTests(testSuite);
+
+ // Create graphics.
+ frame.add(speed, BorderLayout.CENTER);
+ frame.setSize(Integer.parseInt(((JTextField) elements.get("width")).getText()),
+ Integer.parseInt(((JTextField) elements.get("height")).getText()));
+ frame.setVisible(true);
+
+ // Insets are correctly set only after the native peer was created.
+ Insets insets = frame.getInsets();
+ frame.setSize(frame.getWidth() + insets.right + insets.left,
+ frame.getHeight() + insets.top + insets.bottom);
+ }
+ else
+ {
+ errorLabel.setText("Please select at least one test.");
+ }
+ }
+ catch (NumberFormatException e)
+ {
+ errorLabel.setText("Please enter valid integers");
+ }
+ }
+ }
+
+ /**
+ * Wrapper for the J2dBenchmark, which outputs the results to a GUI
+ * instead of the command-line
+ */
+ private class J2dBenchmarkWrapper
+ extends J2dBenchmark
+ {
+ JFrame myFrame;
+
+ ResultsDisplay display;
+
+ /**
+ * Create new J2dBenchmarkWrapper object
+ *
+ * @param frame parent frame
+ */
+ public J2dBenchmarkWrapper(JFrame frame)
+ {
+ // Redirect log messages to the custom handler
+ logger.setUseParentHandlers(false);
+ display = new ResultsDisplay();
+ display.setLevel(Level.INFO);
+ logger.addHandler(display);
+
+ myFrame = frame;
+
+ super.init();
+ }
+
+ /**
+ * Set dimensions of benchmarking canvas
+ *
+ * @param width width of canvas
+ * @param height height of canvas
+ */
+ public void setDimensions(int width, int height)
+ {
+ screenHeight = height;
+ screenWidth = width;
+ setSize(width, height);
+ }
+
+ /**
+ * Set number of iterations
+ *
+ * @param it number of iterations
+ */
+ public void setIterations(int it)
+ {
+ iterations = it;
+ }
+
+ /**
+ * Set size of each test
+ *
+ * @param size size of test
+ */
+ public void setTestSize(int size)
+ {
+ testSize = size;
+ }
+
+ /**
+ * Set clipping options
+ *
+ * @param no run test with no clipping
+ * @param with run test with clipping
+ * @param zero run test with clipping to zero
+ */
+ public void setClipping(boolean no, boolean with, boolean zero)
+ {
+ this.noClippingFlag = no;
+ this.withClippingFlag = with;
+ this.zeroClippingFlag = zero;
+ }
+
+ /**
+ * Set buffering options
+ *
+ * @param single run test without double-buffering
+ * @param doubleb run test with double-buffering
+ */
+ public void setBuffers(boolean single, boolean doubleb)
+ {
+ this.singleBufferFlag = single;
+ this.doubleBufferFlag = doubleb;
+ }
+
+ /**
+ * Set tests to run
+ *
+ * @param tests bit-shifted list of tests (see J2dBenchmark constants)
+ */
+ public void setTests(int tests)
+ {
+ awtTests = tests;
+ }
+
+ /**
+ * Saves test report after each iteration
+ */
+ void printReport()
+ {
+ // Report test results to the GUI display
+ ArrayList results = new ArrayList();
+ for (Iterator i = testSetMap.testIterator(); i.hasNext();)
+ {
+ TestRecorder recorder = testSetMap.getTest((String) i.next());
+
+ results.add("TEST " + recorder.getTestName() + ": average "
+ + recorder.getAverage() + "ms [" + recorder.getMinTime()
+ + "-" + recorder.getMaxTime() + "]");
+ }
+
+ display.report(results);
+ }
+
+ /**
+ * Disables current frame and displays test results
+ */
+ void testComplete()
+ {
+ // Clear benchmarking canvas and display results instead
+ myFrame.setVisible(false);
+
+ display.show();
+ }
+ }
+
+ /**
+ * GUI to display results of benchmarking
+ */
+ private class ResultsDisplay
+ extends Handler
+ implements ActionListener
+ {
+ /**
+ * Allow user to select results from each iteration
+ */
+ JComboBox iterations;
+
+ /**
+ * Area to print results in
+ */
+ JTextArea results;
+
+ /**
+ * Allow user to view summary or full details of test report
+ */
+ JCheckBox details;
+
+ /**
+ * Store all test results
+ */
+ ArrayList testResults;
+
+ /**
+ * Store all test details
+ */
+ ArrayList testDetails;
+
+ /**
+ * Initialize variables
+ */
+ public ResultsDisplay()
+ {
+ testResults = new ArrayList();
+ testDetails = new ArrayList();
+ testDetails.add(new ArrayList());
+ }
+
+ /**
+ * Parse all results and display on a GUI
+ */
+ public void show()
+ {
+ // Set up panel
+ JFrame frame = new JFrame("Java2D benchmark results");
+ Container cp = frame.getContentPane();
+
+ // Non-editable text area for the results
+ results = new JTextArea();
+ results.setEditable(false);
+ results.setRows(15);
+ results.setColumns(60);
+
+ // Checkbox to optionally display details (ie log messages)
+ details = new JCheckBox("Details", false);
+ details.addActionListener(this);
+
+ // Combo box to allow selection of iteration number
+ iterations = new JComboBox();
+ iterations.addActionListener(this);
+ for (int i = 0; i < testResults.size(); i++)
+ iterations.addItem("Iteration #" + (i + 1));
+
+ // Lay it all out
+ JPanel topleft = new JPanel();
+ topleft.add(new JLabel("View results from: "));
+ topleft.add(iterations);
+ topleft.setAlignmentX(JComponent.LEFT_ALIGNMENT);
+ details.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
+ JPanel top = new JPanel();
+ top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
+ top.add(topleft);
+ top.add(details);
+
+ cp.add(top, BorderLayout.NORTH);
+ cp.add(new JScrollPane(results), BorderLayout.SOUTH);
+
+ frame.pack();
+ frame.show();
+ }
+
+ /**
+ * This overrides the logger publish method, which accepts log messages and
+ * saves them for later display
+ *
+ * @param record information about the log event
+ */
+ public void publish(LogRecord record)
+ {
+ ((ArrayList) testDetails.get(testDetails.size() - 1)).add(record.getMessage());
+ }
+
+ /**
+ * Accepts a test summary report, generated after each iteration of a test
+ *
+ * @param results test results
+ */
+ public void report(ArrayList results)
+ {
+ testResults.add(results);
+ testDetails.add(new ArrayList());
+ }
+
+ /**
+ * Provided as part of the Handler interface; not used
+ */
+ public void flush()
+ {
+ }
+
+ /**
+ * Provided as part of the Handler interface; not used
+ */
+ public void close()
+ {
+ }
+
+ /**
+ * Handle user-generated events on the results GUI
+ */
+ public void actionPerformed(ActionEvent ev)
+ {
+ // Display information about the requested iteration
+ int iteration = iterations.getSelectedIndex();
+ String message = "";
+
+ // Display summary or details, as requested
+ Iterator it;
+ if (details.isSelected())
+ it = ((ArrayList) testDetails.get(iteration)).iterator();
+ else
+ it = ((ArrayList) testResults.get(iteration)).iterator();
+
+ // Parse the ArrayList's
+ while (it.hasNext())
+ {
+ message = message + ((String) it.next() + "\n");
+ }
+
+ // Output to screen
+ results.setText(message);
+ }
+ }
+
+}