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

1    package bookExamples.ch27BusinessGraphics.charts; 
2     
3    import gui.ClosableJFrame; 
4     
5    import javax.swing.*; 
6    import java.awt.*; 
7     
8    /** 
9     * BarGraph implements the Paintable interface to draw a bar 
10    * graphics.graph on a frame. Requires an instance of DoubleDataBean in order 
11    * to be constructed.<BR> 
12    * Global variables: 
13    * <UL> 
14    * <LI> DoubleDataBean: instance of DoubleDataBean so the graphics.graph is drawn 
15    *     with the proper data 
16    *  <LI> barwidth: an arbitrary width for each bar in the bar graphics.graph 
17    * </UL> 
18    * 
19    * @author  Allison McHenry 
20    * @author  Douglas Lyon, PhD 
21    * @since   JDK 1.3 
22    */ 
23    
24    
25   public class BarGraph extends JComponent 
26           implements Paintable { 
27    
28   //GLOBAL VARIABLES 
29       private DoubleDataBean dd; 
30       private int barwidth = 6; 
31    
32   //CONSTRUCTORS 
33    
34    
35    
36       /** 
37        *  Constructor used if only height and width should be set, using defaults for all 
38        *  global variables. Probably would only use this as a test case. 
39        *  @param w    Image width (not frame width) 
40        *  @param h    Image height (not frame height) 
41        */ 
42       public BarGraph(DoubleDataBean _dd) { 
43           dd = _dd; 
44           setSize(dd.getWidth(), dd.getHeight()); 
45       } 
46    
47       public Dimension getPreferredSize() { 
48           return new Dimension(dd.getWidth(), dd.getHeight()); 
49       } 
50    
51       public Dimension getMinimumSize() { 
52           return new Dimension(dd.getWidth(), dd.getHeight()); 
53       } 
54    
55       /** 
56        *  Sets the arbitrary width of the bars. 
57        * 
58        *  @param _barwidth Width of each bar 
59        */ 
60       public void setBarWidth(int _barwidth) { 
61           barwidth = _barwidth; 
62       } 
63    
64    
65    
66   //UTILITY METHODS 
67    
68       /** 
69        *  Actually paints this data from DoubleDataBean onto an image. The DrawUtil class is a 
70        *  generic utilities class containing all methods which actually 
71        *  handle the painting. 
72        * 
73        *  @param g    Graphics context for drawing 
74        *  @see        drawGraph 
75        *  @see        DrawUtil.#drawAxes2 
76        *  @see        DrawUtil.#drawXAxisLabel 
77        *  @see        DrawUtil.#drawYAxisLabel 
78        *  @see        DrawUtil.#drawTitle 
79        */ 
80       public void paint(Graphics g) { 
81    
82           DrawUtil du = new DrawUtil(dd); 
83           Ticks t = new Ticks(dd, getSize()); 
84           t.setNumberOfTicks(10, 10); 
85           ; 
86           t.paint(g); 
87           //du.drawGrid(g); 
88           drawGraph(g); 
89           //du.drawXAxisLabel(g); 
90           //du.drawYAxisLabel(g); 
91           du.drawTitle(g); 
92       } 
93    
94    
95       /** 
96        *  Draws the bars for the bar graphics.graph. The height of the bars 
97        *  is determined by the values in the Y array. The values in 
98        *  the X array are irrelevant. 
99        * 
100       *  @param g    Graphics context for drawing 
101       *  @see        paint 
102       *  @see        DoubleDataBean.#getYAxisCoord 
103       *  @see        DoubleDataBean.#getYIncrement 
104       *  @see        getXCoord 
105       *  @see        getYCoord 
106       *  @see        java.awt.#fillRect 
107       */ 
108      private void drawGraph(Graphics g) { 
109   
110          int barheight = 0; 
111          g.setColor(Color.red); 
112          int x; 
113          int y; 
114          int width = dd.getWidth(); 
115   
116          double yOrigin = dd.getYAxisCoord(); 
117          double yNums[] = dd.getYVals(); 
118          int pxPerY = (int) dd.getYIncrement(); 
119   
120          System.out.println("width=" + width); 
121   
122          for (int j = 0; j < yNums.length; j++) { 
123              x = (int) getXCoord(j + 1, (dd.getIncrement(width, yNums.length))); 
124   
125              y = (int) getYCoord(yNums[j], pxPerY); 
126   
127              //center the bar 
128              x = x - (barwidth); 
129   
130              if (y > yOrigin) { // ie Y is a negative value 
131                  barheight = y - (int) yOrigin; 
132                  y = (int) yOrigin; 
133              } else { 
134                  barheight = (int) Math.abs(yOrigin - y); 
135              } 
136   
137              System.out.println("x=" + x); 
138              System.out.println("y=" + y); 
139              System.out.println("barwidth=" + barwidth); 
140              System.out.println("barheight=" + barheight); 
141              g.fillRect(x, y, barwidth, barheight); 
142          } 
143      } 
144   
145      /** 
146       *  Method used to by the bar graphics.graph to calculate where on the x axis 
147       *  to start drawing the rectangle for the graphics.graph: the increment * the 
148       *  x value 
149       * 
150       *  @return xCoord              The X coordinate of the top left corner of this bar 
151       *  @see                        drawGraph 
152       */ 
153   
154      private double getXCoord(double numX, int xPixelsWide) { 
155          double xCoord = numX * xPixelsWide; 
156          return xCoord; 
157      } 
158   
159      /** 
160       *  Method used to by the bar graphics.graph to calculate where on the y axis 
161       *  to start drawing the rectangle for the graphics.graph( the increment * the y value) 
162       *   subtracted from the y origin because the zero value for Y is the top of the screen 
163       * 
164       *  @return yCoord              The Y coordinate of the top left corner of this bar 
165       *  @see                        drawGraph 
166       */ 
167      private double getYCoord(double numY, int yPixelsHigh) { 
168          double yOrigin = dd.getYAxisCoord(); 
169          double yCoord = yOrigin - (numY * yPixelsHigh); 
170          return yCoord; 
171      } 
172   
173      public static void main(String args[]) { 
174          ClosableJFrame cf = new ClosableJFrame(); 
175          Container c = cf.getContentPane(); 
176          DoubleDataBean dd = new DoubleDataBean(200, 200); 
177          double yVals[] = {10, -20, -40, 80}; 
178          dd.setYVals(yVals); 
179          BarGraph bg = new BarGraph(dd); 
180          bg.setBarWidth(6); 
181          c.add(bg); 
182          c.setLayout( 
183                  new FlowLayout()); 
184          cf.setBackground(Color.white); 
185          cf.setSize(200, 200); 
186          cf.setVisible(true); 
187      } 
188   
189   
190  } 
191   
192   
193   
194   
195   
196   
197   
198   
199   
200   
201