KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > graph > renderer > GridGraphRenderer


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.graph.renderer;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.FontMetrics JavaDoc;
11 import java.awt.Graphics JavaDoc;
12 import java.awt.Insets JavaDoc;
13 import java.awt.geom.Rectangle2D JavaDoc;
14 import java.text.Format JavaDoc;
15
16 import org.ejtools.graph.Axis;
17 import org.ejtools.graph.GraphRenderer;
18
19 /**
20  * @author Laurent Etiemble
21  * @version $Revision: 1.7 $
22  * @todo Javadoc to complete
23  */

24 public class GridGraphRenderer extends Axis
25 {
26    /** Description of the Field */
27    protected Format JavaDoc horizontalFormat = null;
28    /** Description of the Field */
29    protected Format JavaDoc verticalFormat = null;
30
31
32    /**
33     *Constructor for the GridRenderer object
34     *
35     * @param horizontalFormat Description of the Parameter
36     * @param verticalFormat Description of the Parameter
37     */

38    public GridGraphRenderer(Format JavaDoc horizontalFormat, Format JavaDoc verticalFormat)
39    {
40       this.format = horizontalFormat;
41       this.horizontalFormat = horizontalFormat;
42       this.verticalFormat = verticalFormat;
43    }
44
45
46    /**
47     * @param graphics Description of the Parameter
48     */

49    protected void paintComponent(Graphics JavaDoc graphics)
50    {
51       double multiplier = 0.0d;
52
53       graphics.setFont(FONT);
54       FontMetrics JavaDoc metrics = graphics.getFontMetrics();
55       graphics.setColor(Color.lightGray);
56
57       double minX = element.getXRange().getMin();
58       double maxX = element.getXRange().getMax();
59       double minY = element.getYRange().getMin();
60       double maxY = element.getYRange().getMax();
61
62       Insets JavaDoc insets = this.getInsets();
63       int x = insets.left;
64       int y = insets.top;
65       double width = (double) this.getWidth() - 1 - insets.left - insets.right;
66       double height = (double) this.getHeight() - 1 - insets.top - insets.bottom;
67
68       double scaleX = width / (maxX - minX);
69       double scaleY = -height / (maxY - minY);
70       double offsetX = x - scaleX * minX;
71       double offsetY = y - scaleY * maxY;
72
73       if (this.horizontalScaling == GraphRenderer.ALIGN_LEFT)
74       {
75          scaleX = 1.0d;
76          offsetX = -minX;
77       }
78       if (this.horizontalScaling == GraphRenderer.ALIGN_RIGHT)
79       {
80          scaleX = 1.0d * this.horizontalScale;
81          offsetX = width - maxX * this.horizontalScale;
82       }
83
84       // Render horizontal lines
85
minX = (x - offsetX) / scaleX;
86       this.format = this.horizontalFormat;
87
88       String JavaDoc minText = this.getFormated(minX);
89       Rectangle2D JavaDoc minBounds = metrics.getStringBounds(minText, graphics);
90       String JavaDoc maxText = this.getFormated(maxX);
91       Rectangle2D JavaDoc maxBounds = metrics.getStringBounds(maxText, graphics);
92
93       // Horizontal drawing
94
double bound = Math.max(minBounds.getWidth() * 4.0d, maxBounds.getWidth() * 4.0d);
95
96       // Specified the number of lines
97
int ticks = 1;
98       while ((width / ticks) > bound)
99       {
100          ticks = ticks * 2;
101       }
102
103       // Draw the divisions
104
multiplier = width / ticks / 2;
105       for (int i = 0; i <= (2 * ticks); i++)
106       {
107          graphics.drawLine((int) (i * multiplier), 0, (int) (i * multiplier), (int) height);
108       }
109
110       // Render horizontal lines
111
this.format = this.verticalFormat;
112
113       minText = this.getFormated(minY);
114       minBounds = metrics.getStringBounds(minText, graphics);
115       maxText = this.getFormated(maxY);
116       maxBounds = metrics.getStringBounds(maxText, graphics);
117
118       // Vertical drawing
119
bound = Math.max(minBounds.getHeight() * 4.0d, maxBounds.getHeight() * 4.0d);
120
121       // Specified the number of texts
122
ticks = 1;
123       while ((height / ticks) > bound)
124       {
125          ticks = ticks * 2;
126       }
127
128       // Draw the divisions
129
multiplier = height / ticks / 2;
130       for (int i = 0; i <= (2 * ticks); i++)
131       {
132          graphics.drawLine(0, (int) (i * multiplier), (int) width, (int) (i * multiplier));
133       }
134
135       this.element.draw(graphics, scaleX, offsetX, scaleY, offsetY);
136    }
137 }
138
Popular Tags