KickJava   Java API By Example, From Geeks To Geeks.

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


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.Dimension JavaDoc;
11 import java.awt.FontMetrics JavaDoc;
12 import java.awt.Graphics JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.geom.Rectangle2D JavaDoc;
15 import java.text.Format JavaDoc;
16
17 import org.ejtools.graph.Axis;
18
19 /**
20  * @author Laurent Etiemble
21  * @version $Revision: 1.7 $
22  * @todo Javadoc to complete
23  */

24 public class VerticalAxis 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 VerticalAxis(Format JavaDoc format, int position)
33    {
34       this.format = format;
35       this.orientation = VERTICAL;
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       int base;
50       int small;
51       int middle;
52       int correction;
53
54       graphics.setFont(FONT);
55       FontMetrics JavaDoc metrics = graphics.getFontMetrics();
56
57       Insets JavaDoc insets = this.getInsets();
58       // int x = this.getX() + insets.left;
59
// int y = this.getY() + insets.top;
60
// double width = (double) this.getWidth() - 1 - insets.left - insets.right;
61
double height = (double) this.getHeight() - 1 - insets.top - insets.bottom;
62
63       double min = element.getYRange().getMin();
64       double max = element.getYRange().getMax();
65
66       String JavaDoc minText = this.getFormated(min);
67       Rectangle2D JavaDoc minBounds = metrics.getStringBounds(minText, graphics);
68       String JavaDoc maxText = this.getFormated(max);
69       Rectangle2D JavaDoc maxBounds = metrics.getStringBounds(maxText, graphics);
70
71       // New dimension
72
double newBound = Math.max(minBounds.getWidth(), maxBounds.getWidth());
73       this.dimension = new Dimension JavaDoc((int) (newBound + FONT.getSize()), this.getHeight());
74       this.revalidate();
75
76       // Vertical drawing
77
double bound = Math.max(minBounds.getHeight() * 4.0d, maxBounds.getHeight() * 4.0d);
78
79       // Specified the number of texts
80
int ticks = 1;
81       while ((height / ticks) > bound)
82       {
83          ticks = ticks * 2;
84       }
85
86       // Set parameters
87
if (this.position == LEFT)
88       {
89          middle = (int) (newBound);
90          small = middle + FONT.getSize() / 2 - 1;
91          base = middle + FONT.getSize() - 1;
92       }
93       else
94       {
95          middle = FONT.getSize() - 2;
96          small = FONT.getSize() / 2 - 1;
97          base = 0;
98       }
99
100       // Draw the baseline
101
graphics.drawLine(base, 0, base, (int) height);
102
103       // Draw the divisions
104
multiplier = height / ticks / 2;
105       graphics.setColor(Color.black);
106       for (int i = 0; i <= (2 * ticks); i++)
107       {
108          graphics.drawLine(base, (int) (i * multiplier), small, (int) (i * multiplier));
109       }
110
111       // draw ticks
112
multiplier = height / ticks;
113
114       // Draw first tick
115
graphics.drawLine(base, (int) height, middle, (int) height);
116       if (this.position == LEFT)
117       {
118          correction = -(int) minBounds.getWidth();
119       }
120       else
121       {
122          correction = 2;
123       }
124       graphics.drawString(minText, middle + correction, (int) (height - metrics.getDescent()));
125
126       // Draw others ticks
127
for (int i = 1; i < ticks; i++)
128       {
129          int j = (int) (height - i * multiplier);
130          graphics.drawLine(base, j, middle, j);
131
132          display = this.getFormated(min + i * (max - min) / ticks);
133          Rectangle2D JavaDoc middleBounds = metrics.getStringBounds(display, graphics);
134
135          j = j + (int) (middleBounds.getHeight() / 2);
136          if (this.position == LEFT)
137          {
138             correction = -(int) middleBounds.getWidth();
139          }
140          else
141          {
142             correction = 2;
143          }
144          graphics.drawString(display, middle + correction, j);
145       }
146
147       // Draw last tick
148
graphics.drawLine(base, 0, middle, 0);
149       if (this.position == LEFT)
150       {
151          correction = -(int) maxBounds.getWidth();
152       }
153       else
154       {
155          correction = 2;
156       }
157       graphics.drawString(maxText, middle + correction, metrics.getAscent());
158    }
159 }
160
Popular Tags