Skip to content

Commit 0de2f82

Browse files
authored
Merge pull request #76 from geoscript/GSG-73
Add new properties to the ScaleBarItem.
2 parents c5d9320 + 189f243 commit 0de2f82

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,12 @@ class Java2DCartoBuilder implements CartoBuilder {
348348
int height = scaleBarItem.height
349349
int border = 5
350350
Font font = scaleBarItem.font
351+
Color fontColor = scaleBarItem.textColor
351352
Color strokeColor = scaleBarItem.strokeColor
352353
Color fillColor = scaleBarItem.fillColor
353354
int strokeWidth = scaleBarItem.strokeWidth
355+
Color barStrokeColor = scaleBarItem.barStrokeColor
356+
float barStrokeWidth = scaleBarItem.barStrokeWidth
354357
int ticHeight = 10
355358

356359
ScaleBarItem.ScaleBarInfo scaleBarInfo = scaleBarItem.calculateScaleBarInfo()
@@ -366,6 +369,7 @@ class Java2DCartoBuilder implements CartoBuilder {
366369
int lineXEnd = (int)(lineXStart + scaleBarInfo.widthInPixels)
367370
int lineY = y + height - border
368371

372+
// Outline
369373
if (fillColor) {
370374
graphics.color = fillColor
371375
graphics.fillRect(lineXStart - border, y, lineXEnd - lineXStart + border * 2, height)
@@ -374,9 +378,13 @@ class Java2DCartoBuilder implements CartoBuilder {
374378
graphics.stroke = new BasicStroke(strokeWidth)
375379
graphics.drawRect(lineXStart - border, y, lineXEnd - lineXStart + border * 2, height)
376380

381+
// Scale bar
382+
graphics.color = barStrokeColor
383+
graphics.stroke = new BasicStroke(barStrokeWidth)
377384
graphics.drawLine(lineXStart, lineY, lineXEnd, lineY)
378385
graphics.drawLine(lineXStart, y + height - border, lineXStart, y + height - border - ticHeight)
379386
graphics.drawLine(lineXEnd, y + height - border, lineXEnd, y + height - border - ticHeight)
387+
graphics.color = fontColor
380388
graphics.font = font
381389
String scaleText = "${(int) scaleBarInfo.widthInUnits} ${scaleBarInfo.unitForScaleText}"
382390
drawString(scaleText, new Rectangle(

src/main/groovy/geoscript/carto/ScaleBarItem.groovy

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ class ScaleBarItem extends Item {
2020

2121
float strokeWidth = 1
2222

23+
float barStrokeWidth = 1
24+
25+
Color barStrokeColor = Color.BLACK
26+
2327
Font font = new Font("Arial", Font.PLAIN, 12)
2428

29+
Color textColor = Color.BLACK
30+
2531
int border = 5
2632

2733
Units units = Units.METRIC
@@ -92,6 +98,36 @@ class ScaleBarItem extends Item {
9298
this
9399
}
94100

101+
/**
102+
* Set the bar stroke Color
103+
* @param strokeColor The bar stroke Color
104+
* @return The ScaleBarItem
105+
*/
106+
ScaleBarItem barStrokeColor(Color strokeColor) {
107+
this.barStrokeColor = strokeColor
108+
this
109+
}
110+
111+
/**
112+
* Set the bar stroke width
113+
* @param strokeWidth The bar stroke width
114+
* @return The ScaleBarItem
115+
*/
116+
ScaleBarItem barStrokeWidth(float strokeWidth) {
117+
this.barStrokeWidth = strokeWidth
118+
this
119+
}
120+
121+
/**
122+
* Set the text Color
123+
* @param color The text Color
124+
* @return The ScaleBarItem
125+
*/
126+
ScaleBarItem textColor(Color color) {
127+
this.textColor = color
128+
this
129+
}
130+
95131
/**
96132
* Set the Font
97133
* @param font The Font

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ class Java2DCartoBuilderTest {
5353
})
5454
}
5555

56+
@Test
57+
void drawScaleBarWithOptions() {
58+
File fileForShapefile = new File(getClass().getClassLoader().getResource("states.shp").toURI())
59+
Shapefile shapefile = new Shapefile(fileForShapefile)
60+
Map map = new Map(layers: [shapefile])
61+
draw(new PageSize(400, 300), "scalebar_options.png", { PageSize pageSize, Java2DCartoBuilder builder ->
62+
builder
63+
.map(new MapItem(0,0,400,300).map(map))
64+
.scaleBar(new ScaleBarItem(10,10,200,20)
65+
.map(map)
66+
.units(ScaleBarItem.Units.US)
67+
.barStrokeWidth(2.0f)
68+
.barStrokeColor(java.awt.Color.RED)
69+
.textColor(java.awt.Color.BLUE)
70+
)
71+
})
72+
}
73+
5674
@Test
5775
void drawNorthArrow() {
5876
draw(new PageSize(80, 140), "northarrow.png", { PageSize pageSize, Java2DCartoBuilder builder ->

src/test/groovy/geoscript/carto/ScaleBarItemTest.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ class ScaleBarItemTest {
2020

2121
ScaleBarItem item = new ScaleBarItem(10,20,300,50)
2222
.font(new Font("Verdana", Font.BOLD, 14))
23+
.textColor(Color.BLACK)
2324
.strokeColor(Color.BLUE)
2425
.fillColor(Color.GRAY)
2526
.strokeWidth(1.45f)
27+
.barStrokeColor(Color.BLUE)
28+
.barStrokeWidth(1.2f)
2629
.border(6)
2730
.units(ScaleBarItem.Units.US)
2831
.map(map)
@@ -35,8 +38,11 @@ class ScaleBarItemTest {
3538
assertEquals(item.font.name, "Verdana")
3639
assertEquals(item.font.style, Font.BOLD)
3740
assertEquals(item.font.size, 14)
41+
assertEquals(item.textColor, Color.BLACK)
3842
assertEquals(item.strokeColor, Color.BLUE)
3943
assertEquals(item.fillColor, Color.GRAY)
44+
assertEquals(item.barStrokeColor, Color.BLUE)
45+
assertEquals(item.barStrokeWidth, 1.2f)
4046
assertEquals(item.border, 6)
4147
assertEquals(item.strokeWidth, 1.45f, 0.01f)
4248
assertEquals(ScaleBarItem.Units.US, item.units)

0 commit comments

Comments
 (0)