Source Code Cross Referenced for LegendElement.java in  » GIS » deegree » org » deegree » graphics » legend » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » deegree » org.deegree.graphics.legend 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/graphics/legend/LegendElement.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.legend;
045:
046:        import java.awt.BasicStroke;
047:        import java.awt.Color;
048:        import java.awt.FontMetrics;
049:        import java.awt.Graphics;
050:        import java.awt.Graphics2D;
051:        import java.awt.image.BufferedImage;
052:        import java.util.ArrayList;
053:
054:        import org.deegree.framework.log.ILogger;
055:        import org.deegree.framework.log.LoggerFactory;
056:        import org.deegree.graphics.displayelements.DisplayElementFactory;
057:        import org.deegree.graphics.displayelements.IncompatibleGeometryTypeException;
058:        import org.deegree.graphics.displayelements.PolygonDisplayElement;
059:        import org.deegree.graphics.sld.LineSymbolizer;
060:        import org.deegree.graphics.sld.PointSymbolizer;
061:        import org.deegree.graphics.sld.PolygonSymbolizer;
062:        import org.deegree.graphics.sld.RasterSymbolizer;
063:        import org.deegree.graphics.sld.Rule;
064:        import org.deegree.graphics.sld.Symbolizer;
065:        import org.deegree.graphics.sld.TextSymbolizer;
066:        import org.deegree.graphics.transformation.WorldToScreenTransform;
067:        import org.deegree.model.filterencoding.FilterEvaluationException;
068:        import org.deegree.model.spatialschema.Envelope;
069:        import org.deegree.model.spatialschema.GeometryFactory;
070:        import org.deegree.model.spatialschema.Position;
071:        import org.deegree.model.spatialschema.Surface;
072:        import org.deegree.ogcwebservices.wms.GraphicContextFactory;
073:
074:        /**
075:         * The implements the basic legend element. a legend element may has a label that can be set to
076:         * eight positions relative to the legend graphic. A <tt>LegendElement</tt> can be activated or
077:         * deactivated. It depends on the using application what effect this behavior will have.
078:         * <p>
079:         * <tt>LegendElement</tt>s can be collected in a <tt>LegendElementCollection</tt> which also is
080:         * a <tt>LegendElement</tt> to group elements or to create more complex elements.
081:         * <p>
082:         * Each <tt>LegendElement</tt> is able to paint itself as <tt>BufferedImage</tt>
083:         * 
084:         * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
085:         * @version $Revision: 7363 $ $Date: 2007-05-29 20:47:55 +0200 (Di, 29 Mai 2007) $
086:         */
087:        public class LegendElement {
088:
089:            private static final ILogger LOG = LoggerFactory
090:                    .getLogger(LegendElement.class);
091:
092:            protected ArrayList<Rule> ruleslist = null;
093:
094:            protected String label = "";
095:
096:            protected double orientation = 0;
097:
098:            protected int labelPosition = -1;
099:
100:            protected boolean active = false;
101:
102:            protected int width = 0;
103:
104:            protected int height = 0;
105:
106:            protected int bufferBetweenLegendAndLabel = 10;
107:
108:            protected BufferedImage bi;
109:
110:            /**
111:             * empty constructor
112:             * 
113:             */
114:            protected LegendElement() {
115:                this .ruleslist = new ArrayList<Rule>();
116:            }
117:
118:            /**
119:             * 
120:             * 
121:             */
122:            LegendElement(BufferedImage legendImage) {
123:                this ();
124:                bi = legendImage;
125:            }
126:
127:            /**
128:             * constructor
129:             * 
130:             * @param rules
131:             *            the different rules from the SLD
132:             * @param label
133:             *            the label beneath the legend symbol
134:             * @param orientation
135:             *            the rotation of the text in the legend
136:             * @param labelPosition
137:             *            the position of the text according to the symbol
138:             * @param active
139:             *            whether the legendsymbol is active or not
140:             * @param width
141:             *            the requested width of the legend symbol
142:             * @param height
143:             *            the requested height of the legend symbol
144:             */
145:            LegendElement(Rule[] rules, String label, double orientation,
146:                    int labelPosition, boolean active, int width, int height) {
147:                this ();
148:                setRules(rules);
149:                setLabel(label);
150:                setLabelOrientation(orientation);
151:                setLabelPlacement(labelPosition);
152:                setActive(active);
153:                setWidth(width);
154:                setHeight(height);
155:            }
156:
157:            /**
158:             * gets the Rules as an array
159:             * 
160:             * @return array of sld rules
161:             */
162:            public Rule[] getRules() {
163:                if (ruleslist != null && ruleslist.size() > 0) {
164:                    return ruleslist.toArray(new Rule[ruleslist.size()]);
165:                }
166:                return null;
167:            }
168:
169:            /**
170:             * adds a rule to the ArrayList ruleslist
171:             * 
172:             * @param rule
173:             *            a sld rule
174:             */
175:            public void addRule(Rule rule) {
176:                this .ruleslist.add(rule);
177:            }
178:
179:            /**
180:             * sets the rules
181:             * 
182:             * @param rules
183:             *            an array of sld rules
184:             */
185:            public void setRules(Rule[] rules) {
186:                this .ruleslist.clear();
187:
188:                if (rules != null) {
189:                    for (int i = 0; i < rules.length; i++) {
190:                        this .ruleslist.add(rules[i]);
191:                    }
192:                }
193:            }
194:
195:            /**
196:             * sets the label of the <tt>LegendElement</tt>
197:             * 
198:             * @param label
199:             *            label of the <tt>LegendElement</tt>
200:             * 
201:             */
202:            public void setLabel(String label) {
203:                this .label = label;
204:            }
205:
206:            /**
207:             * returns the label set to <tt>LegendElement</tt>. If no label is set, the method returns
208:             * <tt>null</tt>
209:             * 
210:             * @return label of the <tt>LegendElement</tt> or <tt>null</tt>
211:             * 
212:             */
213:            public String getLabel() {
214:                return this .label;
215:            }
216:
217:            /**
218:             * sets the orientation of the label of the <tt>LegendElement</tt>. A label can have an
219:             * orientation from -90� to 90� expressed in radians, where 0� is horizontal
220:             * 
221:             * @param orientation
222:             */
223:            public void setLabelOrientation(double orientation) {
224:                this .orientation = orientation;
225:            }
226:
227:            /**
228:             * returns the current orientation of the label of the <tt>LegendElement</tt> in radians. If
229:             * the element hasn't a label <tt>Double.NEGATIVE_INFINITY</tt> will be returned.
230:             * 
231:             * @return orientation of the label of the <tt>LegendElement</tt> in radians
232:             */
233:            public double getLabelOrientation() {
234:                return this .orientation;
235:            }
236:
237:            /**
238:             * sets the placement of the label relative to the legend symbol. Possible values are:
239:             * <ul>
240:             * <li>LP_TOPCENTER
241:             * <li>LP_TOPLEFT
242:             * <li>LP_TOPRIGHT
243:             * <li>LP_RIGHT
244:             * <li>LP_LEFT
245:             * <li>LP_BOTTOMCENTER
246:             * <li>LP_BOTTOMRIGHT
247:             * <li>LP_BOTTOMLEFT
248:             * </ul>
249:             * 
250:             * <pre>
251:             *   +---+---+---+
252:             *   | 1 | 0 | 2 |
253:             *   +---+---+---+
254:             *   | 4 |LEG| 3 |
255:             *   +---+---+---+
256:             *   | 7 | 5 | 6 |
257:             *   +---+---+---+
258:             * </pre>
259:             * 
260:             * An implementation of the interface may not supoort all positions.
261:             * 
262:             * @param labelPosition
263:             */
264:            public void setLabelPlacement(int labelPosition) {
265:                this .labelPosition = labelPosition;
266:            }
267:
268:            /**
269:             * returns the placement of the label relative to the legend symbol. If the element hasn't a
270:             * label <tt>LegendElement.LP_NOLABEL</tt> will be returned. Otherwise possible values are:
271:             * <ul>
272:             * <li>LP_TOPCENTER
273:             * <li>LP_TOPLEFT
274:             * <li>LP_TOPRIGHT
275:             * <li>LP_RIGHT
276:             * <li>LP_LEFT
277:             * <li>LP_BOTTOMCENTER
278:             * <li>LP_BOTTOMRIGHT
279:             * <li>LP_BOTTOMLEFT
280:             * </ul>
281:             * 
282:             * @return coded placement of the label relative to the legend symbol
283:             */
284:            public int getLabelPlacement() {
285:                return this .labelPosition;
286:            }
287:
288:            /**
289:             * activates or deactivates the label
290:             * 
291:             * @param active
292:             */
293:            public void setActive(boolean active) {
294:                this .active = active;
295:            }
296:
297:            /**
298:             * @return the activtion-status of the label
299:             */
300:            public boolean isActive() {
301:                return this .active;
302:            }
303:
304:            /**
305:             * sets the width of the LegendSymbol (in pixels)
306:             * 
307:             * @param width
308:             */
309:            public void setWidth(int width) {
310:                this .width = width;
311:            }
312:
313:            /**
314:             * @return the width of the LegendSymbol (in pixels)
315:             */
316:            public int getWidth() {
317:                return this .width;
318:            }
319:
320:            /**
321:             * sets the height of the LegendSymbol (in pixels)
322:             * 
323:             * @param height
324:             */
325:            public void setHeight(int height) {
326:                this .height = height;
327:            }
328:
329:            /**
330:             * @return the height of the LegendSymbol (in pixels)
331:             */
332:            public int getHeight() {
333:                return this .height;
334:            }
335:
336:            /**
337:             * returns the buffer place between the legend symbol and the legend label in pixels
338:             * 
339:             * @return the buffer as integer in pixels
340:             */
341:            public int getBufferBetweenLegendAndLabel() {
342:                return this .bufferBetweenLegendAndLabel;
343:            }
344:
345:            /**
346:             * @see org.deegree.graphics.legend.LegendElement#getBufferBetweenLegendAndLabel()
347:             * @param i
348:             *            the buffer as integer in pixels
349:             */
350:            public void setBufferBetweenLegendAndLabel(int i) {
351:                this .bufferBetweenLegendAndLabel = i;
352:            }
353:
354:            /**
355:             * draws a legendsymbol, if the SLD defines a point
356:             * 
357:             * @param g
358:             *            the graphics context
359:             * @param c
360:             *            the PointSymbolizer representing the drawable point
361:             * @param width
362:             *            the requested width of the symbol
363:             * @param height
364:             *            the requested height of the symbol
365:             * @throws LegendException
366:             *             is thrown, if the parsing of the sld failes.
367:             */
368:            protected void drawPointLegend(Graphics g, PointSymbolizer c,
369:                    int width, int height) throws LegendException {
370:                org.deegree.graphics.sld.Graphic deegreegraphic = c
371:                        .getGraphic();
372:                try {
373:                    BufferedImage buffi = deegreegraphic.getAsImage(null);
374:                    int w = buffi.getWidth();
375:                    int h = buffi.getHeight();
376:                    g.drawImage(buffi, width / 2 - w / 2, height / 2 - h / 2,
377:                            null);
378:                } catch (FilterEvaluationException feex) {
379:                    throw new LegendException(
380:                            "FilterEvaluationException occured during "
381:                                    + "the creation of the legend:\n"
382:                                    + "The legend for the PointSymbol can't be processed.\n"
383:                                    + feex.getMessage());
384:                }
385:
386:            }
387:
388:            /**
389:             * draws a legendsymbol, if the SLD defines a line
390:             * 
391:             * @param g
392:             *            the graphics context
393:             * @param ls
394:             *            the LineSymbolizer representing the drawable line
395:             * @param width
396:             *            the requested width of the symbol
397:             * @param height
398:             *            the requested height of the symbol
399:             * @throws LegendException
400:             *             is thrown, if the parsing of the sld failes.
401:             */
402:            protected void drawLineStringLegend(Graphics2D g,
403:                    LineSymbolizer ls, int width, int height)
404:                    throws LegendException {
405:
406:                org.deegree.graphics.sld.Stroke sldstroke = ls.getStroke();
407:                try {
408:                    // color, opacity
409:                    setColor(g, sldstroke.getStroke(null), sldstroke
410:                            .getOpacity(null));
411:                    g.setStroke(getBasicStroke(sldstroke));
412:                } catch (FilterEvaluationException feex) {
413:                    throw new LegendException(
414:                            "FilterEvaluationException occured during the creation "
415:                                    + "of the legend:\n The legend for the LineSymbol can't be "
416:                                    + "processed.\n" + feex.getMessage());
417:                }
418:
419:                // p1 = [0 | height]
420:                // p2 = [width / 3 | height / 3]
421:                // p3 = [width - width / 3 | height - height / 3]
422:                // p4 = [width | 0]
423:                int[] xPoints = { 0, width / 3, width - width / 3, width };
424:                int[] yPoints = { height, height / 3, height - height / 3, 0 };
425:                int nPoints = 4;
426:
427:                g.drawPolyline(xPoints, yPoints, nPoints);
428:
429:            }
430:
431:            /**
432:             * draws a legendsymbol, if the SLD defines a polygon
433:             * 
434:             * @param g
435:             *            the graphics context
436:             * @param ps
437:             *            the PolygonSymbolizer representing the drawable polygon
438:             * @param width
439:             *            the requested width of the symbol
440:             * @param height
441:             *            the requested height of the symbol
442:             * @throws LegendException
443:             *             if the parsing of the sld failes.
444:             */
445:            protected void drawPolygonLegend(Graphics2D g,
446:                    PolygonSymbolizer ps, int width, int height)
447:                    throws LegendException {
448:
449:                Position p1 = GeometryFactory.createPosition(0, 0);
450:                Position p2 = GeometryFactory.createPosition(0, height - 1);
451:                Position p3 = GeometryFactory.createPosition(width - 1,
452:                        height - 1);
453:                Position p4 = GeometryFactory.createPosition(width - 1, 0);
454:
455:                Position[] pos = { p1, p2, p3, p4, p1 };
456:                Surface surface = null;
457:                try {
458:                    surface = GeometryFactory.createSurface(pos, null, null,
459:                            null);
460:                } catch (Exception ex) {
461:                    throw new LegendException(
462:                            "Exception occured during the creation of the legend:\n"
463:                                    + "The legendsymbol for the Polygon can't be processed.\n"
464:                                    + "Error in creating the Surface from the Positions.\n"
465:                                    + ex.getMessage());
466:                }
467:
468:                PolygonDisplayElement pde = null;
469:                try {
470:                    // Feature, Geometry, PolygonSymbolizer
471:                    pde = DisplayElementFactory.buildPolygonDisplayElement(
472:                            null, surface, ps);
473:                } catch (IncompatibleGeometryTypeException igtex) {
474:                    throw new LegendException(
475:                            "IncompatibleGeometryTypeException occured during "
476:                                    + "the creation of the legend:\n The legendsymbol for "
477:                                    + "the Polygon can't be processed.\nError in creating "
478:                                    + "the PolygonDisplayElement.\n"
479:                                    + igtex.getMessage());
480:                } catch (Exception e) {
481:                    throw new LegendException("Could not create symbolizer:\n"
482:                            + e.getMessage());
483:                }
484:
485:                Envelope envelope = GeometryFactory
486:                        .createEnvelope(p1, p3, null);
487:
488:                WorldToScreenTransform wtst = new WorldToScreenTransform(
489:                        envelope, envelope);
490:                pde.paint(g, wtst, -1);
491:
492:            }
493:
494:            /**
495:             * sets the color including an opacity
496:             * 
497:             * @param g2
498:             *            the graphics contect as Graphics2D
499:             * @param color
500:             *            the requested color of the legend symbol
501:             * @param opacity
502:             *            the requested opacity of the legend symbol
503:             * @return the Graphics2D object containing color and opacity
504:             */
505:            private Graphics2D setColor(Graphics2D g2, Color color,
506:                    double opacity) {
507:                if (opacity < 0.999) {
508:                    final int alpha = (int) Math.round(opacity * 255);
509:                    final int red = color.getRed();
510:                    final int green = color.getGreen();
511:                    final int blue = color.getBlue();
512:                    color = new Color(red, green, blue, alpha);
513:                }
514:                g2.setColor(color);
515:                return g2;
516:            }
517:
518:            /**
519:             * constructs a java.awt.BasicStroke for painting a LineString legend symbol.
520:             * 
521:             * @param sldstroke
522:             *            the deegree sld stroke
523:             * @return a java.awt.BasicStroke
524:             * @throws LegendException
525:             *             if the sld cannot be processed
526:             */
527:            private BasicStroke getBasicStroke(
528:                    org.deegree.graphics.sld.Stroke sldstroke)
529:                    throws LegendException {
530:                BasicStroke bs = null;
531:                try {
532:                    float width = (float) sldstroke.getWidth(null);
533:                    int cap = sldstroke.getLineCap(null);
534:                    int join = sldstroke.getLineJoin(null);
535:                    float miterlimit = 1f;
536:                    float[] dash = sldstroke.getDashArray(null);
537:                    float dash_phase = sldstroke.getDashOffset(null);
538:
539:                    bs = new BasicStroke(width, cap, join, miterlimit, dash,
540:                            dash_phase);
541:                    // return new BasicStroke((float)sldstroke.getWidth(null), sldstroke.getLineCap(null),
542:                    // sldstroke.getLineJoin(null), 1f, sldstroke.getDashArray(null),
543:                    // sldstroke.getDashOffset(null));
544:
545:                } catch (FilterEvaluationException ex) {
546:                    throw new LegendException(
547:                            "FilterEvaluationException occured during the creation of the legend:\n"
548:                                    + "The Stroke of the element can't be processed.\n"
549:                                    + ex.getMessage());
550:                }
551:                return bs;
552:            }
553:
554:            /**
555:             * calculates the FontMetrics of the LegendLabel in pixels. It returns an 3-dimensional array
556:             * containing [0] the width, [1] the ascent and [2] the descent.
557:             * 
558:             * @param label
559:             *            the label of the LegendElement
560:             * @return the 3-dimensional INT-Array contains [0] the width of the string, [1] the ascent and
561:             *         [2] the descent.
562:             */
563:            protected int[] calculateFontMetrics(String label) {
564:                int[] fontmetrics = new int[3];
565:
566:                BufferedImage bi = new BufferedImage(1, 1,
567:                        BufferedImage.TYPE_INT_ARGB);
568:                Graphics g = bi.getGraphics();
569:
570:                FontMetrics fm = g.getFontMetrics();
571:
572:                if (label != null && label.length() > 0) {
573:                    fontmetrics[0] = fm.stringWidth(label);
574:                    fontmetrics[1] = fm.getAscent();
575:                    fontmetrics[2] = fm.getDescent();
576:                } else {
577:                    fontmetrics[0] = 0;
578:                    fontmetrics[1] = 0;
579:                    fontmetrics[2] = 0;
580:                    // value = 1, because of a bug, which doesn't paint the right border of the
581:                    // polygon-symbol.
582:                    setBufferBetweenLegendAndLabel(1);
583:                }
584:                g.dispose();
585:
586:                return fontmetrics;
587:            }
588:
589:            /**
590:             * calculates the width and height of the resulting LegendSymbol depending on the LabelPlacement
591:             */
592:            private BufferedImage calculateImage(int labelposition,
593:                    int labelwidth, int ascent, int descent, int legendwidth,
594:                    int legendheight, int buffer, String mime) {
595:                // eliminate buffer if label width is zero, so pixel counting works for the reference
596:                // implementation
597:                if (labelwidth == 0) {
598:                    buffer = 0;
599:                }
600:
601:                BufferedImage bi = (BufferedImage) GraphicContextFactory
602:                        .createGraphicTarget(mime,
603:                                (legendwidth + buffer + labelwidth),
604:                                legendheight);
605:
606:                Graphics g = bi.getGraphics();
607:                if (!(mime.equalsIgnoreCase("image/png") || mime
608:                        .equalsIgnoreCase("image/gif"))) {
609:                    g.setColor(Color.WHITE);
610:                    g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
611:                }
612:                g.setColor(Color.BLACK);
613:                // TODO labelposition
614:
615:                switch (labelposition) {
616:                // LP_TOPCENTER
617:                case 0: {
618:                    LOG
619:                            .logInfo("The text-position LP_TOPCENTER in the legend is not "
620:                                    + "implemented yet.\n We put the text on the right side (EAST) of "
621:                                    + "the legendsymbol.");
622:                    g.drawString(getLabel(), width + 10, height / 2
623:                            + ((ascent - descent) / 2));
624:                    break;
625:                }
626:                    // LP_TOPLEFT
627:                case 1: {
628:                    LOG
629:                            .logInfo("The text-position LP_TOPLEFT in the legend is not implemented "
630:                                    + "yet.\n We put the text on the right side (EAST) of the legendsymbol.");
631:                    g.drawString(getLabel(), width + 10, height / 2
632:                            + ((ascent - descent) / 2));
633:                    break;
634:                }
635:                    // LP_TOPRIGHT
636:                case 2: {
637:                    LOG
638:                            .logInfo("The text-position LP_TOPRIGHT in the legend is not implemented "
639:                                    + "yet.\n We put the text on the right side (EAST) of the legendsymbol.");
640:                    g.drawString(getLabel(), width + 10, height / 2
641:                            + ((ascent - descent) / 2));
642:                    break;
643:                }
644:                    // LP_RIGHT
645:                case 3: {
646:                    LOG
647:                            .logInfo("The text-position LP_RIGHT in the legend is not implemented "
648:                                    + "yet.\n We put the text on the right side (EAST) of the legendsymbol.");
649:                    g.drawString(getLabel(), width + 10, height / 2
650:                            + ((ascent - descent) / 2));
651:                    break;
652:
653:                }
654:                    // LP_LEFT
655:                case 4: {
656:                    g.drawString(getLabel(), width + 10, height / 2
657:                            + ((ascent - descent) / 2));
658:                    break;
659:                }
660:                    // LP_BOTTOMCENTER
661:                case 5: {
662:                    LOG
663:                            .logInfo("The text-position LP_BOTTOMCENTER in the legend is not "
664:                                    + "implemented yet.\n We put the text on the right side (EAST) of "
665:                                    + "the legendsymbol.");
666:                    g.drawString(getLabel(), width + 10, height / 2
667:                            + ((ascent - descent) / 2));
668:                    break;
669:                }
670:                    // LP_BOTTOMRIGHT
671:                case 6: {
672:                    LOG
673:                            .logInfo("The text-position LP_BOTTOMRIGHT in the legend is not "
674:                                    + "implemented yet.\n We put the text on the right side (EAST) of "
675:                                    + "the legendsymbol.");
676:                    g.drawString(getLabel(), width + 10, height / 2
677:                            + ((ascent - descent) / 2));
678:                    break;
679:                }
680:                    // LP_BOTTOMLEFT
681:                case 7: {
682:                    LOG
683:                            .logInfo("The text-position LP_BOTTOMLEFT in the legend is not implemented "
684:                                    + "yet.\n We put the text on the right side (EAST) of the legendsymbol.");
685:                    g.drawString(getLabel(), width + 10, height / 2
686:                            + ((ascent - descent) / 2));
687:                    break;
688:                }
689:                }
690:                g.dispose();
691:                return bi;
692:            }
693:
694:            /**
695:             * exports the <tt>LegendElement</tt> as </tt>BufferedImage</tt>
696:             * 
697:             * @param mime
698:             * 
699:             * @return the image
700:             * @throws LegendException
701:             */
702:            public BufferedImage exportAsImage(String mime)
703:                    throws LegendException {
704:
705:                if (bi == null) {
706:                    // calculates the fontmetrics and creates the bufferedimage
707:                    // if getLabel() is null is checked in calculateFontMetrics!
708:                    int[] fontmetrics = calculateFontMetrics(getLabel());
709:                    bi = calculateImage(getLabelPlacement(), fontmetrics[0],
710:                            fontmetrics[1], fontmetrics[2], getWidth(),
711:                            getHeight(), getBufferBetweenLegendAndLabel(), mime);
712:                    Graphics g = bi.getGraphics();
713:                    ((Graphics2D) g).setClip(0, 0, getWidth(), getHeight());
714:                    g.setColor(Color.WHITE);
715:                    Rule[] myrules = getRules();
716:                    Symbolizer[] symbolizer = null;
717:                    // determines the legendsymbol and paints it
718:                    for (int a = 0; a < myrules.length; a++) {
719:                        symbolizer = myrules[a].getSymbolizers();
720:
721:                        for (int b = 0; b < symbolizer.length; b++) {
722:                            if (symbolizer[b] instanceof  PointSymbolizer) {
723:                                drawPointLegend(g,
724:                                        (PointSymbolizer) symbolizer[b],
725:                                        getWidth(), getHeight());
726:                            }
727:                            if (symbolizer[b] instanceof  LineSymbolizer) {
728:                                drawLineStringLegend((Graphics2D) g,
729:                                        (LineSymbolizer) symbolizer[b], width,
730:                                        height);
731:                            }
732:                            if (symbolizer[b] instanceof  PolygonSymbolizer) {
733:                                drawPolygonLegend((Graphics2D) g,
734:                                        (PolygonSymbolizer) symbolizer[b],
735:                                        width, height);
736:                            }
737:                            if (symbolizer[b] instanceof  RasterSymbolizer) {
738:                                // throw new LegendException("RasterSymbolizer is not implemented yet!");
739:                            }
740:                            if (symbolizer[b] instanceof  TextSymbolizer) {
741:                                // throw new LegendException("TextSymbolizer is not implemented yet!");
742:                            }
743:                        }
744:
745:                        // g.setColor(Color.black);
746:                        // g.drawString(getLabel(), width + 10, height / 2 + ((fontmetrics[1] -
747:                        // fontmetrics[2]) / 2));
748:
749:                    }
750:                } else {
751:                    if (mime.equalsIgnoreCase("image/gif")
752:                            || mime.equalsIgnoreCase("image/png")) {
753:                        BufferedImage bii = new BufferedImage(bi.getWidth(), bi
754:                                .getHeight(), BufferedImage.TYPE_INT_ARGB);
755:                        Graphics g = bii.getGraphics();
756:                        g.drawImage(bi, 0, 0, null);
757:                        g.dispose();
758:                        bi = bii;
759:                    } else if (mime.equalsIgnoreCase("image/jpg")
760:                            || mime.equalsIgnoreCase("image/jpeg")
761:                            || mime.equalsIgnoreCase("image/tif")
762:                            || mime.equalsIgnoreCase("image/tiff")
763:                            || mime.equalsIgnoreCase("image/bmp")) {
764:                        BufferedImage bii = new BufferedImage(bi.getWidth(), bi
765:                                .getHeight(), BufferedImage.TYPE_INT_RGB);
766:                        Graphics g = bii.getGraphics();
767:                        g.drawImage(bi, 0, 0, null);
768:                        g.dispose();
769:                        bi = bii;
770:                    } else if (mime.equalsIgnoreCase("image/svg+xml")) {
771:                        // not implemented yet
772:                    }
773:                }
774:                return bi;
775:            }
776:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.