KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > Legend


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     Legend.java
21     Created on 26. Juni 2001, 22:49
22  */

23
24 package de.progra.charting;
25
26 import java.awt.*;
27 import java.awt.geom.*;
28 import java.awt.font.*;
29 import java.awt.image.*;
30 import de.progra.charting.render.*;
31
32 /** This class implements a Chart's Legend. The Strings and the colors
33  * can be set manually or eventually through some kind of data model.
34  */

35 public class Legend extends AbstractRenderer {
36         
37     protected int inner_margin = 5;
38     protected int color_text_spacing = 10;
39     protected Font font = new Font("Helvetica", Font.PLAIN, 14);
40     protected Rectangle colorbox = new Rectangle(25, 15);
41    
42     protected RowColorModel rcm;
43     
44     /** Creates a default Legend. */
45     public Legend() {
46     }
47     
48     /** Creates a Legend with the given Strings and Colors.
49      * @param rcm the RowColorModel containing the row titles and their colors.
50      */

51     public Legend(RowColorModel rcm) {
52         setRowColorModel(rcm);
53     }
54     
55     /** Defines the RowColorModel of the DataModel.
56      * @param rcm the RowColorModel
57      */

58     public void setRowColorModel(RowColorModel rcm) {
59         this.rcm = rcm;
60     }
61     
62     /** Returns the RowColorModel of the DataModel.
63      * @return the RowColorModel
64      */

65     public RowColorModel getRowColorModel() {
66         return rcm;
67     }
68     
69     /** Sets the size of the color boxes.
70      * @param r the Rectangle defining the colored box rendered left to every Legend entry.
71      */

72     public void setColorBox(Rectangle r) {
73         colorbox = r;
74     }
75     
76     /** Returns the size of the color boxes.
77      * @return the Rectangle defining the colored box left to every Legend entry
78      */

79     public Rectangle getColorBox() {
80         return colorbox;
81     }
82     
83     /** Sets the Font that is used to render the Legend.
84      * @param f the font object
85      */

86     public void setFont(Font f) {
87         font = f;
88     }
89     
90     /** Returns this Legend's Font.
91      * @return the font currently in use
92      */

93     public Font getFont() {
94         return font;
95     }
96     
97     /** Returns the preferred size needed for the renderer.
98      * @return a non-null Dimension object
99      */

100     public Dimension getPreferredSize() {
101         RowColorModel rcm = getRowColorModel();
102         
103         int maxTitleWidth = Integer.MIN_VALUE;
104         int titleHeight = Integer.MIN_VALUE;
105         
106         titleHeight =
107             (int)getFont().getMaxCharBounds(new FontRenderContext(null, true, false)).getHeight();
108         
109         for(int i = 0; i < rcm.getRowCount(); i++) {
110             TextLayout layout =
111                 new TextLayout(rcm.getRow(i), getFont(),
112                                new FontRenderContext(null, true, false));
113             
114             maxTitleWidth = (int)Math.max((double)maxTitleWidth,
115                                           layout.getBounds().getWidth());
116         }
117         
118         return new Dimension((int)(2*inner_margin + color_text_spacing +
119                              getColorBox().getWidth() + maxTitleWidth),
120                              Math.max(titleHeight, (int)getColorBox().getHeight()) * rcm.getRowCount() +
121                              (rcm.getRowCount() + 1) * inner_margin);
122     }
123     
124     /** This method is called by the paint method to do the actual painting.
125      * The painting is supposed to start at point (0,0) and the size is
126      * always the same as the preferred size. The paint method performs
127      * the possible scaling.
128      * @param g the <CODE>Graphics2D</CODE> object to paint in
129      */

130     public void paintDefault(Graphics2D g) {
131         RowColorModel rcm = getRowColorModel();
132         
133         int height = Integer.MIN_VALUE;
134         
135         int fontheight =
136             (int)getFont().getMaxCharBounds(g.getFontRenderContext()).getHeight();
137         height = (int)Math.max(fontheight, getColorBox().getHeight());
138         
139         int startx = inner_margin;
140         int starty = inner_margin;
141         
142         Rectangle colorBox = getColorBox();
143         
144         /* Rendering the Text and the ColorBoxes. */
145         for(int i = 0; i < rcm.getRowCount(); i++) {
146             colorBox.setLocation(startx, starty);
147             g.setColor(rcm.getColor(i));
148             
149             g.fill(colorBox);
150             
151             g.setColor(Color.black);
152             
153             TextLayout layout =
154                 new TextLayout(rcm.getRow(i), getFont(),
155                                new FontRenderContext(null, true, false));
156                    
157             layout.draw(g,
158                         startx + (int)colorBox.getWidth() + color_text_spacing,
159                         starty+(int)colorBox.getHeight());
160             
161             starty = starty + height + inner_margin;
162         }
163     }
164 }
Popular Tags