Skip to content

Commit c8d74c9

Browse files
committed
Add north arrow styles
1 parent 9033d26 commit c8d74c9

3 files changed

Lines changed: 137 additions & 5 deletions

File tree

src/main/groovy/geoscript/carto/Java2DCartoBuilder.groovy

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.awt.Color
1313
import java.awt.Font
1414
import java.awt.FontMetrics
1515
import java.awt.Graphics2D
16+
import java.awt.Point
1617
import java.awt.Rectangle
1718
import java.awt.font.LineBreakMeasurer
1819
import java.awt.font.TextAttribute
@@ -95,7 +96,11 @@ class Java2DCartoBuilder implements CartoBuilder {
9596

9697
@Override
9798
CartoBuilder northArrow(NorthArrowItem northArrowItem) {
98-
drawNorthArrow(northArrowItem)
99+
if(northArrowItem.style == NorthArrowStyle.North) {
100+
drawNorthArrow(northArrowItem)
101+
} else if(northArrowItem.style == NorthArrowStyle.NorthEastSouthWest) {
102+
drawNESWArrow(northArrowItem)
103+
}
99104
this
100105
}
101106

@@ -146,6 +151,93 @@ class Java2DCartoBuilder implements CartoBuilder {
146151
graphics.draw(path2)
147152
}
148153

154+
private void drawNESWArrow(NorthArrowItem northArrowItem) {
155+
156+
int x = northArrowItem.x
157+
int y = northArrowItem.y
158+
int width = northArrowItem.width
159+
int height = northArrowItem.height
160+
161+
if (northArrowItem.drawText) {
162+
graphics.color = northArrowItem.textColor
163+
graphics.font = northArrowItem.font
164+
FontMetrics fontMetrics = graphics.fontMetrics
165+
int textWidth = [fontMetrics.stringWidth("N"), fontMetrics.stringWidth("E"), fontMetrics.stringWidth("S"), fontMetrics.stringWidth("W")].max()
166+
int textHeight = fontMetrics.height
167+
drawString("N", new Rectangle((width / 2 - textWidth / 2) as int, 0, textWidth, textHeight), HorizontalAlign.CENTER, VerticalAlign.TOP)
168+
drawString("E", new Rectangle(width - textWidth, (height / 2 - textHeight / 2) as int, textWidth, textHeight), HorizontalAlign.RIGHT, VerticalAlign.MIDDLE)
169+
drawString("S", new Rectangle((width / 2 - textWidth / 2) as int, height - textHeight, textWidth, textHeight), HorizontalAlign.CENTER, VerticalAlign.TOP)
170+
drawString("W", new Rectangle(0, (height / 2 - textHeight / 2) as int, textWidth, textHeight), HorizontalAlign.LEFT, VerticalAlign.MIDDLE)
171+
x = x + textWidth
172+
y = y + textHeight
173+
width = width - (textWidth * 2)
174+
height = height - (textHeight * 2)
175+
}
176+
177+
Point north = new Point((x + (width / 2) as int) - 2, y)
178+
Point east = new Point(x + width, y + (height / 2) as int)
179+
Point south = new Point((x + (width / 2) as int) - 2, y + height)
180+
Point west = new Point(x, y + (height / 2) as int)
181+
182+
Point mid = new Point(x + (width / 2) as int, y + (height / 2) as int)
183+
184+
Point northEast = new Point(x + (width * 2/3) as int, y + (height * 1/3) as int)
185+
Point southEast = new Point(x + (width * 2/3) as int, y + (height * 2/3) as int)
186+
Point southWest = new Point(x + (width * 1/3) as int, y + (height * 2/3) as int)
187+
Point northWest = new Point(x + (width * 1/3) as int, y + (height * 1/3) as int)
188+
189+
// Fill
190+
graphics.color = northArrowItem.fillColor1
191+
graphics.fill(createPolygon(north, northEast, mid))
192+
graphics.color = northArrowItem.fillColor2
193+
graphics.fill(createPolygon(north, northWest, mid))
194+
195+
graphics.color = northArrowItem.fillColor1
196+
graphics.fill(createPolygon(east, southEast, mid))
197+
graphics.color = northArrowItem.fillColor2
198+
graphics.fill(createPolygon(east, northEast, mid))
199+
200+
graphics.color = northArrowItem.fillColor1
201+
graphics.fill(createPolygon(south, southWest, mid))
202+
graphics.color = northArrowItem.fillColor2
203+
graphics.fill(createPolygon(south, southEast, mid))
204+
205+
graphics.color = northArrowItem.fillColor1
206+
graphics.fill(createPolygon(west, northWest, mid))
207+
graphics.color = northArrowItem.fillColor2
208+
graphics.fill(createPolygon(west, southWest, mid))
209+
210+
// Stroke
211+
graphics.color = northArrowItem.strokeColor1
212+
graphics.draw(createPolygon(north, northEast, mid))
213+
graphics.color = northArrowItem.strokeColor2
214+
graphics.draw(createPolygon(north, northWest, mid))
215+
216+
graphics.color = northArrowItem.strokeColor1
217+
graphics.draw(createPolygon(east, southEast, mid))
218+
graphics.color = northArrowItem.strokeColor2
219+
graphics.draw(createPolygon(east, northEast, mid))
220+
221+
graphics.color = northArrowItem.strokeColor1
222+
graphics.draw(createPolygon(south, southWest, mid))
223+
graphics.color = northArrowItem.strokeColor2
224+
graphics.draw(createPolygon(south, southEast, mid))
225+
226+
graphics.color = northArrowItem.strokeColor1
227+
graphics.draw(createPolygon(west, northWest, mid))
228+
graphics.color = northArrowItem.strokeColor2
229+
graphics.draw(createPolygon(west, southWest, mid))
230+
}
231+
232+
private GeneralPath createPolygon(Point point1, Point point2, Point point3) {
233+
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 3)
234+
path.moveTo(point1.x as int, point1.y as int)
235+
path.lineTo(point2.x as int, point2.y as int)
236+
path.lineTo(point3.x as int, point3.y as int)
237+
path.closePath()
238+
path
239+
}
240+
149241
@Override
150242
CartoBuilder text(TextItem textItem) {
151243
graphics.color = textItem.color

src/main/groovy/geoscript/carto/NorthArrowItem.groovy

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class NorthArrowItem extends Item {
2525

2626
Color textColor = Color.BLACK
2727

28+
NorthArrowStyle style = NorthArrowStyle.North
29+
2830
/**
2931
* Create a north arrow from the top left with the given width and height.
3032
* @param x The number of pixels from the left
@@ -36,6 +38,16 @@ class NorthArrowItem extends Item {
3638
super(x, y, width, height)
3739
}
3840

41+
/**
42+
* Set the NorthArrowStyle
43+
* @param northArrowStyle The NorthArrowStyle
44+
* @return The NorthArrowItem
45+
*/
46+
NorthArrowItem style(NorthArrowStyle northArrowStyle) {
47+
this.style = northArrowStyle
48+
this
49+
}
50+
3951
/**
4052
* Set the first stroke color
4153
* @param color The first stroke color
@@ -121,7 +133,7 @@ class NorthArrowItem extends Item {
121133
"NorthArrowItem(x = ${x}, y = ${y}, width = ${width}, height = ${height}, " +
122134
"fill-color1 = ${fillColor1}, stroke-color1 = ${strokeColor1}, " +
123135
"fill-color2 = ${fillColor2}, stroke-color2 = ${strokeColor2}, stroke-width = ${strokeWidth}, " +
124-
"draw-text = ${drawText}, font = ${font}, text-color = ${textColor})"
136+
"draw-text = ${drawText}, font = ${font}, text-color = ${textColor}, style = ${style.toString()})"
125137
}
126138

127139
}

src/test/groovy/geoscript/carto/Java2DCartoBuilderTest.groovy

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package geoscript.carto
33
import org.junit.Rule;
44
import org.junit.Test
55
import org.junit.rules.TemporaryFolder
6+
7+
import java.awt.Font
8+
69
import static org.junit.Assert.*
710

811
import javax.imageio.ImageIO
@@ -19,8 +22,33 @@ class Java2DCartoBuilderTest {
1922

2023
@Test
2124
void drawNorthArrow() {
22-
draw(new PageSize(80, 140), "northarrow.png", { Java2DCartoBuilder builder ->
23-
builder.northArrow(new NorthArrowItem(0,0,80,140).drawText(true))
25+
draw(new PageSize(80, 140), "northarrow.png", { PageSize pageSize, Java2DCartoBuilder builder ->
26+
builder.northArrow(new NorthArrowItem(0,0,pageSize.width, pageSize.height))
27+
})
28+
}
29+
30+
@Test
31+
void drawNorthArrowWithText() {
32+
draw(new PageSize(80, 140), "northarrow_text.png", { PageSize pageSize, Java2DCartoBuilder builder ->
33+
builder.northArrow(new NorthArrowItem(0,0,pageSize.width, pageSize.height).drawText(true))
34+
})
35+
}
36+
37+
@Test
38+
void drawNESWArrow() {
39+
draw(new PageSize(200, 200), "northarrow_nesw.png", { PageSize pageSize, Java2DCartoBuilder builder ->
40+
builder.northArrow(new NorthArrowItem(0, 0, pageSize.width, pageSize.height).style(NorthArrowStyle.NorthEastSouthWest))
41+
})
42+
}
43+
44+
@Test
45+
void drawNESWArrowWithText() {
46+
draw(new PageSize(200, 200), "northarrow_nesw_text.png", { PageSize pageSize, Java2DCartoBuilder builder ->
47+
builder.northArrow(new NorthArrowItem(0, 0, pageSize.width, pageSize.height)
48+
.style(NorthArrowStyle.NorthEastSouthWest)
49+
.drawText(true)
50+
.font(new Font("Arial", Font.BOLD, 24))
51+
)
2452
})
2553
}
2654

@@ -32,7 +60,7 @@ class Java2DCartoBuilderTest {
3260
(RenderingHints.KEY_TEXT_ANTIALIASING): RenderingHints.VALUE_TEXT_ANTIALIAS_ON
3361
]
3462
Java2DCartoBuilder builder = new Java2DCartoBuilder(graphics, pageSize)
35-
closure.call(builder)
63+
closure.call(pageSize, builder)
3664
File file = getTempFile(fileName)
3765
ImageIO.write(image, "png", file)
3866
assertTrue(file.exists())

0 commit comments

Comments
 (0)