1 /*
2  * Copyright (c) 2016 Vivid Solutions.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
8  * and the Eclipse Distribution License is available at
9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 
13 package org.locationtech.jtstest.testbuilder.ui.style;
14 
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Graphics2D;
18 import java.awt.Stroke;
19 
20 import org.locationtech.jts.geom.Geometry;
21 import org.locationtech.jtstest.testbuilder.ui.ColorUtil;
22 import org.locationtech.jtstest.testbuilder.ui.Viewport;
23 import org.locationtech.jtstest.testbuilder.ui.render.GeometryPainter;
24 
25 
26 public class BasicStyle implements Style
27 {
28   private Color lineColor;
29   private int lineAlpha = 255;
30   private Color fillColor;
31   private int fillAlpha = 150;
32 
33   private boolean isStroked = true;
34   private boolean isFilled = true;
35   private float strokeWidth = 1;
36   private boolean isDashed = false;
37   private float[] dashes = { 5 };
38 
BasicStyle(Color lineColor, Color fillColor)39   public BasicStyle(Color lineColor, Color fillColor) {
40     this.lineColor = lineColor;
41     this.fillColor = fillColor;
42   }
43 
BasicStyle()44   public BasicStyle() {
45   }
46 
BasicStyle(BasicStyle style)47   public BasicStyle(BasicStyle style) {
48     this.lineColor = style.lineColor;
49     this.lineAlpha = style.lineAlpha;
50     this.fillColor = style.fillColor;
51     this.fillAlpha = style.fillAlpha;
52     this.isStroked = style.isStroked;
53     this.isFilled = style.isFilled;
54     this.strokeWidth = style.strokeWidth;
55     this.isDashed = style.isDashed;
56     this.dashes = style.dashes.clone();
57   }
58 
copy()59   public BasicStyle copy() {
60     return new BasicStyle(this);
61   }
62 
paint(Geometry geom, Viewport viewport, Graphics2D g)63   public void paint(Geometry geom, Viewport viewport, Graphics2D g)
64   {
65     Stroke stroke = createStroke();
66     Color lineClr = (isStroked && stroke != null) ? getLineColor() : null;
67     Color fillClr = isFilled ? getFillColor() : null;
68 
69     GeometryPainter.paint(geom, viewport, g, lineClr, fillClr, stroke);
70   }
71 
createStroke()72   private Stroke createStroke() {
73     if (strokeWidth <= 0) return null;
74 
75     if (! isDashed)
76       return new BasicStroke(strokeWidth);
77 
78     dashes = createDashes(strokeWidth);
79     return new BasicStroke(strokeWidth,
80         BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f,
81         dashes, 0
82     );
83   }
84 
createDashes(float width)85   private float[] createDashes(float width) {
86     float dashSize = 5;
87     float len = 2 * dashSize * width;
88     float dashFrac = 0.5f;
89     return new float[] { (1f - dashFrac) * len, dashFrac * len };
90   }
91 
getLineColor()92   public Color getLineColor() {
93     return ColorUtil.setAlpha(lineColor, lineAlpha);
94   }
95 
setLineColor(Color color)96   public void setLineColor(Color color) {
97     lineColor = color;
98   }
99 
setLineAlpha(int alpha)100   public void setLineAlpha(int alpha) {
101     lineAlpha = alpha;
102   }
103 
getLineAlpha()104   public int getLineAlpha() {
105     return lineAlpha;
106   }
107 
getFillColor()108   public Color getFillColor() {
109     return ColorUtil.setAlpha(fillColor, fillAlpha);
110   }
111 
setFillColor(Color color)112   public void setFillColor(Color color) {
113     fillColor = color;
114   }
115 
setFillAlpha(int alpha)116   public void setFillAlpha(int alpha) {
117     fillAlpha = alpha;
118   }
119 
getFillAlpha()120   public int getFillAlpha() {
121     return fillAlpha;
122   }
123 
isStroked()124   public boolean isStroked() {
125     return isStroked;
126   }
127 
setStroked(boolean isStroked)128   public void setStroked(boolean isStroked) {
129     this.isStroked = isStroked;
130   }
131 
isFilled()132   public boolean isFilled() {
133     return isFilled;
134   }
135 
setFilled(boolean isFilled)136   public void setFilled(boolean isFilled) {
137     this.isFilled = isFilled;
138   }
139 
getStrokeWidth()140   public float getStrokeWidth() {
141     return strokeWidth;
142   }
143 
setStrokeWidth(float width)144   public void setStrokeWidth(float width) {
145     strokeWidth = width;
146   }
147 
isDashed()148   public boolean isDashed() {
149     return isDashed;
150   }
151 
setDashed(boolean isDashed)152   public void setDashed(boolean isDashed) {
153     this.isDashed = isDashed;
154   }
155 
getDashes()156   public float[] getDashes() {
157     return dashes;
158   }
159 
setDashes(float[] dashArray)160   public void setDashes(float[] dashArray) {
161     this.dashes = dashArray;
162   }
163 
164 
165 }
166