001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/graphics/DefaultScaleBar.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.graphics;
045:
046: import java.awt.Color;
047: import java.awt.Font;
048: import java.awt.Graphics;
049: import java.awt.Rectangle;
050: import java.text.DecimalFormat;
051: import java.text.NumberFormat;
052:
053: /**
054: *
055: *
056: *
057: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
058: * @author last edited by: $Author: apoth $
059: *
060: * @version $Revision: 9340 $, $Date: 2007-12-27 04:32:12 -0800 (Thu, 27 Dec 2007) $
061: */
062: public class DefaultScaleBar implements ScaleBar {
063: private Color barColor;
064:
065: private Color labelColor;
066:
067: private Color bgColor;
068:
069: private Font barFont;
070:
071: private String barStyle;
072:
073: private String unit;
074:
075: private double scale;
076:
077: private int bottomLabel;
078:
079: private double scaleDenominator;
080:
081: private int topLabel;
082:
083: private NumberFormat numberFormat;
084:
085: /**
086: * Constructor with all Parameters
087: *
088: * @param topLabel
089: * type of the label on top of the scale bar. Chose L_NONE or no label, L_SCALE for
090: * scale on top and L_SCALEDENOMIATOR for scaledenominator on top.
091: * @param bottomLabel
092: * the same as above but below the scalebar.
093: * @param scale
094: * the scale to be displayed. For a value of e.g. 1000 there will be 1:1000 displayed
095: * @param scaleDenominator
096: * the scaledenominator to be displayed
097: * @param units
098: * the units the scaledenominator is in. Possible values are inch, Meter, Mile,
099: * Kilometer...
100: * @param labelColor
101: * the Color the label has to be in (and of course the text below and above)
102: * @param barColor
103: * @param allgColor
104: * not used so far
105: * @param barStyle
106: * the style the bar appears in. Currently just "default" is supported.
107: * @param barFont
108: * the font the text above and below the scale bar appears in.
109: * @param numberFormat
110: *
111: */
112: public DefaultScaleBar(int topLabel, int bottomLabel, double scale,
113: double scaleDenominator, String units, Color labelColor,
114: Color barColor, Color allgColor, String barStyle,
115: Font barFont, NumberFormat numberFormat) {
116: setTopLabel(topLabel);
117: setBottomLabel(bottomLabel);
118: setScale(scale);
119: setScaleDenominator(scaleDenominator);
120: setColor(allgColor);
121: setBarColor(barColor);
122: setLabelColor(labelColor);
123: setStyle(barStyle);
124: setFont(barFont);
125: setUnits(units);
126: setNumberFormat(numberFormat);
127: }
128:
129: /**
130: *
131: * Constructor just using defaults
132: *
133: */
134: public DefaultScaleBar() {
135: this (L_SCALEDENOMINATOR, L_SCALE, 40000, 100, "Meter",
136: Color.GREEN, Color.BLUE, Color.BLACK, "default",
137: new Font("default", Font.PLAIN, 12), new DecimalFormat(
138: "###,###,###"));
139: }
140:
141: /**
142: * will paint the scale bar to the passed graphic context
143: *
144: * @param g
145: * graphic context
146: */
147: public void paint(Graphics g) { // throws Exception {
148:
149: Rectangle rect = g.getClipBounds();
150: if (rect != null && rect.getWidth() > 0 && rect.getHeight() > 0) {
151: g.setColor(bgColor);
152: g.fillRect(0, 0, (int) rect.getWidth(), (int) rect
153: .getHeight());
154: }
155:
156: // if ( toplabel + bottomlabel < 1 ) throw Exception
157: g.setColor(barColor);
158:
159: int width = g.getClipBounds().width;
160: int height = g.getClipBounds().height;
161: g.setFont(barFont);
162:
163: int laenge;
164:
165: if (barStyle.equals("default")) {
166: g
167: .drawLine(0, (height / 2) + 1, width - 1,
168: (height / 2) + 1);
169: g.drawLine(0, height / 2, width - 1, height / 2);
170: g
171: .drawLine(0, (height / 2) - 1, width - 1,
172: (height / 2) - 1);
173: g.drawLine(0, (height / 2) + 10, 0, (height / 2) - 10);
174: g.drawLine(width - 1, (height / 2) + 10, width - 1,
175: (height / 2) - 10);
176: }
177:
178: String strScale = numberFormat.format(scale);
179: String strScaleDen = numberFormat.format(scaleDenominator);
180:
181: g.setColor(labelColor);
182: switch (topLabel) {
183: case -1:
184: break;
185: case 0:
186: laenge = g.getFontMetrics().stringWidth(
187: strScale + " " + unit);
188: g.drawString(strScale + " " + unit, (width - laenge) / 2,
189: (height / 2) - 6);
190: break;
191: case 1:
192: laenge = g.getFontMetrics().stringWidth(
193: "1 : " + strScaleDen);
194: g.drawString("1 : " + strScaleDen, (width - laenge) / 2,
195: (height / 2) - 6);
196: break;
197: }
198:
199: switch (bottomLabel) {
200: case -1:
201: break;
202: case 0:
203: laenge = g.getFontMetrics().stringWidth(
204: strScale + " " + unit);
205: g.drawString(strScale + " " + unit, (width - laenge) / 2,
206: (height / 2) + 1 + barFont.getSize());
207: break;
208: case 1:
209: laenge = g.getFontMetrics().stringWidth(
210: "1 : " + strScaleDen);
211: g.drawString("1 : " + strScaleDen, (width - laenge) / 2,
212: (height / 2) + 1 + barFont.getSize());
213: break;
214: }
215: }
216:
217: /**
218: * sets the type of the label above the scale bar
219: *
220: * @param labelType
221: * lable type
222: *
223: */
224: public void setTopLabel(int labelType) {
225: switch (labelType) {
226: case -1:
227: topLabel = -1;
228: break;
229: case 0:
230: topLabel = 0;
231: break;
232: case 1:
233: topLabel = 1;
234: break;
235: }
236: }
237:
238: /**
239: * sets the type of the label below the scale bar
240: *
241: * @param labelType
242: * lable type
243: *
244: */
245: public void setBottomLabel(int labelType) {
246: switch (labelType) {
247: case -1:
248: bottomLabel = -1;
249: break;
250: case 0:
251: bottomLabel = 0;
252: break;
253: case 1:
254: bottomLabel = 1;
255: break;
256: }
257: }
258:
259: /**
260: * sets the scale as defined in the OGC WMS 1.1.1 specification. Scale is defined as the
261: * diagonal size of a pixel in the center of a map measured in meter. The setting of the scale
262: * will affect the value of the scale denominator
263: *
264: * @param scale
265: * map scale
266: *
267: */
268: public void setScale(double scale) {
269: this .scale = scale;
270: }
271:
272: /**
273: * sets the scale denominator for the scale bar. The scale denominator is the scale expression
274: * as we know it for printed maps (e.g. 1:10000 1:5000). The passed value is expressed in
275: * meters. The setting of the scale denominator will affect the value of the scale
276: *
277: * @param scaleDen
278: * scale denominator value
279: *
280: */
281: public void setScaleDenominator(double scaleDen) {
282: scaleDenominator = scaleDen;
283: }
284:
285: /**
286: * sets the units the scale and the scale denominater will be expressed at. Settings other than
287: * meter will cause that the passed values for scale and scale denominater will be recalculated
288: * for painting. it depends on the implementation what units are supported.
289: *
290: * @param units
291: * name units (meter, miles, feet etc.)
292: */
293: public void setUnits(String units) {
294: unit = units;
295: }
296:
297: /**
298: * sets the front color of the scale bar
299: *
300: * @param color
301: */
302: public void setColor(Color color) {
303: bgColor = color;
304: }
305:
306: /**
307: * sets the label color of the scale bar
308: *
309: * @param color
310: *
311: */
312: public void setLabelColor(Color color) {
313: labelColor = color;
314: }
315:
316: /**
317: * sets the bar color of the scale bar
318: *
319: * @param color
320: *
321: */
322: public void setBarColor(Color color) {
323: barColor = color;
324: }
325:
326: /**
327: * sets the style of the scale bar. default style is |--------| the list of known styles depends
328: * on the implementation
329: *
330: * @param style
331: * style name
332: */
333: public void setStyle(String style) {
334: barStyle = style;
335: }
336:
337: /**
338: * sets the font for label rendering
339: *
340: * @param font
341: * awt font object
342: */
343: public void setFont(Font font) {
344: barFont = font;
345: }
346:
347: /*
348: * (non-Javadoc)
349: *
350: * @see org.deegree.graphics.ScaleBar#setBackgroundColor(java.awt.Color)
351: */
352: public void setBackgroundColor(Color color) {
353: this .bgColor = color;
354:
355: }
356:
357: /**
358: * sets the number format for a scale bar
359: *
360: * @param numberFormat
361: */
362: public void setNumberFormat(NumberFormat numberFormat) {
363: this.numberFormat = numberFormat;
364: }
365: }
|