1   // Copyright 2000, CERN, Geneva, Switzerland and University of Santa Cruz, California, U.S.A.
2   package org.freehep.graphicsio.ps.test;
3   
4   import java.awt.BorderLayout;
5   import java.awt.Color;
6   import java.awt.Container;
7   import java.awt.Dimension;
8   import java.awt.Graphics;
9   import java.awt.GridLayout;
10  import java.awt.Insets;
11  import java.awt.event.ActionEvent;
12  import java.awt.event.ActionListener;
13  import java.awt.event.WindowAdapter;
14  import java.awt.event.WindowEvent;
15  
16  import javax.swing.BorderFactory;
17  import javax.swing.JComboBox;
18  import javax.swing.JFrame;
19  import javax.swing.JMenu;
20  import javax.swing.JMenuBar;
21  import javax.swing.JMenuItem;
22  import javax.swing.JOptionPane;
23  import javax.swing.JPanel;
24  import javax.swing.WindowConstants;
25  import javax.swing.border.Border;
26  
27  import org.freehep.graphicsio.ps.ColorMap;
28  import org.freehep.util.export.ExportDialog;
29  
30  /**
31   * 
32   * @author Charles Loomis
33   * @version $Id: TestColorMap.java,v 1.8 2002/07/26 06:44:21 duns Exp $
34   */
35  public class TestColorMap extends JFrame implements ActionListener {
36  
37      // Create the color map to use.
38      ColorMap colorMap = new ColorMap();
39  
40      // Export Dialog.
41      ExportDialog dialog;
42  
43      // The main panel.
44      JPanel panel;
45  
46      // Fill this panel with nine panels of different colors.
47      public TestColorMap() {
48  
49          // Title this frame.
50          super("Color Map Test");
51  
52          // Make this exit when the close button is clicked.
53          setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
54          addWindowListener(new WindowAdapter() {
55              public void windowClosing(WindowEvent e) {
56                  quit();
57              }
58          });
59  
60          // Create the Export dialog.
61          dialog = new ExportDialog();
62  
63          // Make a menu bar and menu.
64          JMenuBar menuBar = new JMenuBar();
65          JMenu file = new JMenu("File");
66  
67          // Add a menu item which will bring up this dialog.
68          JMenuItem export = new JMenuItem("Export...");
69          export.addActionListener(this);
70          file.add(export);
71  
72          // Quit menu item.
73          JMenuItem quit = new JMenuItem("Quit");
74          quit.addActionListener(new ActionListener() {
75              public void actionPerformed(ActionEvent e) {
76                  quit();
77              }
78          });
79          file.add(quit);
80  
81          // Add this to the frame.
82          menuBar.add(file);
83          setJMenuBar(menuBar);
84  
85          // Get the content pane.
86          Container content = this.getContentPane();
87  
88          // Set the layout of this panel.
89          content.setLayout(new BorderLayout());
90  
91          // Create a combo box with the four color maps in it.
92          String[] chooserTest = { "Display", "Print", "Grayscale", "Black&White" };
93          JComboBox chooser = new JComboBox(chooserTest);
94          chooser.addActionListener(this);
95  
96          // Add this to the top of this container.
97          content.add(chooser, BorderLayout.NORTH);
98  
99          // Create a border of white surrounded by black.
100         Border border = BorderFactory.createCompoundBorder(BorderFactory
101                 .createMatteBorder(1, 1, 1, 1, Color.white), BorderFactory
102                 .createMatteBorder(2, 2, 2, 2, Color.black));
103 
104         // Create a subpanel with grid layout to hold the colored tiles.
105         panel = new JPanel();
106         panel.setLayout(new GridLayout(13, 3));
107 
108         // Add tiles of colors to this panel.
109         for (int i = 0; i < 13; i++) {
110             TestPanel test;
111 
112             test = new TestPanel(colorMap, i);
113             test.setBorder(border);
114             panel.add(test);
115 
116             test = new TestPanel(colorMap, i + 13);
117             test.setBorder(border);
118             panel.add(test);
119 
120             test = new TestPanel(colorMap, i + 26);
121             test.setBorder(border);
122             panel.add(test);
123 
124         }
125 
126         // Add this panel to this container.
127         content.add(panel, BorderLayout.CENTER);
128 
129     }
130 
131     /**
132      * This method brings up a dialog box to ask if the user really wants to
133      * quit. If the answer is yes, the application is stopped.
134      */
135     public void quit() {
136 
137         // Create a dialog box to ask if the user really wants to quit.
138         int n = JOptionPane.showConfirmDialog(this,
139                 "Do you really want to quit?", "Confirm Quit",
140                 JOptionPane.YES_NO_OPTION);
141 
142         if (n == JOptionPane.YES_OPTION) {
143             System.exit(0);
144         }
145     }
146 
147     // Action performed method used to change color map.
148     public void actionPerformed(ActionEvent e) {
149         Object source = e.getSource();
150         if (source instanceof JComboBox) {
151             JComboBox cb = (JComboBox) e.getSource();
152             String tag = (String) cb.getSelectedItem();
153 
154             if (tag == "Display") {
155                 colorMap.useDisplayColorMap();
156             } else if (tag == "Print") {
157                 colorMap.usePrintColorMap();
158             } else if (tag == "Grayscale") {
159                 colorMap.useGrayscaleColorMap();
160             } else if (tag == "Black&White") {
161                 colorMap.useBlackAndWhiteColorMap();
162             }
163             this.repaint();
164         } else if (source instanceof JMenuItem) {
165             dialog.showExportDialog(this, "Export...", panel, "untitled");
166         }
167     }
168 
169     // Class panel which just paints a given background color.
170     class TestPanel extends JPanel {
171         private ColorMap colorMap;
172 
173         private int bkgColorIndex;
174 
175         public TestPanel(ColorMap colorMap, int bkgColorIndex) {
176             this.colorMap = colorMap;
177             this.bkgColorIndex = bkgColorIndex;
178         }
179 
180         public void paintComponent(Graphics g) {
181             if (g != null) {
182                 Dimension dim = getSize();
183                 Insets insets = getInsets();
184 
185                 Color bkgColor = colorMap.getColor(bkgColorIndex);
186 
187                 g.setColor(bkgColor);
188                 g
189                         .fillRect(insets.left, insets.top, dim.width
190                                 - insets.left - insets.right, dim.height
191                                 - insets.top - insets.bottom);
192             }
193         }
194     }
195 
196     public static void main(String[] args) {
197 
198         // Create a new instance of this class and add it to the frame.
199         TestColorMap test = new TestColorMap();
200 
201         // Give the frame a size and make it visible.
202         test.pack();
203         test.setSize(new Dimension(350, 700));
204         test.setVisible(true);
205 
206     }
207 }