View Javadoc

1   // COpyright 2000-2005, FreeHEP.
2   package org.freehep.graphicsio.test;
3   
4   import java.awt.Color;
5   import java.awt.Dimension;
6   import java.awt.Graphics;
7   import java.awt.Insets;
8   
9   import javax.swing.JPanel;
10  
11  import org.freehep.graphics2d.VectorGraphics;
12  import org.freehep.graphics2d.VectorGraphicsConstants;
13  
14  /**
15   * @author Charles Loomis
16   * @author Mark Donszelmann
17   * @version $Id: TestSymbolPerformance.java 8584 2006-08-10 23:06:37Z duns $
18   */
19  public class TestSymbolPerformance extends JPanel {
20  
21      public TestSymbolPerformance() {
22          setOpaque(false);
23      }
24  
25      public void paintComponent(Graphics g) {
26  
27          VectorGraphics vg = VectorGraphics.create(g);
28  
29          Dimension dim = getSize();
30          Insets insets = getInsets();
31  
32          vg.setColor(Color.RED);
33          vg.fillRect(insets.left, insets.top, dim.width - insets.left
34                  - insets.right, dim.height - insets.top - insets.bottom);
35  
36          vg.setColor(Color.BLACK);
37          writeSymbols(10000, vg, 0, dim.height / 2, dim.width,
38                  VectorGraphicsConstants.SYMBOL_STAR, 4);
39          writeSymbols(10000, vg, dim.height / 2, dim.height / 2, dim.width,
40                  VectorGraphicsConstants.SYMBOL_CIRCLE, 256);
41      }
42  
43      private void writeSymbols(int n, VectorGraphics vg, double yo,
44              double height, double width, int type, int levelsPerColor) {
45  
46          long start = System.currentTimeMillis();
47  
48          for (int i = 0; i < n; i++) {
49              double x = Math.random() * width;
50              double y = yo + 20 + Math.random() * (height - 20);
51              int colorUnit = 256 / levelsPerColor;
52              int r = (int) (Math.random() * 256 / colorUnit) * colorUnit;
53              int g = (int) (Math.random() * 256 / colorUnit) * colorUnit;
54              int b = (int) (Math.random() * 256 / colorUnit) * colorUnit;
55              vg.setColor(new Color(r, g, b));
56              vg.fillSymbol(x, y, 6, type);
57          }
58          long end = System.currentTimeMillis();
59  
60          vg.setColor(Color.BLACK);
61          vg.drawString(n + " symbols of type " + type + " filled in "
62                  + (end - start) + " ms", 10, yo + 15);
63      }
64  
65      public static void main(String[] args) {
66  
67          // Create a new frame to hold everything.
68          TestingFrame frame = new TestingFrame("Test Symbol Performance");
69  
70          // Create a new instance of this class and add it to the frame.
71          frame.addPanel(new TestSymbolPerformance());
72  
73          // Give the frame a size and make it visible.
74          frame.pack();
75          frame.setSize(new Dimension(600, 600));
76          frame.setVisible(true);
77      }
78  }