| 1 |
// Copyright 2002, SLAC, Stanford University, U.S.A. |
| 2 |
package org.freehep.graphicsio.test; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Font; |
| 7 |
import java.awt.Graphics; |
| 8 |
|
| 9 |
import org.freehep.graphics2d.VectorGraphics; |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
* @author Mark Donszelmann |
| 14 |
* @author Charles Loomis |
| 15 |
* @version $Id: freehep-graphicsio-tests/src/main/java/org/freehep/graphicsio/test/TestShapes.java f493ff6e61b2 2005/12/01 18:46:43 duns $ |
| 16 |
*/ |
| 17 |
public class TestShapes extends TestingPanel { |
| 18 |
|
| 19 |
final static int maxCharHeight = 15; |
| 20 |
|
| 21 |
final static Color bg = Color.white; |
| 22 |
|
| 23 |
final static Color fg = Color.black; |
| 24 |
|
| 25 |
final static Color red = Color.red; |
| 26 |
|
| 27 |
final static Color white = Color.white; |
| 28 |
|
| 29 |
Dimension totalSize; |
| 30 |
|
| 31 |
public TestShapes(String[] args) throws Exception { |
| 32 |
super(args); |
| 33 |
setName("Shapes"); |
| 34 |
// Initialize drawing colors |
| 35 |
setBackground(bg); |
| 36 |
setForeground(fg); |
| 37 |
} |
| 38 |
|
| 39 |
public void paintComponent(Graphics g) { |
| 40 |
VectorGraphics vg = VectorGraphics.create(g); |
| 41 |
if (vg != null) |
| 42 |
drawComponent(vg); |
| 43 |
} |
| 44 |
|
| 45 |
protected void drawComponent(VectorGraphics g) { |
| 46 |
|
| 47 |
// Fill in the background. |
| 48 |
Dimension d = getSize(); |
| 49 |
|
| 50 |
g.setColor(bg); |
| 51 |
g.fillRect(0, 0, d.width, d.height); |
| 52 |
|
| 53 |
int gridWidth = d.width / 6; |
| 54 |
int gridHeight = d.height / 8; |
| 55 |
|
| 56 |
int x = 5; |
| 57 |
int y = 7; |
| 58 |
int rectWidth = gridWidth - 2 * x; |
| 59 |
int rectHeight = gridHeight - 2 * y; |
| 60 |
|
| 61 |
drawShapes(g, x, y, rectWidth, rectHeight, gridWidth); |
| 62 |
x = 5; |
| 63 |
y += gridHeight; |
| 64 |
|
| 65 |
drawFilledShapes(g, x, y, rectWidth, rectHeight, gridWidth); |
| 66 |
x = 5; |
| 67 |
y += gridHeight; |
| 68 |
|
| 69 |
g.setLineWidth(5.0); |
| 70 |
drawShapes(g, x, y, rectWidth, rectHeight, gridWidth); |
| 71 |
x = 5; |
| 72 |
y += gridHeight; |
| 73 |
|
| 74 |
drawFilledShapes(g, x, y, rectWidth, rectHeight, gridWidth); |
| 75 |
x = 5; |
| 76 |
y += gridHeight; |
| 77 |
|
| 78 |
int xp = x; |
| 79 |
int yp = y; |
| 80 |
|
| 81 |
String saying = "The quick brown fox jumped over the lazy dog."; |
| 82 |
|
| 83 |
Font thisFont = new Font("SansSerif", Font.PLAIN, 14); |
| 84 |
g.setFont(thisFont); |
| 85 |
g.drawString("SansSerif: " + saying, xp, yp); |
| 86 |
yp += 16; |
| 87 |
|
| 88 |
thisFont = new Font("SansSerif", Font.BOLD, 14); |
| 89 |
g.setFont(thisFont); |
| 90 |
g.drawString("SansSerif (bold): " + saying, xp, yp); |
| 91 |
yp += 16; |
| 92 |
|
| 93 |
thisFont = new Font("SansSerif", Font.ITALIC, 14); |
| 94 |
g.setFont(thisFont); |
| 95 |
g.drawString("SansSerif (italic): " + saying, xp, yp); |
| 96 |
yp += 16; |
| 97 |
|
| 98 |
thisFont = new Font("Serif", Font.PLAIN, 14); |
| 99 |
g.setFont(thisFont); |
| 100 |
g.drawString("Serif: " + saying, xp, yp); |
| 101 |
yp += 16; |
| 102 |
|
| 103 |
thisFont = new Font("Serif", Font.BOLD, 14); |
| 104 |
g.setFont(thisFont); |
| 105 |
g.drawString("Serif (bold): " + saying, xp, yp); |
| 106 |
yp += 16; |
| 107 |
|
| 108 |
thisFont = new Font("Serif", Font.ITALIC, 14); |
| 109 |
g.setFont(thisFont); |
| 110 |
g.drawString("Serif (italic): " + saying, xp, yp); |
| 111 |
yp += 16; |
| 112 |
|
| 113 |
thisFont = new Font("Monospaced", Font.PLAIN, 14); |
| 114 |
g.setFont(thisFont); |
| 115 |
g.drawString("Monospaced: " + saying, xp, yp); |
| 116 |
yp += 16; |
| 117 |
|
| 118 |
thisFont = new Font("Monospaced", Font.BOLD, 14); |
| 119 |
g.setFont(thisFont); |
| 120 |
g.drawString("Monospaced (bold): " + saying, xp, yp); |
| 121 |
yp += 16; |
| 122 |
|
| 123 |
thisFont = new Font("Monospaced", Font.ITALIC, 14); |
| 124 |
g.setFont(thisFont); |
| 125 |
g.drawString("Monospaced (italic): " + saying, xp, yp); |
| 126 |
yp += 16; |
| 127 |
|
| 128 |
thisFont = new Font("Symbol", Font.PLAIN, 14); |
| 129 |
g.setFont(thisFont); |
| 130 |
g.drawString("Symbol: " + saying, xp, yp); |
| 131 |
yp += 16; |
| 132 |
|
| 133 |
thisFont = new Font("Symbol", Font.BOLD, 14); |
| 134 |
g.setFont(thisFont); |
| 135 |
g.drawString("Symbol (bold): " + saying, xp, yp); |
| 136 |
yp += 16; |
| 137 |
|
| 138 |
thisFont = new Font("Symbol", Font.ITALIC, 14); |
| 139 |
g.setFont(thisFont); |
| 140 |
g.drawString("Symbol (italic): " + saying, xp, yp); |
| 141 |
yp += 16; |
| 142 |
|
| 143 |
thisFont = new Font("ZapfDingbats", Font.PLAIN, 14); |
| 144 |
g.setFont(thisFont); |
| 145 |
g.drawString("ZapfDingbats: " + saying, xp, yp); |
| 146 |
yp += 16; |
| 147 |
|
| 148 |
thisFont = new Font("ZapfDingbats", Font.BOLD, 14); |
| 149 |
g.setFont(thisFont); |
| 150 |
g.drawString("ZapfDingbats (bold): " + saying, xp, yp); |
| 151 |
yp += 16; |
| 152 |
|
| 153 |
thisFont = new Font("ZapfDingbats", Font.ITALIC, 14); |
| 154 |
g.setFont(thisFont); |
| 155 |
g.drawString("ZapfDingbats (italic): " + saying, xp, yp); |
| 156 |
yp += 16; |
| 157 |
|
| 158 |
thisFont = new Font("Monospaced", Font.PLAIN, 14); |
| 159 |
g.setFont(thisFont); |
| 160 |
g.drawString("Unbalanced (( )) ))) ((( TEST! )T( (T)", xp, yp); |
| 161 |
yp += 16; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
private void drawShapes(VectorGraphics g, int x, int y, int rectWidth, |
| 166 |
int rectHeight, int gridWidth) { |
| 167 |
// draw Line2D.Double |
| 168 |
g.setColor(Color.red); |
| 169 |
g.drawLine(x, y + rectHeight - 1, x + rectWidth, y); |
| 170 |
x += gridWidth; |
| 171 |
|
| 172 |
// draw Rectangle2D.Double |
| 173 |
g.setColor(Color.green); |
| 174 |
g.drawRect(x, y, rectWidth, rectHeight); |
| 175 |
x += gridWidth; |
| 176 |
|
| 177 |
// draw RoundRectangle2D.Double |
| 178 |
g.setColor(Color.blue); |
| 179 |
g.drawRoundRect(x, y, rectWidth, rectHeight, 50, 50); |
| 180 |
x += gridWidth; |
| 181 |
|
| 182 |
// draw Arc2D.Double |
| 183 |
g.setColor(Color.cyan); |
| 184 |
g.drawArc(x, y, rectWidth, rectHeight, 90, 135); |
| 185 |
x += gridWidth; |
| 186 |
|
| 187 |
// draw Ellipse2D.Double |
| 188 |
g.setColor(Color.magenta); |
| 189 |
g.drawOval(x, y, rectWidth, rectHeight); |
| 190 |
x += gridWidth; |
| 191 |
|
| 192 |
// draw GeneralPath (polygon) |
| 193 |
int x1Points[] = { x, x + rectWidth, x, x + rectWidth }; |
| 194 |
int y1Points[] = { y, y + rectHeight, y + rectHeight, y }; |
| 195 |
|
| 196 |
g.setColor(Color.yellow); |
| 197 |
g.drawPolygon(x1Points, y1Points, 4); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
private void drawFilledShapes(VectorGraphics g, int x, int y, |
| 202 |
int rectWidth, int rectHeight, int gridWidth) { |
| 203 |
// draw GeneralPath (polyline) |
| 204 |
|
| 205 |
int x2Points[] = { x, x + rectWidth, x, x + rectWidth }; |
| 206 |
int y2Points[] = { y, y + rectHeight, y + rectHeight, y }; |
| 207 |
|
| 208 |
g.setColor(Color.red); |
| 209 |
g.drawPolyline(x2Points, y2Points, 4); |
| 210 |
x += gridWidth; |
| 211 |
|
| 212 |
// fill Rectangle2D.Double (red) |
| 213 |
g.setColor(Color.green); |
| 214 |
g.fillRect(x, y, rectWidth, rectHeight); |
| 215 |
g.setColor(fg); |
| 216 |
g.drawRect(x, y, rectWidth, rectHeight); |
| 217 |
x += gridWidth; |
| 218 |
|
| 219 |
// fill RoundRectangle2D.Double |
| 220 |
g.setColor(Color.blue); |
| 221 |
g.fillRoundRect(x, y, rectWidth, rectHeight, 50, 50); |
| 222 |
g.setColor(fg); |
| 223 |
x += gridWidth; |
| 224 |
|
| 225 |
// fill Arc2D |
| 226 |
g.setColor(Color.cyan); |
| 227 |
g.fillArc(x, y, rectWidth, rectHeight, 90, 135); |
| 228 |
g.setColor(fg); |
| 229 |
x += gridWidth; |
| 230 |
|
| 231 |
// fill Ellipse2D.Double |
| 232 |
g.setColor(Color.magenta); |
| 233 |
g.fillOval(x, y, rectWidth, rectHeight); |
| 234 |
g.setColor(fg); |
| 235 |
x += gridWidth; |
| 236 |
|
| 237 |
// fill and stroke GeneralPath |
| 238 |
int x3Points[] = { x, x + rectWidth, x, x + rectWidth }; |
| 239 |
int y3Points[] = { y, y + rectHeight, y + rectHeight, y }; |
| 240 |
|
| 241 |
g.setColor(Color.yellow); |
| 242 |
g.fillPolygon(x3Points, y3Points, 4); |
| 243 |
g.setColor(fg); |
| 244 |
g.drawPolygon(x3Points, y3Points, 4); |
| 245 |
} |
| 246 |
|
| 247 |
public static void main(String[] args) throws Exception { |
| 248 |
new TestShapes(args).runTest(); |
| 249 |
} |
| 250 |
} |