1
2 package org.freehep.graphicsio.test;
3
4 import java.awt.Component;
5 import java.awt.Dimension;
6 import java.io.File;
7 import java.lang.reflect.Constructor;
8 import java.lang.reflect.InvocationTargetException;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Properties;
12
13 import javax.swing.JComponent;
14 import javax.swing.JPanel;
15
16 import org.freehep.graphics2d.VectorGraphics;
17 import org.freehep.graphicsio.ImageGraphics2D;
18 import org.freehep.swing.Headless;
19 import org.freehep.util.UserProperties;
20
21
22
23
24
25 public class TestingPanel extends JPanel {
26
27 public static final int width = 600;
28 public static final int height = 600;
29
30 protected String[] args;
31
32 protected VectorGraphics graphics;
33
34 protected List
35
36 protected List
37
38 public TestingPanel(String[] args) throws Exception {
39 this.args = args;
40 if ((args != null) && (args.length != 0)) {
41 if (args[0].equals(ImageGraphics2D.class.getName())) {
42 if (args.length != 3) {
43 System.err.println("Usage: Test... "
44 + ImageGraphics2D.class + " format OutputFile");
45 System.exit(1);
46 }
47 } else {
48 if (args.length != 2) {
49 System.err
50 .println("Usage: Test... VectorGraphicsClassName OutputFile");
51 System.exit(1);
52 }
53 }
54 }
55 }
56
57 protected void addPage(String name, JComponent c) {
58 names.add(name);
59 pages.add(c);
60 }
61
62 public void runTest() throws Exception {
63 runTest(null);
64 }
65
66 public void runTest(int width, int height) throws Exception {
67 runTest(width, height, null);
68 }
69
70 public void runTest(Properties options) throws Exception {
71 runTest(width, height, options);
72 }
73
74 public void runTest(int width, int height, Properties options)
75 throws Exception {
76
77 setPreferredSize(new Dimension(width, height));
78
79 if ((args == null) || (args.length == 0)) {
80
81 TestingFrame frame = new TestingFrame(getClass().toString());
82
83 if (names.size() > 0) {
84 for (int i = 0; i < names.size(); i++) {
85 frame.addPanel((String) names.get(i), (JComponent) pages
86 .get(i));
87 }
88 frame.setSize(width, height);
89 } else {
90
91 frame.addPanel(this);
92 frame.pack();
93 }
94
95
96 frame.setVisible(true);
97 } else {
98
99 Headless headless = new Headless(this);
100 headless.pack();
101 headless.setVisible(true);
102
103 if (args[0].equals(ImageGraphics2D.class.getName())) {
104 File file = new File(args[2]);
105 graphics = new ImageGraphics2D(file, this, args[1]);
106 } else {
107 try {
108 File file = new File(args[1]);
109
110 Class cls = Class.forName(args[0]);
111 Constructor constructor = cls.getConstructor(new Class[] {
112 File.class, Component.class });
113 graphics = (VectorGraphics) constructor
114 .newInstance(new Object[] { file, this });
115 } catch (ClassNotFoundException e) {
116 System.out.println("Cannot find class: " + args[0]);
117 throw e;
118 } catch (ClassCastException e) {
119 System.out.println("Class: " + args[0]
120 + " does not extend VectorGraphics");
121 throw e;
122 } catch (NoSuchMethodException e) {
123 System.out.println("Class: " + args[0]
124 + " does not have constructor(File, Component)");
125 throw e;
126 } catch (InvocationTargetException e) {
127 System.out.println(e.getTargetException());
128 e.getTargetException().printStackTrace();
129 throw e;
130 }
131 }
132
133
134 UserProperties user = (options == null) ? new UserProperties()
135 : new UserProperties(options);
136
137
138
139
140
141
142
143 user.setProperty(ImageGraphics2D.ANTIALIAS_TEXT, false);
144
145
146
147
148
149
150 graphics.setProperties(user);
151
152 graphics.setDeviceIndependent(true);
153 graphics.startExport();
154 print(graphics);
155 graphics.endExport();
156 }
157 }
158 }