001: package com.vividsolutions.jump.workbench.ui.renderer.style;
002:
003: import com.vividsolutions.jts.geom.Coordinate;
004: import com.vividsolutions.jts.io.ParseException;
005: import com.vividsolutions.jts.io.WKTReader;
006: import com.vividsolutions.jts.util.Assert;
007:
008: import com.vividsolutions.jump.geom.Angle;
009: import com.vividsolutions.jump.util.Blackboard;
010: import com.vividsolutions.jump.util.CollectionUtil;
011: import com.vividsolutions.jump.warp.AffineTransform;
012: import com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter;
013:
014: import java.awt.AlphaComposite;
015: import java.awt.BasicStroke;
016: import java.awt.Color;
017: import java.awt.Graphics2D;
018: import java.awt.geom.NoninvertibleTransformException;
019: import java.awt.geom.Point2D;
020: import java.awt.image.BufferedImage;
021:
022: public class WKTFillPattern extends BasicFillPattern {
023: private static final String LINE_WIDTH_KEY = "LINE WIDTH";
024: private static final String EXTENT_KEY = "EXTENT";
025: private static final String PATTERN_WKT_KEY = "PATTERN WKT";
026:
027: /**
028: * Parameterless constructor for Java2XML
029: */
030: public WKTFillPattern() {
031: }
032:
033: public WKTFillPattern(int lineWidth, int extent, String patternWKT) {
034: super (new Blackboard().putAll(CollectionUtil
035: .createMap(new Object[] { BasicFillPattern.COLOR_KEY,
036: Color.black, LINE_WIDTH_KEY,
037: new Integer(lineWidth), EXTENT_KEY,
038: new Integer(extent), PATTERN_WKT_KEY,
039: patternWKT })));
040: }
041:
042: public BufferedImage createImage(Blackboard properties) {
043: BufferedImage image = new BufferedImage(properties
044: .getInt(EXTENT_KEY), properties.getInt(EXTENT_KEY),
045: BufferedImage.TYPE_INT_ARGB);
046: Graphics2D g = image.createGraphics();
047: g.setComposite(AlphaComposite.getInstance(
048: AlphaComposite.SRC_OVER, ((Color) getProperties().get(
049: COLOR_KEY)).getAlpha() / 255f));
050:
051: //Vertical flip so the y-axis points upwards [Jon Aquino]
052: g.scale(1, -1);
053:
054: //Center [Jon Aquino]
055: g.translate(properties.getInt(EXTENT_KEY) / 2d, -properties
056: .getInt(EXTENT_KEY) / 2d);
057: g.setColor((Color) properties.get(BasicFillPattern.COLOR_KEY));
058: g.setStroke(new BasicStroke(properties.getInt(LINE_WIDTH_KEY)));
059:
060: try {
061: g.draw(new Java2DConverter(
062: new Java2DConverter.PointConverter() {
063: public Point2D toViewPoint(
064: Coordinate modelCoordinate) {
065: return new Point2D.Double(
066: modelCoordinate.x,
067: modelCoordinate.y);
068: }
069:
070: public double getScale() {
071: return 1;
072: }
073: }).toShape(new WKTReader().read((String) properties
074: .get(PATTERN_WKT_KEY))));
075: } catch (NoninvertibleTransformException e) {
076: //Eat it [Jon Aquino]
077: } catch (ParseException e) {
078: Assert.shouldNeverReachHere((String) properties
079: .get(PATTERN_WKT_KEY));
080: }
081:
082: return image;
083: }
084:
085: public static WKTFillPattern createDiagonalStripePattern(
086: int lineWidth, double centerlineSeparationInLineWidths,
087: boolean forward, boolean back) {
088: double centerlineSeparation = centerlineSeparationInLineWidths
089: * lineWidth;
090:
091: return new WKTFillPattern(lineWidth, (int) Math.rint(Math
092: .sqrt(2)
093: * centerlineSeparation), "GEOMETRYCOLLECTION("
094: + wktForThreeLines(centerlineSeparation, 45, forward)
095: + ", "
096: + wktForThreeLines(centerlineSeparation, -45, back)
097: + ")");
098: }
099:
100: public static WKTFillPattern createVerticalHorizontalStripePattern(
101: int lineWidth, double centerlineSeparationInLineWidths,
102: boolean vertical, boolean horizontal) {
103: double centerlineSeparation = centerlineSeparationInLineWidths
104: * lineWidth;
105:
106: return new WKTFillPattern(lineWidth, (int) Math
107: .rint(2 * centerlineSeparation), "GEOMETRYCOLLECTION("
108: + wktForThreeLines(centerlineSeparation, 90, vertical)
109: + ", "
110: + wktForThreeLines(centerlineSeparation, 0, horizontal)
111: + ")");
112: }
113:
114: private static String wktForThreeLines(double centerlineSeparation,
115: double angleInDegrees, boolean enabled) {
116: return enabled ? wktForThreeLines(4 * centerlineSeparation,
117: centerlineSeparation, angleInDegrees) : "POINT EMPTY";
118: }
119:
120: private static String wktForThreeLines(double length,
121: double centerlineSeparation, double angleInDegrees) {
122: AffineTransform transform = new AffineTransform(
123: new Coordinate(), new Coordinate(),
124: new Coordinate(1, 0), new Coordinate(Math.cos(Angle
125: .toRadians(angleInDegrees)), Math.sin(Angle
126: .toRadians(angleInDegrees))));
127:
128: try {
129: return transform.transform(
130: new WKTReader().read("MULTILINESTRING("
131: + ("(" + (-length / 2) + " "
132: + -centerlineSeparation + ", "
133: + (length / 2) + " "
134: + -centerlineSeparation + "), ")
135: + ("(" + (-length / 2) + " " + 0 + ", "
136: + (length / 2) + " " + 0 + "), ")
137: + ("(" + (-length / 2) + " "
138: + centerlineSeparation + ", "
139: + (length / 2) + " "
140: + centerlineSeparation + ") ")
141: + ")")).toText();
142: } catch (Exception e) {
143: Assert.shouldNeverReachHere();
144:
145: return null;
146: }
147: }
148: }
|