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

1    package bookExamples.ch27BusinessGraphics.charts; 
2     
3    import gui.ClosableJFrame; 
4     
5    import javax.swing.*; 
6    import java.awt.*; 
7     
8    /** 
9     * LineGraph implements the Paintable interface to draw a line 
10    * graphics.graph on a frame. <BR> 
11    * Global variables: 
12    * <UL> 
13    * <LI> DoubleDataBean: instance of DoubleDataBean so the graphics.graph is drawn 
14    *     with the proper data 
15    * </UL> 
16    * 
17    * @author  Allison McHenry 
18    * @author  Douglas Lyon, PhD 
19    * @since   JDK 1.3 
20    */ 
21    
22   public class LineGraph extends JComponent 
23           implements Paintable { 
24    
25   //GLOBAL VARIABLES 
26       private DoubleDataBean dd; 
27    
28   //CONSTRUCTORS 
29       /** 
30        *  Default constructor, used to instantiate an instance of LineGraph by 
31        *  the testing class 
32        * 
33        *      @param _dd      instance of DoubleDataBean used to draw the graphics.graph 
34        */ 
35       public LineGraph(DoubleDataBean _dd) { 
36           dd = _dd; 
37           setSize(dd.getWidth(), dd.getHeight()); 
38       } 
39    
40       public Dimension getPreferredSize() { 
41           return new Dimension(dd.getWidth(), dd.getHeight()); 
42       } 
43    
44       public Dimension getMinimumSize() { 
45           return new Dimension(dd.getWidth(), dd.getHeight()); 
46       } 
47    
48   //UTILITY METHODS 
49    
50       /** 
51        *  Actually paints this data from DoubleDataBean onto an image. The DrawUtil class is a 
52        *  generic utilities class containing all methods which actually 
53        *  handle the painting. 
54        * 
55        *  @param g    Graphics context for drawing 
56        *  @see        DrawUtil.#drawGrid 
57        *  @see        DrawUtil.#drawTicks 
58        *  @see        DrawUtil.#drawAxes2 
59        *  @see        DrawUtil.#drawXAxisLabel 
60        *  @see        DrawUtil.#drawYAxisLabel 
61        *  @see        DrawUtil.#drawTitle 
62        *  @see        DrawUtil.#drawLine 
63        */ 
64       public void paint(Graphics g) { 
65           DrawUtil du = new DrawUtil(dd); 
66           Ticks t = new Ticks(dd, getSize()); 
67           t.paint(g); 
68           drawGraph(g); 
69           du.drawXAxisLabel(g); 
70           du.drawYAxisLabel(g); 
71           du.drawTitle(g); 
72    
73           System.out.println("Successfully drawing line graphics.graph"); 
74       } 
75    
76    
77       /** 
78        *  Draws the line for the line graphics.graph. 
79        * 
80        *  @param g    Graphics context for drawing 
81        *  @see        LineGraph.#paint 
82        *  @see        DoubleDataBean.#getXScreenCoords2 
83        *  @see        DoubleDataBean.#getYScreenCoords2 
84        *  @see        java.awt.#drawPolyline 
85        */ 
86       private void drawGraph(Graphics g) { 
87           // actual code 
88           g.setColor(Color.red); 
89           Polygon p = getPolygon(); 
90           g.drawPolyline(p.xpoints, p.ypoints, p.npoints); 
91    
92       } 
93    
94       /** 
95        *  Method used to by the line graphics.graph to convert entered X data 
96        *  to data appropriate for drawing on the screen 
97        *  (incremented to fit in the available screen size, corrected for offset, etc) 
98        * 
99        *  @return Xs                  The new data array 
100       *  @see                        LineGraph.#drawGraph 
101       */ 
102   
103      private int[] getXScreenCoords() { 
104          double[] xVals = dd.getXVals(); 
105          int xLength = xVals.length; 
106          int width = dd.getWidth(); 
107          double deltaX = dd.getDeltaX(); 
108          double xOrigin = dd.getXAxisCoord(); 
109          double xIncrement = dd.getIncrementNew(xVals, width); 
110          double maxX = dd.getMax(xVals); 
111   
112          int Xs[] = new int[xLength]; 
113   
114          if (maxX > 0) { 
115              for (int i = 0; i < xLength; i++) { 
116                  Xs[i] = (int) (xIncrement * xVals[i]) + (int) xOrigin; 
117   
118              } 
119          } else { 
120              for (int i = 0; i < xLength; i++) { 
121                  Xs[i] = width - ((int) (xIncrement * xVals[i])); 
122   
123              } 
124          } 
125          return Xs; 
126   
127      } 
128   
129      /** 
130       *  Method used to by the line graphics.graph to convert entered Y data 
131       *  to data appropriate for drawing on the screen 
132       *  (incremented to fit in the available screen size, corrected for offset, etc) 
133       * 
134       *  @return Ys                  The new data array 
135       *  @see                        LineGraph.#drawGraph 
136       */ 
137      private int[] getYScreenCoords() { 
138          double[] yVals = dd.getYVals(); 
139          int height = dd.getHeight(); 
140          int yLength = yVals.length; 
141          double deltaY = dd.getDeltaY(); 
142          double yIncrement = dd.getYIncrement(); 
143          int Ys[] = new int[yLength]; 
144          double maxY = dd.getMax(yVals); 
145   
146          double yOrigin = dd.getYAxisCoord(); 
147   
148          for (int i = 0; i < yLength; i++) { 
149              Ys[i] = (int) (yOrigin - (yVals[i] * yIncrement)); // +Y_OFFSET 
150          } 
151   
152          return Ys; 
153   
154      } 
155   
156      private Polygon getPolygon() { 
157          return new Polygon(getXScreenCoords(), 
158                  getYScreenCoords(), getXScreenCoords().length); 
159      } 
160   
161      public static void main(String args[]) { 
162          ClosableJFrame cf = new ClosableJFrame(); 
163          Container c = cf.getContentPane(); 
164          double numberOfPoints = 100; 
165          double eps = (2 * Math.PI + .1) / numberOfPoints; 
166          double theta = 0; 
167          double x[] = new double[(int) numberOfPoints]; 
168          double y[] = new double[x.length]; 
169          for (int i = 0; i < x.length; i++) { 
170              x[i] = Math.cos(theta); 
171              y[i] = Math.sin(theta); 
172              theta = theta + eps; 
173          } 
174   
175          //double xes[] = new double[]{-10,-20,-30,-40}; 
176          //double yes[] = new double[] {10,20,30,40}; 
177          DoubleDataBean dd = new DoubleDataBean(200, 200); 
178          dd.setXLabel("x label"); 
179          dd.setYLabel("y label"); 
180          dd.setXVals(x); 
181          dd.setYVals(y); 
182          LineGraph lg = new LineGraph(dd); 
183          c.add(lg); 
184          c.setLayout( 
185                  new FlowLayout()); 
186          c.setBackground(Color.white); 
187          cf.setSize(200, 200); 
188          cf.setVisible(true); 
189      } 
190   
191  } 
192   
193   
194   
195   
196   
197   
198   
199   
200   
201   
202   
203