1 package gov
.nasa
.worldwind
.render
;
3 import java
.awt
.image
.BufferedImage
;
5 import java
.awt
.geom
.AffineTransform
;
8 * Static class to creates tilable patterns.
10 * The <code>createPattern()</code> method draws a shape inside a usually square bitmap, so that it will match if tiled.
13 * Each pattern supports a <code>scale</code> factor between <code>zero</code> and <code>one</code> - default is .5.
14 * With a scale of <code>zero</code> no pattern will be produced. With a scale of <code>one</code> the pattern will
15 * cover all the background.
17 * @author Patrick Murris
20 public class PatternFactory
{
22 public final static String PATTERN_CIRCLE
= "PatternFactory.PatternCircle";
23 public final static String PATTERN_CIRCLES
= "PatternFactory.PatternCircles";
24 public final static String PATTERN_SQUARE
= "PatternFactory.PatternSquare";
25 public final static String PATTERN_SQUARES
= "PatternFactory.PatternSquares";
26 public final static String PATTERN_HLINE
= "PatternFactory.PatternHLine";
27 public final static String PATTERN_VLINE
= "PatternFactory.PatternVLine";
28 public final static String PATTERN_HVLINE
= "PatternFactory.PatternHVLine";
29 public final static String PATTERN_DIAGONAL_UP
= "PatternFactory.PatternDiagonalUp";
30 public final static String PATTERN_DIAGONAL_DOWN
= "PatternFactory.PatternDiagonalDown";
32 public final static String GRADIENT_HLINEAR
= "PatternFactory.GradientHLinear";
33 public final static String GRADIENT_VLINEAR
= "PatternFactory.GradientVLinear";
35 private static Dimension defaultDimension
= new Dimension(32, 32);
36 private static float defaultScale
= .5f
;
37 private static Color defaultLineColor
= Color
.LIGHT_GRAY
;
38 private static Color defaultBackColor
= new Color(0f
, 0f
, 0f
, 0f
);
41 * Draws a pattern using the default scale (.5), bitmap dimensions (32x32) and colors (light grey over
42 * a transparent background).
43 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
44 * @return the corresponding <code>BufferedImage</code>.
46 public static BufferedImage
createPattern(String pattern
)
48 return createPattern(pattern
, defaultDimension
, defaultScale
, defaultLineColor
, defaultBackColor
);
52 * Draws a pattern with a given <code>Color</code> using the default scale (.5), bitmap dimensions (32x32)
53 * and backgound color (transparent).
54 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
55 * @param lineColor the pattern <code>Color</code>.
56 * @return the corresponding <code>BufferedImage</code>.
58 public static BufferedImage
createPattern(String pattern
, Color lineColor
)
60 return createPattern(pattern
, defaultDimension
, defaultScale
, lineColor
, defaultBackColor
);
64 * Draws a pattern with a given <code>scale</code> using the default bitmap dimensions (32x32) and colors
65 * (light grey over a transparent background).
66 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
67 * @param scale the scale at which the pattern should be drawn (0 to 1).
68 * @return the corresponding <code>BufferedImage</code>.
70 public static BufferedImage
createPattern(String pattern
, float scale
)
72 return createPattern(pattern
, defaultDimension
, scale
, defaultLineColor
, defaultBackColor
);
76 * Draws a pattern with a given <code>scale</code> and <code>Color</code> using the default bitmap
77 * dimensions (32x32) and backgound color (transparent).
78 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
79 * @param scale the scale at which the pattern should be drawn (0 to 1).
80 * @param lineColor the pattern <code>Color</code>.
81 * @return the corresponding <code>BufferedImage</code>.
83 public static BufferedImage
createPattern(String pattern
, float scale
, Color lineColor
)
85 return createPattern(pattern
, defaultDimension
, scale
, lineColor
, defaultBackColor
);
89 * Draws a pattern with a given <code>scale</code> and <code>Color</code>s using the default bitmap
91 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
92 * @param scale the scale at which the pattern should be drawn (0 to 1).
93 * @param lineColor the pattern <code>Color</code>.
94 * @param backColor the pattern background <code>Color</code>.
95 * @return the corresponding <code>BufferedImage</code>.
97 public static BufferedImage
createPattern(String pattern
, float scale
, Color lineColor
, Color backColor
)
99 return createPattern(pattern
, defaultDimension
, scale
, lineColor
, backColor
);
103 * Draws a pattern with a given <code>scale</code>, <code>Color</code> and bitmap
104 * dimensions, using the default backgound color (transparent).
105 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
106 * @param size the <code>Dimension</code> of the <code>BufferedImage produced</code>.
107 * @param scale the scale at which the pattern should be drawn (0 to 1).
108 * @param lineColor the pattern <code>Color</code>.
109 * @return the corresponding <code>BufferedImage</code>.
111 public static BufferedImage
createPattern(String pattern
, Dimension size
, float scale
, Color lineColor
)
113 return createPattern(pattern
, size
, scale
, lineColor
, defaultBackColor
);
117 * Draws a pattern with the given <code>scale</code>, <code>Color</code>s and bitmap dimensions.
118 * @param pattern the pattern to draw. See {@link PatternFactory} static constants.
119 * @param size the <code>Dimension</code> of the <code>BufferedImage produced</code>.
120 * @param scale the scale at which the pattern should be drawn (0 to 1).
121 * @param lineColor the pattern <code>Color</code>.
122 * @param backColor the pattern background <code>Color</code>.
123 * @return the corresponding <code>BufferedImage</code>.
125 public static BufferedImage
createPattern(String pattern
, Dimension size
, float scale
, Color lineColor
, Color backColor
)
127 int halfWidth
= size
.width
/ 2;
128 int halfHeight
= size
.height
/ 2;
129 int dim
= (int)(size
.width
* scale
);
130 BufferedImage image
= new BufferedImage(size
.width
, size
.height
, BufferedImage
.TYPE_4BYTE_ABGR
);
131 Graphics2D g2
= image
.createGraphics();
132 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
, RenderingHints
.VALUE_ANTIALIAS_ON
);
135 g2
.setPaint(backColor
);
136 g2
.fillRect(0, 0, size
.width
, size
.height
);
141 g2
.setPaint(lineColor
);
142 g2
.setStroke(new BasicStroke(dim
));
143 if (pattern
.compareTo(PATTERN_HLINE
) == 0)
145 int y
= halfHeight
- 1 - dim
/ 2;
146 g2
.fillRect(0, y
, size
.width
, dim
);
148 else if (pattern
.compareTo(PATTERN_VLINE
) == 0)
150 int x
= halfWidth
- 1 - dim
/ 2;
151 g2
.fillRect(x
, 0, dim
, size
.height
);
153 if (pattern
.compareTo(PATTERN_HVLINE
) == 0)
155 int x
= halfWidth
- 1 - dim
/ 2;
156 g2
.fillRect(x
, 0, dim
, size
.height
);
157 int y
= halfHeight
- 1 - dim
/ 2;
158 g2
.fillRect(0, y
, size
.width
, dim
);
160 else if (pattern
.compareTo(PATTERN_SQUARE
) == 0)
162 int x
= halfWidth
- 1 - dim
/ 2;
163 int y
= halfHeight
- 1 - dim
/ 2;
164 g2
.fillRect(x
, y
, dim
, dim
);
166 else if (pattern
.compareTo(PATTERN_SQUARES
) == 0)
168 int x
= halfWidth
- 1 - dim
/ 2;
169 int y
= halfHeight
- 1 - dim
/ 2;
170 g2
.fillRect(x
, y
, dim
, dim
);
171 g2
.fillRect(x
- halfWidth
, y
- halfHeight
, dim
, dim
);
172 g2
.fillRect(x
- halfWidth
, y
+ halfHeight
, dim
, dim
);
173 g2
.fillRect(x
+ halfWidth
, y
- halfHeight
, dim
, dim
);
174 g2
.fillRect(x
+ halfWidth
, y
+ halfHeight
, dim
, dim
);
176 else if (pattern
.compareTo(PATTERN_CIRCLE
) == 0)
178 int x
= halfWidth
- 1 - dim
/ 2;
179 int y
= halfHeight
- 1 - dim
/ 2;
180 g2
.fillOval(x
, y
, dim
, dim
);
182 else if (pattern
.compareTo(PATTERN_CIRCLES
) == 0)
184 int x
= halfWidth
- 1 - dim
/ 2;
185 int y
= halfHeight
- 1 - dim
/ 2;
186 g2
.fillOval(x
, y
, dim
, dim
);
187 g2
.fillOval(x
- halfWidth
, y
- halfHeight
, dim
, dim
);
188 g2
.fillOval(x
- halfWidth
, y
+ halfHeight
, dim
, dim
);
189 g2
.fillOval(x
+ halfWidth
, y
- halfHeight
, dim
, dim
);
190 g2
.fillOval(x
+ halfWidth
, y
+ halfHeight
, dim
, dim
);
192 else if (pattern
.compareTo(PATTERN_DIAGONAL_UP
) == 0 || pattern
.compareTo(PATTERN_DIAGONAL_DOWN
) == 0)
194 if (pattern
.compareTo(PATTERN_DIAGONAL_DOWN
) == 0)
196 AffineTransform at
= AffineTransform
.getScaleInstance(-1, 1);
197 at
.translate(-size
.width
, 0);
200 g2
.drawLine(-dim
, size
.height
- 1 + dim
, size
.width
- 1 + dim
, - dim
);
201 g2
.drawLine(-dim
- 1, dim
, dim
- 1, - dim
);
202 g2
.drawLine(size
.width
- dim
, size
.height
- 1 + dim
, size
.width
+ dim
, size
.height
- 1 - dim
);
204 else if (pattern
.compareTo(GRADIENT_VLINEAR
) == 0)
206 g2
.setPaint(new GradientPaint((float)halfWidth
, 0f
, lineColor
, (float)halfWidth
, (float)size
.height
- 1, backColor
));
207 g2
.fillRect(0, 0, size
.width
, size
.height
);
209 else if (pattern
.compareTo(GRADIENT_HLINEAR
) == 0)
211 g2
.setPaint(new GradientPaint(0f
, halfHeight
, lineColor
, (float)size
.width
- 1, halfHeight
, backColor
));
212 g2
.fillRect(0, 0, size
.width
, size
.height
);