View Javadoc

1   // Copyright 2000, CERN, Geneva, Switzerland and University of Santa Cruz, California, U.S.A.
2   package org.freehep.graphicsio.test;
3   
4   import java.awt.BorderLayout;
5   import java.awt.Color;
6   import java.awt.Dimension;
7   import java.awt.Graphics;
8   import java.awt.GridLayout;
9   import java.awt.Insets;
10  
11  import javax.swing.BorderFactory;
12  import javax.swing.JLabel;
13  import javax.swing.JPanel;
14  import javax.swing.border.Border;
15  
16  import org.freehep.graphics2d.PrintColor;
17  import org.freehep.graphics2d.VectorGraphics;
18  
19  /**
20   * 
21   * @author Mark Donszelmann
22   * @version $Id: TestPrintColors.java 8584 2006-08-10 23:06:37Z duns $
23   */
24  public class TestPrintColors extends TestingPanel {
25  
26      // Fill this panel with nine panels of different colors.
27      public TestPrintColors(String[] args) throws Exception {
28  
29          super(args);
30  
31          // Set the layout of this panel.
32          setLayout(new BorderLayout());
33  
34          // Create a border of white surrounded by black.
35          Border border = BorderFactory.createCompoundBorder(BorderFactory
36                  .createMatteBorder(1, 1, 1, 1, Color.white), BorderFactory
37                  .createMatteBorder(2, 2, 2, 2, Color.black));
38  
39          // Create a subpanel with grid layout to hold the colored tiles.
40          JPanel panel = new JPanel();
41          panel.setLayout(new GridLayout(14, 3));
42  
43          JLabel label1 = new JLabel("Color");
44          // label1.setFont(new Font("Lucida Sans", Font.BOLD, 12));
45          panel.add(label1);
46  
47          JLabel label2 = new JLabel("GrayScale");
48          // label2.setFont(new Font("Lucida Sans", Font.BOLD, 12));
49          panel.add(label2);
50  
51          JLabel label3 = new JLabel("Black and White");
52          // label3.setFont(new Font("Lucida Sans", Font.BOLD, 12));
53          panel.add(label3);
54  
55          // Add tiles of colors to this panel.
56          for (int i = 0; i < 13; i++) {
57              TestColor test;
58  
59              test = new TestColor(PrintColor.COLOR, i);
60              test.setBorder(border);
61              panel.add(test);
62  
63              test = new TestColor(PrintColor.GRAYSCALE, i);
64              test.setBorder(border);
65              panel.add(test);
66  
67              test = new TestColor(PrintColor.BLACK_AND_WHITE, i);
68              test.setBorder(border);
69              panel.add(test);
70  
71          }
72  
73          // Add this panel to this container.
74          add(panel, BorderLayout.CENTER);
75      }
76  
77      // Class panel which just paints a given background color.
78      class TestColor extends JPanel {
79          private int mode;
80  
81          private int bkgColorIndex;
82  
83          public TestColor(int mode, int bkgColorIndex) {
84              this.mode = mode;
85              this.bkgColorIndex = bkgColorIndex;
86          }
87  
88          public void paintComponent(Graphics g) {
89              Dimension dim = getSize();
90              Insets insets = getInsets();
91  
92              Color bkgColor = PrintColor.getDefaultColor(bkgColorIndex);
93  
94              VectorGraphics vg = VectorGraphics.create(g);
95              vg.setColorMode(mode);
96              vg.setColor(bkgColor);
97              vg.fillRect(insets.left, insets.top, dim.width - insets.left
98                      - insets.right, dim.height - insets.top - insets.bottom);
99          }
100     }
101 
102     public static void main(String[] args) throws Exception {
103         new TestPrintColors(args).runTest();
104     }
105 }