OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / java2d / J2dBenchmarkGUI.java
1 /* J2dBenchmarkGUI.java -- GUI for java2d benchmarker
2  Copyright (C) 2006  Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
6  GNU Classpath is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37
38 package gnu.classpath.examples.java2d;
39
40 import java.awt.BorderLayout;
41 import java.awt.Color;
42 import java.awt.Container;
43 import java.awt.GridBagConstraints;
44 import java.awt.GridBagLayout;
45 import java.awt.GridLayout;
46 import java.awt.Insets;
47 import java.awt.event.ActionEvent;
48 import java.awt.event.ActionListener;
49 import java.awt.event.WindowAdapter;
50 import java.awt.event.WindowEvent;
51 import java.util.ArrayList;
52 import java.util.Hashtable;
53 import java.util.Iterator;
54 import java.util.logging.Handler;
55 import java.util.logging.Level;
56 import java.util.logging.LogRecord;
57
58 import javax.swing.Box;
59 import javax.swing.BoxLayout;
60 import javax.swing.ButtonGroup;
61 import javax.swing.JButton;
62 import javax.swing.JCheckBox;
63 import javax.swing.JComboBox;
64 import javax.swing.JComponent;
65 import javax.swing.JFrame;
66 import javax.swing.JLabel;
67 import javax.swing.JPanel;
68 import javax.swing.JRadioButton;
69 import javax.swing.JScrollPane;
70 import javax.swing.JTextArea;
71 import javax.swing.JTextField;
72 import javax.swing.border.BevelBorder;
73
74 /**
75  * Extends the J2dBenchmark to provide a GUI for selecting options and tests.
76  */
77 public class J2dBenchmarkGUI
78     implements ActionListener
79 {
80
81   JLabel errorLabel;
82
83   JCheckBox noClipping;
84
85   JCheckBox withClipping;
86
87   JCheckBox zeroClipping;
88
89   JCheckBox singleBuffer;
90
91   JCheckBox doubleBuffer;
92
93   public J2dBenchmarkGUI()
94   {
95     super();
96   }
97
98   public static void main(String[] args)
99   {
100     new J2dBenchmarkGUI().run();
101   }
102
103   /**
104    * Sets up the initial GUI
105    */
106   public void run()
107   {
108     // Store all elements in a hashtable so that they can be passed into the
109     // harness easily.
110     Hashtable elements = new Hashtable();
111
112     // Set up frame
113     final JFrame frame = new JFrame("Java2D benchmark");
114     errorLabel = new JLabel(" ");
115
116     JPanel panel = new JPanel();
117     panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
118     Container content = frame.getContentPane();
119
120     // Display options for dimensions, iterations, test size, etc
121     JPanel options = new JPanel(new GridLayout(0, 2));
122
123     options.add(new JLabel("Height: "));
124     JTextField heightField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_SCREEN_HEIGHT));
125     heightField.setColumns(5);
126     options.add(heightField);
127     elements.put("height", heightField);
128
129     options.add(new JLabel("Width: "));
130     JTextField widthField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_SCREEN_WIDTH));
131     widthField.setColumns(5);
132     options.add(widthField);
133     elements.put("width", widthField);
134
135     options.add(new JLabel("Iterations: "));
136     JTextField iterField = new JTextField("1");
137     iterField.setColumns(5);
138     options.add(iterField);
139     elements.put("iterations", iterField);
140
141     options.add(new JLabel("Test size: "));
142     JTextField testSizeField = new JTextField(Integer.toString(J2dBenchmark.DEFAULT_TEST_SIZE));
143     testSizeField.setColumns(5);
144     options.add(testSizeField);
145     elements.put("size", testSizeField);
146
147     options.add(new JLabel("Test without clipping: "));
148     noClipping = new JCheckBox("", true);
149     noClipping.addActionListener(this);
150     options.add(noClipping);
151     elements.put("noclip", noClipping);
152
153     options.add(new JLabel("Test with clipping: "));
154     withClipping = new JCheckBox("", true);
155     withClipping.addActionListener(this);
156     options.add(withClipping);
157     elements.put("withclip", withClipping);
158
159     options.add(new JLabel("Test with clipping to zero: "));
160     zeroClipping = new JCheckBox("", true);
161     zeroClipping.addActionListener(this);
162     options.add(zeroClipping);
163     elements.put("zeroclip", zeroClipping);
164
165     options.add(new JLabel("Run single-buffer test: "));
166     singleBuffer = new JCheckBox("", true);
167     singleBuffer.addActionListener(this);
168     options.add(singleBuffer);
169     elements.put("singlebuffer", singleBuffer);
170
171     options.add(new JLabel("Run double-buffer test: "));
172     doubleBuffer = new JCheckBox("", true);
173     doubleBuffer.addActionListener(this);
174     options.add(doubleBuffer);
175     elements.put("doublebuffer", doubleBuffer);
176
177     // Allow user to select tests to run
178     JPanel tests = new JPanel();
179     tests.setLayout(new BoxLayout(tests, BoxLayout.PAGE_AXIS));
180     tests.setBorder(new BevelBorder(BevelBorder.RAISED));
181     tests.add(new JLabel("Shapes to test:"));
182
183     JCheckBox test_arcDraw = new JCheckBox("Arc", true);
184     tests.add(test_arcDraw);
185     elements.put("test_arcDraw", test_arcDraw);
186
187     JCheckBox test_ccurveDraw = new JCheckBox("Cubic Curve", true);
188     tests.add(test_ccurveDraw);
189     elements.put("test_ccurveDraw", test_ccurveDraw);
190
191     JCheckBox test_ellipseDraw = new JCheckBox("Ellipse", true);
192     tests.add(test_ellipseDraw);
193     elements.put("test_ellipseDraw", test_ellipseDraw);
194
195     /*
196      JCheckBox test_pathDraw = new JCheckBox("General Path", true);
197      tests.add(test_pathDraw);
198      elements.put("test_pathDraw", test_pathDraw);
199      */
200
201     JCheckBox test_lineDraw = new JCheckBox("Line", true);
202     tests.add(test_lineDraw);
203     elements.put("test_lineDraw", test_lineDraw);
204
205     JCheckBox test_qcurveDraw = new JCheckBox("Quadratic Curve", true);
206     tests.add(test_qcurveDraw);
207     elements.put("test_qcurveDraw", test_qcurveDraw);
208
209     JCheckBox test_rectDraw = new JCheckBox("Rectangle", true);
210     tests.add(test_rectDraw);
211     elements.put("test_rectDraw", test_rectDraw);
212
213     JCheckBox test_rrectDraw = new JCheckBox("Round Rectangle", true);
214     tests.add(test_rrectDraw);
215     elements.put("test_rrectDraw", test_rrectDraw);
216
217     JCheckBox test_image = new JCheckBox("Images", true);
218     tests.add(test_image);
219     elements.put("test_image", test_image);
220
221     // Additional image-processing options
222     JPanel extras = new JPanel();
223     extras.setBorder(new BevelBorder(BevelBorder.LOWERED));
224     GridBagLayout layout = new GridBagLayout();
225     GridBagConstraints gbc = new GridBagConstraints();
226     gbc.anchor = GridBagConstraints.NORTHWEST;
227     gbc.insets = new Insets(5, 2, 15, 15);
228     extras.setLayout(layout);
229
230     // Filling (solid, gradient, or texture)
231     JPanel opt_Fill = new JPanel();
232     opt_Fill.setLayout(new BoxLayout(opt_Fill, BoxLayout.PAGE_AXIS));
233     JLabel opt_FillLabel = new JLabel("Filling:");
234     opt_FillLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
235     opt_Fill.add(opt_FillLabel);
236
237     ButtonGroup opt_FillGroup = new ButtonGroup();
238     JRadioButton opt_FillSolid = new JRadioButton("Solid colour", true);
239     opt_FillSolid.setActionCommand("solid");
240     opt_Fill.add(opt_FillSolid);
241     opt_FillGroup.add(opt_FillSolid);
242     JRadioButton opt_FillGradient = new JRadioButton("Gradient", false);
243     opt_FillGradient.setActionCommand("gradient");
244     opt_Fill.add(opt_FillGradient);
245     opt_FillGroup.add(opt_FillGradient);
246     JRadioButton opt_FillTexture = new JRadioButton("Texture", false);
247     opt_FillTexture.setActionCommand("texture");
248     opt_Fill.add(opt_FillTexture);
249     opt_FillGroup.add(opt_FillTexture);
250     JTextField opt_FillTextureFile = new JTextField("texture file");
251     opt_FillTextureFile.setAlignmentX(JComponent.LEFT_ALIGNMENT);
252     opt_Fill.add(opt_FillTextureFile);
253     elements.put("opt_FillGroup", opt_FillGroup);
254     elements.put("opt_FillTextureFile", opt_FillTextureFile);
255     layout.setConstraints(opt_Fill, gbc);
256     extras.add(opt_Fill);
257
258     // Stroke
259     JPanel opt_Stroke = new JPanel();
260     opt_Stroke.setLayout(new BoxLayout(opt_Stroke, BoxLayout.PAGE_AXIS));
261     JLabel opt_StrokeLabel = new JLabel("Stroke:");
262     opt_StrokeLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
263     opt_Stroke.add(opt_StrokeLabel);
264     JCheckBox opt_StrokeRandom = new JCheckBox("random", false);
265     elements.put("opt_StrokeRandom", opt_StrokeRandom);
266     opt_Stroke.add(opt_StrokeRandom);
267     gbc.gridwidth = GridBagConstraints.REMAINDER;
268     layout.setConstraints(opt_Stroke, gbc);
269     extras.add(opt_Stroke);
270
271     // Anti-Alias
272     JPanel opt_Alias = new JPanel();
273     opt_Alias.setLayout(new BoxLayout(opt_Alias, BoxLayout.PAGE_AXIS));
274     JLabel opt_AliasLabel = new JLabel("Anti-Aliasing:");
275     opt_AliasLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
276     opt_Alias.add(opt_AliasLabel);
277     JCheckBox opt_AliasOn = new JCheckBox("on", false);
278     elements.put("opt_AliasOn", opt_AliasOn);
279     opt_Alias.add(opt_AliasOn);
280     gbc.gridwidth = 1;
281     layout.setConstraints(opt_Alias, gbc);
282     extras.add(opt_Alias);
283
284     // Alpha composite
285     JPanel opt_Composite = new JPanel();
286     opt_Composite.setLayout(new BoxLayout(opt_Composite, BoxLayout.PAGE_AXIS));
287     JLabel opt_CompositeLabel = new JLabel("Alpha Composite:");
288     opt_CompositeLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
289     opt_Composite.add(opt_CompositeLabel);
290     JTextField opt_CompositeValue = new JTextField("1.0");
291     opt_CompositeValue.setAlignmentX(JComponent.LEFT_ALIGNMENT);
292     elements.put("opt_CompositeValue", opt_CompositeValue);
293     opt_Composite.add(opt_CompositeValue);
294     gbc.gridwidth = GridBagConstraints.REMAINDER;
295     layout.setConstraints(opt_Composite, gbc);
296     extras.add(opt_Composite);
297
298     // Transformations
299     // TODO: allow user-defined matrices for AffineTransform
300     // (backend already has hooks for it, need to create gui)
301     JLabel opt_TransformLabel = new JLabel("Transformations:");
302     opt_TransformLabel.setBorder(new BevelBorder(BevelBorder.RAISED));
303     gbc.insets = new Insets(5, 2, 0, 15);
304     layout.setConstraints(opt_TransformLabel, gbc);
305     extras.add(opt_TransformLabel);
306
307     JPanel opt_Transform_Translate = new JPanel(new GridLayout(0, 2, 5, 5));
308     opt_Transform_Translate.add(new JLabel("x-axis translation "));
309     JTextField opt_TransformTranslateX = new JTextField("0");
310     opt_TransformTranslateX.setAlignmentX(JComponent.LEFT_ALIGNMENT);
311     opt_Transform_Translate.add(opt_TransformTranslateX);
312     elements.put("opt_TransformTranslateX", opt_TransformTranslateX);
313     opt_Transform_Translate.add(new JLabel("y-axis translation "));
314     JTextField opt_TransformTranslateY = new JTextField("0");
315     opt_TransformTranslateY.setAlignmentX(JComponent.LEFT_ALIGNMENT);
316     opt_Transform_Translate.add(opt_TransformTranslateY);
317     elements.put("opt_TransformTranslateY", opt_TransformTranslateY);
318     gbc.gridwidth = 1;
319     gbc.insets = new Insets(0, 2, 5, 15);
320     layout.setConstraints(opt_Transform_Translate, gbc);
321     extras.add(opt_Transform_Translate);
322
323     JPanel opt_Transform_Shear = new JPanel(new GridLayout(0, 2, 5, 5));
324     opt_Transform_Shear.add(new JLabel("x-axis shear "));
325     JTextField opt_TransformShearX = new JTextField("0");
326     opt_TransformShearX.setAlignmentX(JComponent.LEFT_ALIGNMENT);
327     opt_Transform_Shear.add(opt_TransformShearX);
328     elements.put("opt_TransformShearX", opt_TransformShearX);
329     opt_Transform_Shear.add(new JLabel("y-axis shear "));
330     JTextField opt_TransformShearY = new JTextField("0");
331     opt_Transform_Shear.add(opt_TransformShearY);
332     elements.put("opt_TransformShearY", opt_TransformShearY);
333     gbc.gridwidth = GridBagConstraints.REMAINDER;
334     layout.setConstraints(opt_Transform_Shear, gbc);
335     extras.add(opt_Transform_Shear);
336
337     JPanel opt_Transform_Rotate = new JPanel(new GridLayout(0, 2, 5, 5));
338     opt_Transform_Rotate.add(new JLabel("rotation (radians) "));
339     JTextField opt_TransformRotate = new JTextField("0");
340     opt_Transform_Rotate.add(opt_TransformRotate);
341     elements.put("opt_TransformRotate", opt_TransformRotate);
342     layout.setConstraints(opt_Transform_Rotate, gbc);
343     extras.add(opt_Transform_Rotate);
344
345     // Final submit button
346     JPanel submit = new JPanel();
347     submit.setLayout(new BoxLayout(submit, BoxLayout.PAGE_AXIS));
348
349     JButton rectButton = new JButton("Run benchmark");
350     rectButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
351     submit.add(rectButton, BorderLayout.CENTER);
352
353     errorLabel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
354     errorLabel.setForeground(Color.RED);
355     submit.add(errorLabel);
356
357     rectButton.addActionListener(new Harness(elements, errorLabel));
358
359     // Lay it all out
360     JPanel body = new JPanel();
361     body.setLayout(new BoxLayout(body, BoxLayout.LINE_AXIS));
362     options.setAlignmentX(JComponent.LEFT_ALIGNMENT);
363     body.add(options);
364     body.add(Box.createHorizontalStrut(50));
365     tests.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
366     body.add(tests);
367
368     body.setAlignmentX(JComponent.CENTER_ALIGNMENT);
369     panel.add(body);
370     extras.setAlignmentX(JComponent.CENTER_ALIGNMENT);
371     panel.add(extras);
372     submit.setAlignmentX(JComponent.CENTER_ALIGNMENT);
373     panel.add(submit);
374
375     content.add(panel, BorderLayout.CENTER);
376
377     // Leave some breathing space in the frame
378     frame.pack();
379
380     frame.addWindowListener(new WindowAdapter()
381     {
382       public void windowClosing(WindowEvent e)
383       {
384         frame.setVisible(false);
385         System.exit(0);
386       }
387     });
388
389     frame.show();
390   }
391
392   /**
393    * Handles user events on the options GUI, ensuring that user input is valid
394    */
395   public void actionPerformed(ActionEvent ev)
396   {
397     if (! noClipping.isSelected() && ! withClipping.isSelected()
398         && ! zeroClipping.isSelected())
399       errorLabel.setText("You must select at least one clipping option");
400
401     else if (! singleBuffer.isSelected() && ! doubleBuffer.isSelected())
402       errorLabel.setText("You must select at least one buffering option");
403
404     else
405       errorLabel.setText(" ");
406   }
407
408   /**
409    * Parses GUI input and sets options in the benchmarker 
410    */
411   private class Harness
412       implements ActionListener
413   {
414     Hashtable elements;
415
416     JLabel errorLabel;
417
418     /**
419      * Creates a new Harness object
420      * 
421      * @param elements Hashtable containing the swing elements from the GUI
422      * @param errorLabel JLabel on which to display any error messages
423      */
424     public Harness(Hashtable elements, JLabel errorLabel)
425     {
426       super();
427
428       this.elements = elements;
429       this.errorLabel = errorLabel;
430     }
431
432     /**
433      * Handles user button-clicks, parsing the form, setting options, and
434      * starting the J2dBenchmark
435      * 
436      * @param ae event that triggered this action
437      */
438     public void actionPerformed(ActionEvent ae)
439     {
440       try
441         {
442           // Create benchmarker object
443           final JFrame frame = new JFrame("Java2D benchmark");
444           J2dBenchmarkWrapper speed = new J2dBenchmarkWrapper(frame);
445
446           // Set options
447           speed.setDimensions(Integer.parseInt(((JTextField) elements.get("width")).getText()),
448                               Integer.parseInt(((JTextField) elements.get("height")).getText()));
449
450           speed.setIterations(Integer.parseInt(((JTextField) elements.get("iterations")).getText()));
451           speed.setTestSize(Integer.parseInt(((JTextField) elements.get("size")).getText()));
452
453           speed.setClipping(((JCheckBox) elements.get("noclip")).isSelected(),
454                             ((JCheckBox) elements.get("withclip")).isSelected(),
455                             ((JCheckBox) elements.get("zeroclip")).isSelected());
456
457           speed.setBuffers(((JCheckBox) elements.get("singlebuffer")).isSelected(),
458                            ((JCheckBox) elements.get("doublebuffer")).isSelected());
459
460           // Set additional processing options
461           speed.setFill(((ButtonGroup) elements.get("opt_FillGroup")).getSelection().getActionCommand(),
462                         ((JTextField) elements.get("opt_FillTextureFile")).getText());
463
464           speed.setStroke(((JCheckBox) elements.get("opt_StrokeRandom")).isSelected());
465
466           speed.setAlias(((JCheckBox) elements.get("opt_AliasOn")).isSelected());
467
468           speed.setComposite(Float.parseFloat(((JTextField) elements.get("opt_CompositeValue")).getText()));
469
470           speed.setTranslation(Integer.parseInt(((JTextField) elements.get("opt_TransformTranslateX")).getText()),
471                                Integer.parseInt(((JTextField) elements.get("opt_TransformTranslateY")).getText()));
472
473           speed.setRotation(Double.parseDouble(((JTextField) elements.get("opt_TransformRotate")).getText()));
474
475           speed.setShear(Double.parseDouble(((JTextField) elements.get("opt_TransformShearX")).getText()),
476                          Double.parseDouble(((JTextField) elements.get("opt_TransformShearY")).getText()));
477
478           // Set tests
479           int testSuite = 0;
480           if (((JCheckBox) elements.get("test_arcDraw")).isSelected())
481             testSuite |= J2dBenchmarkWrapper.J2DTEST_ARC;
482           if (((JCheckBox) elements.get("test_ccurveDraw")).isSelected())
483             testSuite |= J2dBenchmarkWrapper.J2DTEST_CUBICCURVE;
484           if (((JCheckBox) elements.get("test_ellipseDraw")).isSelected())
485             testSuite |= J2dBenchmarkWrapper.J2DTEST_ELLIPSE;
486           //if (((JCheckBox)elements.get("test_pathDraw")).isSelected())
487           //  testSuite |= J2dBenchmarkWrapper.J2DTEST_GENERALPATH; 
488           if (((JCheckBox) elements.get("test_lineDraw")).isSelected())
489             testSuite |= J2dBenchmarkWrapper.J2DTEST_LINE;
490           if (((JCheckBox) elements.get("test_qcurveDraw")).isSelected())
491             testSuite |= J2dBenchmarkWrapper.J2DTEST_QUADCURVE;
492           if (((JCheckBox) elements.get("test_rectDraw")).isSelected())
493             testSuite |= J2dBenchmarkWrapper.J2DTEST_RECTANGLE;
494           if (((JCheckBox) elements.get("test_rrectDraw")).isSelected())
495             testSuite |= J2dBenchmarkWrapper.J2DTEST_ROUNDRECTANGLE;
496           if (((JCheckBox) elements.get("test_image")).isSelected())
497             testSuite |= J2dBenchmarkWrapper.J2DTEST_IMAGE;
498
499           if (testSuite != 0)
500             {
501               speed.setTests(testSuite);
502
503               String initResult = speed.init();
504
505               if (initResult == null)
506                 {
507                   // Create graphics.
508                   frame.add(speed, BorderLayout.CENTER);
509                   frame.setSize(
510                                 Integer.parseInt(((JTextField) elements.get("width")).getText()),
511                                 Integer.parseInt(((JTextField) elements.get("height")).getText()));
512                   frame.setVisible(true);
513
514                   // Insets are correctly set only after the native peer was
515                   // created.
516                   Insets insets = frame.getInsets();
517                   frame.setSize(frame.getWidth() + insets.right + insets.left,
518                                 frame.getHeight() + insets.top + insets.bottom);
519
520                   // Clear any old error messages
521                   errorLabel.setText(" ");
522                 }
523               else
524                 errorLabel.setText(initResult);
525             }
526           else
527             errorLabel.setText("Please select at least one test.");
528         }
529       catch (NumberFormatException e)
530         {
531           errorLabel.setText("Please enter valid integers");
532         }
533     }
534   }
535
536   /**
537    * Wrapper for the J2dBenchmark, which outputs the results to a GUI
538    * instead of the command-line
539    */
540   private class J2dBenchmarkWrapper
541       extends J2dBenchmark
542   {
543     JFrame myFrame;
544
545     ResultsDisplay display;
546
547     /**
548      * Create new J2dBenchmarkWrapper object
549      * 
550      * @param frame parent frame
551      */
552     public J2dBenchmarkWrapper(JFrame frame)
553     {
554       // Redirect log messages to the custom handler
555       logger.setUseParentHandlers(false);
556       display = new ResultsDisplay();
557       display.setLevel(Level.INFO);
558       logger.addHandler(display);
559
560       myFrame = frame;
561     }
562
563     /**
564      * Set dimensions of benchmarking canvas
565      * 
566      * @param width width of canvas
567      * @param height height of canvas
568      */
569     public void setDimensions(int width, int height)
570     {
571       screenHeight = height;
572       screenWidth = width;
573       setSize(width, height);
574     }
575
576     /**
577      * Set number of iterations
578      * 
579      * @param it number of iterations
580      */
581     public void setIterations(int it)
582     {
583       iterations = it;
584     }
585
586     /**
587      * Set size of each test
588      * 
589      * @param size size of test
590      */
591     public void setTestSize(int size)
592     {
593       testSize = size;
594     }
595
596     /**
597      * Set clipping options
598      * 
599      * @param no run test with no clipping
600      * @param with run test with clipping
601      * @param zero run test with clipping to zero
602      */
603     public void setClipping(boolean no, boolean with, boolean zero)
604     {
605       this.noClippingFlag = no;
606       this.withClippingFlag = with;
607       this.zeroClippingFlag = zero;
608     }
609
610     /**
611      * Set buffering options
612      * 
613      * @param single run test without double-buffering
614      * @param doubleb run test with double-buffering
615      */
616     public void setBuffers(boolean single, boolean doubleb)
617     {
618       this.singleBufferFlag = single;
619       this.doubleBufferFlag = doubleb;
620     }
621
622     /**
623      * Set fill options
624      * 
625      * @param type fill type: "solid", "gradient", or "texture"
626      * @param file filename to use if texturing
627      */
628     public void setFill(String type, String file)
629     {
630       if (type.equals("gradient"))
631         this.gradientFlag = true;
632       else if (type.equals("texture"))
633         {
634           this.texture = file;
635         }
636     }
637
638     /**
639      * Set stroke options
640      * 
641      * @param stroke boolean flag to use random stroking or not
642      */
643     public void setStroke(boolean stroke)
644     {
645       this.strokeFlag = stroke;
646     }
647
648     /**
649      * Set anti-aliasing options
650      * 
651      * @param alias boolean flag to use anti-aliasing or not
652      */
653     public void setAlias(boolean alias)
654     {
655       this.antialiasFlag = alias;
656     }
657
658     /**
659      * Set alpha composite
660      * 
661      * @param alpha alpha composite
662      */
663     public void setComposite(float alpha)
664     {
665       this.composite = alpha;
666     }
667
668     /**
669      * Set translation values
670      * 
671      * @param x x-axis translation
672      * @param y y-axis translation
673      */
674     public void setTranslation(int x, int y)
675     {
676       this.xtranslate = x;
677       this.ytranslate = y;
678     }
679
680     /**
681      * Set rotation
682      * 
683      * @param theta angle to rotate by (radians)
684      */
685     public void setRotation(double theta)
686     {
687       this.rotate = theta;
688     }
689
690     /**
691      * Set shear values
692      * 
693      * @param x x-axis shear value
694      * @param y-axis shear value
695      */
696     public void setShear(double x, double y)
697     {
698       this.xshear = x;
699       this.yshear = y;
700     }
701
702     /**
703      * Set tests to run
704      * 
705      * @param tests bit-shifted list of tests (see J2dBenchmark constants)
706      */
707     public void setTests(int tests)
708     {
709       awtTests = tests;
710     }
711
712     /**
713      * Saves test report after each iteration
714      */
715     void printReport()
716     {
717       // Report test results to the GUI display
718       ArrayList results = new ArrayList();
719       for (Iterator i = testSetMap.testIterator(); i.hasNext();)
720         {
721           TestRecorder recorder = testSetMap.getTest((String) i.next());
722
723           results.add("TEST " + recorder.getTestName() + ": average "
724                       + recorder.getAverage() + "ms [" + recorder.getMinTime()
725                       + "-" + recorder.getMaxTime() + "]");
726         }
727
728       display.report(results);
729     }
730
731     /**
732      * Disables current frame and displays test results
733      */
734     void testComplete()
735     {
736       // Clear benchmarking canvas and display results instead
737       myFrame.setVisible(false);
738
739       display.show();
740     }
741   }
742
743   /**
744    * GUI to display results of benchmarking
745    */
746   private class ResultsDisplay
747       extends Handler
748       implements ActionListener
749   {
750     /**
751      * Allow user to select results from each iteration
752      */
753     JComboBox iterations;
754
755     /**
756      * Area to print results in
757      */
758     JTextArea results;
759
760     /**
761      * Allow user to view summary or full details of test report
762      */
763     JCheckBox details;
764
765     /**
766      * Store all test results
767      */
768     ArrayList testResults;
769
770     /**
771      * Store all test details
772      */
773     ArrayList testDetails;
774
775     /**
776      * Initialize variables
777      */
778     public ResultsDisplay()
779     {
780       testResults = new ArrayList();
781       testDetails = new ArrayList();
782       testDetails.add(new ArrayList());
783     }
784
785     /**
786      * Parse all results and display on a GUI
787      */
788     public void show()
789     {
790       // Set up panel
791       JFrame frame = new JFrame("Java2D benchmark results");
792       Container cp = frame.getContentPane();
793
794       // Non-editable text area for the results
795       results = new JTextArea();
796       results.setEditable(false);
797       results.setRows(15);
798       results.setColumns(60);
799
800       // Checkbox to optionally display details (ie log messages)
801       details = new JCheckBox("Details", false);
802       details.addActionListener(this);
803
804       // Combo box to allow selection of iteration number
805       iterations = new JComboBox();
806       iterations.addActionListener(this);
807       for (int i = 0; i < testResults.size(); i++)
808         iterations.addItem("Iteration #" + (i + 1));
809
810       // Lay it all out
811       JPanel topleft = new JPanel();
812       topleft.add(new JLabel("View results from: "));
813       topleft.add(iterations);
814       topleft.setAlignmentX(JComponent.LEFT_ALIGNMENT);
815       details.setAlignmentX(JComponent.RIGHT_ALIGNMENT);
816       JPanel top = new JPanel();
817       top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS));
818       top.add(topleft);
819       top.add(details);
820
821       cp.add(top, BorderLayout.NORTH);
822       cp.add(new JScrollPane(results), BorderLayout.SOUTH);
823
824       frame.pack();
825       frame.show();
826     }
827
828     /**
829      * This overrides the logger publish method, which accepts log messages and
830      * saves them for later display
831      * 
832      * @param record information about the log event
833      */
834     public void publish(LogRecord record)
835     {
836       ((ArrayList) testDetails.get(testDetails.size() - 1)).add(record.getMessage());
837     }
838
839     /**
840      * Accepts a test summary report, generated after each iteration of a test
841      * 
842      * @param results test results
843      */
844     public void report(ArrayList results)
845     {
846       testResults.add(results);
847       testDetails.add(new ArrayList());
848     }
849
850     /**
851      * Provided as part of the Handler interface; not used
852      */
853     public void flush()
854     {
855     }
856
857     /**
858      * Provided as part of the Handler interface; not used
859      */
860     public void close()
861     {
862     }
863
864     /**
865      * Handle user-generated events on the results GUI
866      */
867     public void actionPerformed(ActionEvent ev)
868     {
869       // Display information about the requested iteration
870       int iteration = iterations.getSelectedIndex();
871       String message = "";
872
873       // Display summary or details, as requested
874       Iterator it;
875       if (details.isSelected())
876         it = ((ArrayList) testDetails.get(iteration)).iterator();
877       else
878         it = ((ArrayList) testResults.get(iteration)).iterator();
879
880       // Parse the ArrayList's
881       while (it.hasNext())
882         {
883           message = message + ((String) it.next() + "\n");
884         }
885
886       // Output to screen
887       results.setText(message);
888     }
889   }
890
891 }