Debian Codegraph @

freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestAll.java

raw
1 // Copyright 2002-2007, FreeHEP.
2 package org.freehep.graphicsio.test;
3
4 import java.awt.BasicStroke;
5 import java.awt.Color;
6 import java.awt.Dimension;
7 import java.awt.Font;
8 import java.awt.GradientPaint;
9 import java.awt.Graphics;
10 import java.awt.Image;
11 import java.awt.Insets;
12 import java.awt.MediaTracker;
13 import java.awt.TexturePaint;
14 import java.awt.Rectangle;
15 import java.awt.font.TextAttribute;
16 import java.awt.geom.GeneralPath;
17 import java.awt.geom.Rectangle2D;
18 import java.awt.geom.AffineTransform;
19 import java.awt.image.BufferedImage;
20 import java.text.AttributedString;
21
22 import org.freehep.graphics2d.TagString;
23 import org.freehep.graphics2d.VectorGraphics;
24 import org.freehep.graphics2d.VectorGraphicsConstants;
25 import org.freehep.graphicsbase.util.images.ImageHandler;
26
27 /**
28 * @author Simon Fischer
29 * @version $Id: freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestAll.java e908a30ae307 2007/01/13 00:45:55 duns $
30 */
31 public class TestAll extends TestingPanel implements VectorGraphicsConstants {
32
33 private BufferedImage marble, sky;
34
35 private static float[] dash = { 10.f, 5.f, 2.f, 5.f };
36
37 public TestAll(String[] args) throws Exception {
38 super(args);
39 setName("All");
40 }
41
42 public void paintComponent(Graphics g) {
43
44 if (g == null)
45 return;
46
47 if (marble == null) {
48 MediaTracker t = new MediaTracker(this);
49 Image skyI = ImageHandler.getImage("images/sky.gif", TestAll.class);
50 t.addImage(skyI, 0);
51 try {
52 t.waitForAll();
53 } catch (Exception e) {
54 e.printStackTrace();
55 }
56
57 sky = new BufferedImage(skyI.getWidth(this), skyI.getHeight(this),
58 BufferedImage.TYPE_INT_RGB);
59 sky.createGraphics().drawImage(skyI, 0, 0, this);
60 }
61
62 VectorGraphics vg = VectorGraphics.create(g);
63
64 // Rectangle dim = vg.getClip().getBounds();
65 Dimension dim = getSize();
66 Insets insets = getInsets();
67 double width = dim.getWidth();
68 double height = dim.getHeight();
69
70 vg.setColor(Color.white);
71 vg.fillRect(insets.left, insets.top, dim.width - insets.left
72 - insets.right, dim.height - insets.top - insets.bottom);
73
74 vg.setPaint(new GradientPaint((int) 0, (int) 10, Color.blue,
75 (int) width, (int) 10, Color.white));
76 vg.fillRect(0, 10, width, 30);
77 vg.setFont(new Font("Helvetica", Font.BOLD, 18));
78 vg.setPaint(new GradientPaint((int) 0, (int) 10, Color.white,
79 (int) width, (int) 10, Color.orange));
80 String str = "Testing " + vg.getClass().getName();
81 vg.drawString(str, 10, 30);
82
83 double fx = 3 * width / 16;
84 double fy = 5 * height / 16;
85 double fw = width / 4;
86 for (int i = 0; i < 15; i++) {
87 vg.setColor(Color.black);
88 vg.fillRect(fx - fw / 2, fy - fw / 2, fw, fw);
89 vg.setColor(Color.white);
90 vg.fillOval(fx - fw / 2, fy - fw / 2, fw, fw);
91 fw /= Math.sqrt(2);
92 }
93
94 vg.setColor(Color.black);
95 vg.setFont(new Font("TimesRoman", Font.PLAIN, 11));
96 // vg.setFont(new Font("Arial", Font.PLAIN, 11));
97
98 vg.drawString(
99 new TagString("The <i>drawString</i> methods in <i>VectorGraphics</i> support"),
100 fx + 3 * width / 16,
101 fy - width / 8 + 10);
102 vg.drawString(
103 new TagString("output of strings using a subset of the <b>HTML language</b>."),
104 fx + 3 * width / 16,
105 fy - width / 8 + 24);
106
107 String text = "The direct output of attributed strings is supported too.";
108 AttributedString as = new AttributedString(text);
109 as.addAttribute(
110 TextAttribute.FAMILY,
111 "TimesRoman");
112 as.addAttribute(
113 TextAttribute.SIZE,
114 new Float(11));
115 // make "output" italic
116 as.addAttribute(
117 TextAttribute.POSTURE,
118 TextAttribute.POSTURE_OBLIQUE,
119 text.indexOf("output"),
120 text.indexOf("output") + "output".length());
121 // make "attributed" bold
122 as.addAttribute(
123 TextAttribute.WEIGHT,
124 TextAttribute.WEIGHT_BOLD,
125 text.indexOf("attributed"),
126 text.indexOf("attributed") + "attributed".length());
127 // make "supported" bold
128 as.addAttribute(
129 TextAttribute.TRANSFORM,
130 AffineTransform.getTranslateInstance(0, 5),
131 text.indexOf("supported"),
132 text.indexOf("supported") + "supported".length());
133 as.addAttribute(
134 TextAttribute.FOREGROUND,
135 Color.red,
136 text.indexOf("strings"),
137 text.indexOf("strings") + "strings".length());
138 as.addAttribute(
139 TextAttribute.BACKGROUND,
140 new TexturePaint(sky, new Rectangle(0, 0, 100, 100)),
141 text.indexOf("direct"),
142 text.indexOf("direct") + "direct".length());
143 vg.drawString(
144 as.getIterator(),
145 (float)(fx + 3 * width / 16),
146 (float)(fy - width / 8 + 38));
147
148 vg.setColor(Color.red);
149 double sx = width / 2;
150 double sy = 5 * height / 16;
151 for (int i = 0; i < NUMBER_OF_SYMBOLS; i++) {
152 vg.drawSymbol(sx + 15 * i, sy, 12, i);
153 vg.fillSymbol(sx + 15 * i, sy + 15, 10, i);
154 }
155
156 vg.setPaint(new TexturePaint(sky, new Rectangle2D.Double(0, 0, sky
157 .getWidth(), sky.getHeight())));
158 double mx = 0;
159 double my = height / 2;
160 vg.fillRect(mx, my, width / 2, height / 2);
161 vg.setPaint(Color.black);
162 vg.setFont(new Font("Impact", Font.BOLD, 60));
163 vg.drawString("\u25C4Impact\u25BA", mx + width / 4, my + height / 4,
164 VectorGraphicsConstants.TEXT_CENTER, VectorGraphicsConstants.TEXT_BASELINE);
165
166 GeneralPath shape = new GeneralPath();
167 shape.moveTo(0.f, 0.f);
168 shape.lineTo(25.f, 50.f);
169 shape.lineTo(-25.f, 50.f);
170 shape.lineTo(25.f, -50.f);
171 shape.lineTo(-25.f, -50.f);
172 shape.closePath();
173
174 Graphics subgraphics = vg.create();
175 VectorGraphics svg = VectorGraphics.create(subgraphics);
176 svg.translate(width / 2, height / 2);
177 svg.setStroke(new BasicStroke(8.f, BasicStroke.CAP_BUTT,
178 BasicStroke.JOIN_BEVEL, 10.f, null, 0.f));
179 svg.draw(shape);
180
181 subgraphics = svg.create();
182 VectorGraphics svg2 = VectorGraphics.create(subgraphics);
183 svg2.setColor(Color.black);
184 svg2.setStroke(new BasicStroke(3.f, BasicStroke.CAP_ROUND,
185 BasicStroke.JOIN_ROUND, 10.f, dash, 0.f));
186 svg2.rotate(Math.PI / 5);
187 svg2.draw(shape);
188 svg2.dispose();
189
190 svg.setLineWidth(1);
191 svg.shear(0.5, 0.5);
192 svg.draw(shape);
193 svg.dispose();
194
195 subgraphics = vg.create();
196 svg = VectorGraphics.create(subgraphics);
197 double tx = 3 * width / 4;
198 double ty = 3 * height / 4;
199 double d = width / 10;
200 svg.setColor(Color.black);
201 svg.translate(tx, ty);
202 svg.fillOval(-d, -d, 2 * d, 2 * d);
203 svg.setStroke(new BasicStroke(4.f, BasicStroke.CAP_ROUND,
204 BasicStroke.JOIN_ROUND, 10.f, null, 0.f));
205 svg.drawOval(-1.4 * d, -1.4 * d, 1.4 * 2 * d, 1.4 * 2 * d);
206
207 String circle = "ORAETLAB";
208 svg.setFont(new Font("TimesRoman", Font.BOLD, 16));
209 for (int i = 0; i < circle.length(); i++) {
210 svg.drawString(circle.substring(i, i + 1), 0, -1.1 * d);
211 svg.rotate(Math.PI * 2 / circle.length());
212 }
213 svg.dispose();
214 }
215
216 public static void main(String[] args) throws Exception {
217 new TestAll(args).runTest();
218 }
219 }