/Users/lyon/j4p/src/bookExamples/ch27BusinessGraphics/charts/Ticks.java

1    package bookExamples.ch27BusinessGraphics.charts; 
2     
3    import java.awt.*; 
4     
5    public class Ticks extends Canvas 
6            implements Paintable { 
7        private DoubleDataBean dd; 
8        private int xtickNumber = 1; 
9        private int ytickNumber = 1; 
10    
11       public Ticks(DoubleDataBean _dd, Dimension d) { 
12           dd = _dd; 
13           setSize(d); 
14       } 
15    
16       public void setNumberOfTicks(int _xtickNumber, 
17                                    int _ytickNumber) { 
18           xtickNumber = _xtickNumber; 
19           ytickNumber = _ytickNumber; 
20       } 
21    
22       /** 
23        *  Draws the grid in the background of the line graphics.graph, 
24        if 
25        *  it has been set to do so in the constructor 
26        (defaults to 
27        *  true). Grid runs length and width of each axis, at 
28        the 
29        *  increment value set in DoubleDataBean.getXIncrement() 
30        and 
31        *  DoubleDataBean.getYIncrement(). 
32        * 
33        *  @param g    Graphics context for drawing 
34        *  @see        LineGraph.#paint 
35        *  @see        DoubleDataBean.#getNumTicks 
36        *  @see        DoubleDataBean.#getXIncrement 
37        *  @see        DoubleDataBean.#getYIncrement 
38        *  @see        DoubleDataBean.#DeltaY 
39        *  @see        java.awt.Graphics.#drawLine 
40        */ 
41       protected void drawGrid(Graphics g) { 
42           if (!dd.isGridOn()) return; 
43           Dimension d = getSize(); 
44           int width = d.width; 
45           int height = d.height; 
46           int numXTicks = dd.getNumTicks 
47                   (dd.getXVals(), width); 
48           int numYTicks = dd.getNumTicks(dd.getYVals(), height); 
49           double xIncrement = dd.getXIncrement(); 
50           double deltaY = dd.getDeltaY(); 
51           int yIncrement = 
52                   (int) Math.round(dd.getYIncrement()); 
53           g.setColor(Color.darkGray); 
54    
55           //new code starts here 
56           double xCoord = dd.getXAxisCoord(); 
57           double yCoord = dd.getYAxisCoord(); 
58           // end new code 
59    
60           //grid along X axis (horizontal lines) 
61           int yHeightPos = (int) (yCoord + yIncrement); 
62           int yHeightNeg = (int) (yCoord - yIncrement); 
63    
64           int yLeft = 0; 
65           int yRight = width; 
66    
67           while (yHeightPos > 0) { 
68               yHeightPos = yHeightPos - (yIncrement); 
69               g.drawLine(yLeft, yHeightPos, yRight, yHeightPos); 
70           } 
71    
72           while (yHeightNeg < height) { 
73               yHeightNeg = yHeightNeg + yIncrement; 
74               g.drawLine(yLeft, yHeightNeg, yRight, yHeightNeg); 
75           } 
76    
77    
78           int xTop = 0; 
79           int xBottom = height; 
80           int xWidthPos = (int) (xCoord + xIncrement); 
81           int xWidthNeg = (int) (xCoord - xIncrement); 
82    
83           while (xWidthPos > 0) { 
84               xWidthPos = xWidthPos - (int) xIncrement; 
85               g.drawLine(xWidthPos, xTop, xWidthPos, xBottom); 
86           } 
87    
88           while (xWidthNeg < width) { 
89               xWidthNeg = xWidthNeg + (int) xIncrement; 
90               g.drawLine(xWidthNeg, xTop, xWidthNeg, xBottom); 
91           } 
92    
93    
94       } 
95    
96       /** 
97        *  Draws the axes for the line graphics.graph and bar graphics.graph 
98        along the 
99        *  calculated X and Y origins. 
100       * 
101       *  @param g    Graphics context for drawing 
102       *  @see        LineGraph.#paint 
103       *  @see        BarGraph.#paint 
104       *  @see        DoubleDataBean.#getXAxisCoord 
105       *  @see        DoubleDataBean.#getYAxisCoord 
106       *  @see        java.awt.Graphics.#drawLine 
107       */ 
108      protected void drawAxes(Graphics g) { 
109          g.setColor(Color.blue); 
110          int xorig = (int) dd.getXAxisCoord(); 
111          int yorig = (int) dd.getYAxisCoord(); 
112   
113          g.drawLine(0, yorig, getSize().width, yorig); 
114          g.drawLine(xorig, 0, xorig, getSize().height); 
115   
116      } 
117   
118      /** 
119       *  Draws the ticks along the axes for the line graphics.graph, 
120       if 
121       *  it has been set to do so in the constructor 
122       (defaults to 
123       *  true). Ticks are hard coded to be two pixels on 
124       either side 
125       *  of each axis. 
126       * 
127       *  @param g    Graphics context for drawing 
128       *  @see        LineGraph.#paint 
129       *  @see        DoubleDataBean.#getNumTicks 
130       *  @see        DoubleDataBean.#getXIncrement 
131       *  @see        DoubleDataBean.#getYIncrement 
132       *  @see        DoubleDataBean.#DeltaY 
133       *  @see        DoubleDataBean.#getXAxisCoord 
134       *  @see        DoubleDataBean.#getYAxisCoord 
135       *  @see        java.awt.Graphics.#drawLine 
136       */ 
137      public void paint(Graphics g) { 
138          Dimension d = getSize(); 
139          drawAxes(g); 
140          //drawGrid(g); 
141          int width = d.width; 
142          int height = d.height; 
143          double xIncrement = width / xtickNumber; 
144          double deltaY = dd.getDeltaY(); 
145          double yIncrement = height / ytickNumber; 
146   
147  //Horizontal lines (along Y Axis) 
148          int xCoord = (int) dd.getXAxisCoord(); 
149          int yCoord = (int) dd.getYAxisCoord(); 
150          int yLeft = xCoord - 2; 
151          int yRight = xCoord + 2; 
152          int xTop = yCoord + 2; 
153          int xBottom = yCoord - 2; 
154          int yHeightPos = (int) (yCoord + yIncrement); 
155          int yHeightNeg = (int) (yCoord - yIncrement); 
156          int xWidthPos = (int) (xCoord + xIncrement); 
157          int xWidthNeg = (int) (xCoord - xIncrement); 
158   
159          while (yHeightPos > 0) { 
160              yHeightPos = yHeightPos - ((int) yIncrement); 
161              g.drawLine(yLeft, yHeightPos, yRight, yHeightPos); 
162   
163          } 
164   
165          while (yHeightNeg < height) { 
166              yHeightNeg = yHeightNeg + (int) yIncrement; 
167              g.drawLine(yLeft, yHeightNeg, yRight, yHeightNeg); 
168          } 
169   
170          while (xWidthPos > 0) { 
171              xWidthPos = xWidthPos - (int) xIncrement; 
172              g.drawLine(xWidthPos, xTop, xWidthPos, xBottom); 
173          } 
174   
175          while (xWidthNeg < width) { 
176              xWidthNeg = xWidthNeg + (int) xIncrement; 
177              g.drawLine(xWidthNeg, xTop, xWidthNeg, xBottom); 
178          } 
179   
180   
181      } 
182  } 
183