/Users/lyon/j4p/src/bookExamples/ch27BusinessGraphics/charts/GraphFrame.java
|
1 package bookExamples.ch27BusinessGraphics.charts;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.WindowEvent;
6
7 public class GraphFrame extends Frame {
8
9 private Plot1 Data;
10 private String[] xaxis;
11 private String[] yaxis;
12 private Rectangle r;
13
14
15 public static void main(String args[]) {
16 double limit = 2 * Math.PI;
17 double incr = 10;
18 String xaxis[] = {"X", "1pi", "2pi", "3pi"};
19 String yaxis[] = {"Sin(x)", "-1.0", "0.0", "1.0"};
20
21 int n = (int) (1 + limit / incr);
22 Plot1 data = new Plot1(n, xaxis, yaxis);
23
24 for (double x = 0; x < limit; x += incr)
25 data.push(x, Math.sin(2 * x), 0);
26
27 GraphFrame pf = new GraphFrame("Eye Intensity", data);
28 pf.setSize(400, 400);
29 pf.setVisible(true);
30
31 }
32
33
34 public GraphFrame(String mark, Plot1 data1) {
35
36 super(mark);
37 Data = data1;
38 xaxis = data1.getxA();
39 yaxis = data1.getyA();
40 }
41
42
43 public void update(Graphics g) {
44 g.clearRect(0, 0, r.width, r.height);
45 paint(g);
46 }
47
48
49 public void paint(Graphics g) {
50 r = getBounds();
51
52
53 int w = r.width;
54 int h = r.height;
55 int leftMargin = 30;
56 int tickMarkInterval = 10;
57 int tickMarkWidth = 30;
58 int middleY = h / 2 - 25;
59
60 setBackground(Color.white);
61
62 g.drawLine(leftMargin, middleY, w, middleY);
63 g.drawLine(leftMargin, 0, leftMargin, h);
64
65 for (int i = 1; i < w; i += tickMarkInterval)
66 g.drawLine(i, middleY, i, h / 2 - tickMarkWidth);
67
68 for (int j = 1; j < h; j += tickMarkInterval)
69 g.drawLine(leftMargin, j, 35, j);
70
71 g.drawString(yaxis[0], 35, h / 20);
72 g.drawString(yaxis[1], 3, h / 4 * 3 - 5);
73 g.drawString(yaxis[2], 5, middleY);
74 g.drawString(yaxis[3], 5, h / 5 - 30);
75 g.drawString(xaxis[0], w / 2, h / 4 * 3 + 20);
76 g.drawString(xaxis[1], w / 3 - 5, h / 2 - 15);
77 g.drawString(xaxis[2], w / 2 + 20, h / 2 - 15);
78 g.drawString(xaxis[3], w - 80, h / 2 - 15);
79
80
81 Point p1,p2;
82
83 p1 = new Point(leftMargin, h / 3 + 37);
84 double max = 2 * Math.PI;
85
86 for (int t = 0; t < Data.getNum(); t++) {
87
88 int x1 = (int) (Data.getx(t) * w / max + leftMargin);
89 int y1 = (int) (h / 3 - (h / 3) * Data.gety(t) + 37);
90
91 p2 = new Point(x1, y1);
92 g.drawLine(p1.x, p1.y, p2.x, p2.y);
93 p1 = p2;
94 }
95 }
96
97 public void actionPerformed(ActionEvent e) {
98
99 if (e.getID() == WindowEvent.WINDOW_CLOSING)
100 dispose();
101 }
102
103 public Plot1 getData() {
104 return Data;
105 }
106
107 public void setData(Plot1 data) {
108 Data = data;
109 }
110
111 public String[] getXaxis() {
112 return xaxis;
113 }
114
115 public void setXaxis(String[] xaxis) {
116 this.xaxis = xaxis;
117 }
118
119 public String[] getYaxis() {
120 return yaxis;
121 }
122
123 public void setYaxis(String[] yaxis) {
124 this.yaxis = yaxis;
125 }
126
127 public Rectangle getR() {
128 return r;
129 }
130
131 public void setR(Rectangle r) {
132 this.r = r;
133 }
134 }
135
136 class Plot1 {
137 private int num;
138 private String x[];
139 private String y[];
140
141 private double x_d[];
142 private double y_d[];
143 private double z_d[];
144 private int lct = 0;
145
146 public Plot1(int nn, String[] xx, String[] yy) {
147 num = nn;
148 x = xx;
149 y = yy;
150 x_d = new double[num];
151 y_d = new double[num];
152 z_d = new double[num];
153 }
154
155 public String[] getxA() {
156 return x;
157 }
158
159 public String[] getyA() {
160 return y;
161 }
162
163 public int getsize() {
164 return num;
165 }
166
167 public double getx(int dd) {
168 return x_d[dd];
169 }
170
171 public double gety(int dd) {
172 return y_d[dd];
173 }
174
175 public double getz(int dd) {
176 return z_d[dd];
177 }
178
179 public void push(double xxx, double yyy, double zzz) {
180 x_d[lct] = xxx;
181 y_d[lct] = yyy;
182 z_d[lct] = zzz;
183 lct++;
184 }
185
186 public int getNum() {
187 return num;
188 }
189
190 public void setNum(int num) {
191 this.num = num;
192 }
193
194 public String[] getX() {
195 return x;
196 }
197
198 public void setX(String[] x) {
199 this.x = x;
200 }
201
202 public String[] getY() {
203 return y;
204 }
205
206 public void setY(String[] y) {
207 this.y = y;
208 }
209
210 public double[] getX_d() {
211 return x_d;
212 }
213
214 public void setX_d(double[] x_d) {
215 this.x_d = x_d;
216 }
217
218 public double[] getY_d() {
219 return y_d;
220 }
221
222 public void setY_d(double[] y_d) {
223 this.y_d = y_d;
224 }
225
226 public double[] getZ_d() {
227 return z_d;
228 }
229
230 public void setZ_d(double[] z_d) {
231 this.z_d = z_d;
232 }
233
234 public int getLct() {
235 return lct;
236 }
237
238 public void setLct(int lct) {
239 this.lct = lct;
240 }
241 }
242
243
244
245