KickJava   Java API By Example, From Geeks To Geeks.

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


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 HorizontalAxis extends Axis
25 {
26    /**
27     * Constructor for VerticalAxis.
28     *
29     * @param format Description of the Parameter
30     * @param position Description of the Parameter
31     */

32    public HorizontalAxis(Format JavaDoc format, int position)
33    {
34       this.format = format;
35       this.orientation = HORIZONTAL;
36       this.position = position;
37    }
38
39
40    /**
41     * Description of the Method
42     *
43     * @param graphics Description of the Parameter
44     */

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