1
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
33
34
35 public class TestColorMap extends JFrame implements ActionListener {
36
37
38 ColorMap colorMap = new ColorMap();
39
40
41 ExportDialog dialog;
42
43
44 JPanel panel;
45
46
47 public TestColorMap() {
48
49
50 super("Color Map Test");
51
52
53 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
54 addWindowListener(new WindowAdapter() {
55 public void windowClosing(WindowEvent e) {
56 quit();
57 }
58 });
59
60
61 dialog = new ExportDialog();
62
63
64 JMenuBar menuBar = new JMenuBar();
65 JMenu file = new JMenu("File");
66
67
68 JMenuItem export = new JMenuItem("Export...");
69 export.addActionListener(this);
70 file.add(export);
71
72
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
82 menuBar.add(file);
83 setJMenuBar(menuBar);
84
85
86 Container content = this.getContentPane();
87
88
89 content.setLayout(new BorderLayout());
90
91
92 String[] chooserTest = { "Display", "Print", "Grayscale", "Black&White" };
93 JComboBox chooser = new JComboBox(chooserTest);
94 chooser.addActionListener(this);
95
96
97 content.add(chooser, BorderLayout.NORTH);
98
99
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
105 panel = new JPanel();
106 panel.setLayout(new GridLayout(13, 3));
107
108
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
127 content.add(panel, BorderLayout.CENTER);
128
129 }
130
131
132
133
134
135 public void quit() {
136
137
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
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
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
199 TestColorMap test = new TestColorMap();
200
201
202 test.pack();
203 test.setSize(new Dimension(350, 700));
204 test.setVisible(true);
205
206 }
207 }