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.Container;
7   import java.awt.Dimension;
8   import java.awt.Font;
9   import java.awt.Graphics;
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.JFrame;
18  import javax.swing.JMenu;
19  import javax.swing.JMenuBar;
20  import javax.swing.JMenuItem;
21  import javax.swing.JOptionPane;
22  import javax.swing.JPanel;
23  import javax.swing.WindowConstants;
24  import javax.swing.border.Border;
25  
26  import org.freehep.graphics2d.VectorGraphics;
27  import org.freehep.util.export.ExportDialog;
28  
29  /**
30   * 
31   * @author Charles Loomis
32   * @version $Id: TestUnicodeMap.java 8584 2006-08-10 23:06:37Z duns $
33   */
34  public class TestUnicodeMap extends JFrame implements ActionListener {
35  
36      // Export Dialog.
37      ExportDialog dialog;
38  
39      // The main panel.
40      JPanel panel;
41  
42      // The symbols panel.
43      TestPanel symbols;
44  
45      // Fill this panel with nine panels of different colors.
46      public TestUnicodeMap() {
47  
48          // Title this frame.
49          super("Text Test");
50  
51          // Make this exit when the close button is clicked.
52          setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
53          addWindowListener(new WindowAdapter() {
54              public void windowClosing(WindowEvent e) {
55                  quit();
56              }
57          });
58  
59          // Create the Export dialog.
60          dialog = new ExportDialog();
61  
62          // Make a menu bar and menu.
63          JMenuBar menuBar = new JMenuBar();
64          JMenu file = new JMenu("File");
65          JMenu page = new JMenu("Page");
66          JMenu font = new JMenu("Font");
67  
68          // Add a menu item which will bring up this dialog.
69          JMenuItem export = new JMenuItem("Export...");
70          export.addActionListener(this);
71          file.add(export);
72          file.addSeparator();
73  
74          // Quit menu item.
75          JMenuItem quit = new JMenuItem("Quit");
76          quit.addActionListener(new ActionListener() {
77              public void actionPerformed(ActionEvent e) {
78                  quit();
79              }
80          });
81          file.add(quit);
82  
83          // Latin menu item.
84          JMenuItem item = new JMenuItem("Latin");
85          item.addActionListener(new ActionListener() {
86              public void actionPerformed(ActionEvent e) {
87                  symbols.setStartString("\u0000");
88                  symbols.setEndString("\u00ff");
89                  symbols.repaint();
90              }
91          });
92          page.add(item);
93  
94          item = new JMenuItem("Greek");
95          item.addActionListener(new ActionListener() {
96              public void actionPerformed(ActionEvent e) {
97                  symbols.setStartString("\u0300");
98                  symbols.setEndString("\u03ff");
99                  symbols.repaint();
100             }
101         });
102         page.add(item);
103 
104         item = new JMenuItem("Punctuation");
105         item.addActionListener(new ActionListener() {
106             public void actionPerformed(ActionEvent e) {
107                 symbols.setStartString("\u2000");
108                 symbols.setEndString("\u20ff");
109                 symbols.repaint();
110             }
111         });
112         page.add(item);
113 
114         item = new JMenuItem("Arrows");
115         item.addActionListener(new ActionListener() {
116             public void actionPerformed(ActionEvent e) {
117                 symbols.setStartString("\u2100");
118                 symbols.setEndString("\u21ff");
119                 symbols.repaint();
120             }
121         });
122         page.add(item);
123 
124         item = new JMenuItem("MathOps");
125         item.addActionListener(new ActionListener() {
126             public void actionPerformed(ActionEvent e) {
127                 symbols.setStartString("\u2200");
128                 symbols.setEndString("\u22ff");
129                 symbols.repaint();
130             }
131         });
132         page.add(item);
133 
134         item = new JMenuItem("Dingbats");
135         item.addActionListener(new ActionListener() {
136             public void actionPerformed(ActionEvent e) {
137                 symbols.setStartString("\u2700");
138                 symbols.setEndString("\u27ff");
139                 symbols.repaint();
140             }
141         });
142         page.add(item);
143 
144         item = new JMenuItem("Courier");
145         item.addActionListener(new ActionListener() {
146             public void actionPerformed(ActionEvent e) {
147                 symbols.setFont("courier");
148                 repaint();
149             }
150         });
151         font.add(item);
152 
153         item = new JMenuItem("Times");
154         item.addActionListener(new ActionListener() {
155             public void actionPerformed(ActionEvent e) {
156                 symbols.setFont("serif");
157                 repaint();
158             }
159         });
160         font.add(item);
161 
162         item = new JMenuItem("Helvetica");
163         item.addActionListener(new ActionListener() {
164             public void actionPerformed(ActionEvent e) {
165                 symbols.setFont("sansserif");
166                 repaint();
167             }
168         });
169         font.add(item);
170 
171         // Add this to the frame.
172         menuBar.add(file);
173         menuBar.add(page);
174         menuBar.add(font);
175         setJMenuBar(menuBar);
176 
177         // Get the content pane.
178         Container content = this.getContentPane();
179 
180         // Set the layout of this panel.
181         content.setLayout(new BorderLayout());
182 
183         // Create a border of white surrounded by black.
184         Border border = BorderFactory.createCompoundBorder(BorderFactory
185                 .createMatteBorder(1, 1, 1, 1, Color.white), BorderFactory
186                 .createMatteBorder(2, 2, 2, 2, Color.black));
187 
188         // Create a subpanel to hold the symbol panel.
189         panel = new JPanel();
190         panel.setLayout(new BorderLayout());
191         symbols = new TestPanel();
192         symbols.setBorder(border);
193         panel.add(symbols);
194 
195         // Add this panel to this container.
196         content.add(panel, BorderLayout.CENTER);
197 
198     }
199 
200     // Action performed method used to change color map.
201     public void actionPerformed(ActionEvent e) {
202         Object source = e.getSource();
203         if (source instanceof JMenuItem) {
204             dialog.showExportDialog(this, "Export...", panel, "untitled");
205         }
206     }
207 
208     /**
209      * This method brings up a dialog box to ask if the user really wants to
210      * quit. If the answer is yes, the application is stopped.
211      */
212     public void quit() {
213 
214         // Create a dialog box to ask if the user really wants to quit.
215         int n = JOptionPane.showConfirmDialog(this,
216                 "Do you really want to quit?", "Confirm Quit",
217                 JOptionPane.YES_NO_OPTION);
218 
219         if (n == JOptionPane.YES_OPTION) {
220             System.exit(0);
221         }
222     }
223 
224     // Class panel which just paints a given background color.
225     class TestPanel extends JPanel {
226 
227         String[] label = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
228                 "A", "B", "C", "D", "E", "F" };
229 
230         String startString = "\u0000";
231 
232         String endString = "\u00ff";
233 
234         String fontname = "serif";
235 
236         int fontsize = 0;
237 
238         public void setStartString(String startString) {
239             this.startString = startString;
240         }
241 
242         public void setEndString(String endString) {
243             this.endString = endString;
244         }
245 
246         public void setFont(String fontname) {
247             this.fontname = fontname;
248         }
249 
250         public void paintComponent(Graphics g) {
251             if (g != null) {
252 
253                 VectorGraphics vg = VectorGraphics.create(g);
254 
255                 Dimension dim = getSize();
256                 Insets insets = getInsets();
257 
258                 vg.setColor(Color.white);
259                 vg
260                         .fillRect(insets.left, insets.top, dim.width
261                                 - insets.left - insets.right, dim.height
262                                 - insets.top - insets.bottom);
263 
264                 int dw = dim.width / 17;
265                 int dh = dim.height / 17;
266 
267                 vg.setColor(Color.black);
268                 fontsize = dh * 2 / 3;
269                 Font font = new Font(fontname, Font.ITALIC, fontsize);
270                 vg.setFont(font);
271 
272                 StringBuffer character = new StringBuffer(" ");
273 
274                 int iy = dh / 2 + dh;
275                 int ix = dw / 2 + dw;
276                 for (int i = 0; i < 16; i++) {
277                     vg.drawString(label[i], dw / 2, iy,
278                             VectorGraphics.TEXT_CENTER,
279                             VectorGraphics.TEXT_CENTER, false, Color.cyan, 2,
280                             false, Color.yellow);
281                     vg.drawString(label[i], ix, dh / 2,
282                             VectorGraphics.TEXT_CENTER,
283                             VectorGraphics.TEXT_CENTER, false, Color.cyan, 2,
284                             false, Color.yellow);
285                     iy += dh;
286                     ix += dw;
287                 }
288 
289                 iy = dh / 2 + dh;
290                 ix = dw / 2;
291                 for (int i = (int) startString.charAt(0); i <= (int) endString
292                         .charAt(0); i++) {
293 
294                     if (i % 16 == 0) {
295                         iy = dh / 2 + dh;
296                         ix += dw;
297                     }
298 
299                     character.setCharAt(0, (char) i);
300                     vg.drawString(character.toString(), ix, iy,
301                             VectorGraphics.TEXT_CENTER,
302                             VectorGraphics.TEXT_CENTER, false, Color.cyan, 2,
303                             true, Color.yellow);
304                     iy += dh;
305                 }
306 
307             }
308         }
309     }
310 
311     public static void main(String[] args) {
312 
313         // Create a new instance of this class and add it to the frame.
314         TestUnicodeMap test = new TestUnicodeMap();
315 
316         // Give the frame a size and make it visible.
317         test.pack();
318         test.setSize(new Dimension(400, 600));
319         test.setVisible(true);
320 
321     }
322 }