1: import java.awt.*;
2:
3: public abstract class Plotter
4: extends java.applet.Applet {
5: public abstract double func(double x);
6: public void init() {
7: d = getSize();
8: String att = getParameter("xratio");
9: if (att != null)
10: xratio = Integer.parseInt(att);
11: att = getParameter("yratio");
12: if (att != null)
13: yratio = Integer.parseInt(att);
14: att = getParameter("xorigin");
15: if (att != null)
16: xorigin = Integer.parseInt(att);
17: else
18: xorigin = d.width / 2;
19: att = getParameter("yorigin");
20: if (att != null)
21: yorigin = Integer.parseInt(att);
22: else
23: yorigin = d.height / 2;
24: }
25: public void paint(Graphics g) {
26: drawCoordinates(g);
27: plotFunction(g);
28: }
29:
30: /** the dimension of the viewing area */
31: protected Dimension d;
32:
33: /** The color used for plotting */
34: protected Color color = Color.black;
35:
36: /** The position of the origin of the coordinate system */
37: protected int xorigin, yorigin;
38:
39: /** The number of pixels between 0 and 1 in x and y direction */
40: protected int xratio = 100, yratio = 100;
41:
42: protected void plotFunction(Graphics g) {
43: for (int px = 0; px < d.width; px++) {
44: try {
45: double x = (double)(px - xorigin) / (double)xratio;
46: double y = func(x);
47: int py = yorigin - (int) (y * yratio);
48: g.fillOval(px - 1, py - 1, 3, 3);
49: } catch (Exception e) {}
50: }
51: }
52: protected void drawCoordinates(Graphics g) {
53: g.setColor(Color.white);
54: g.fillRect(0, 0, d.width, d.height);
55:
56: g.setColor(color);
57: g.drawLine(0, yorigin, d.width, yorigin);
58: g.drawLine(xorigin, 0, xorigin, d.height);
59:
60: g.setFont(new Font("TimeRoman", Font.PLAIN, 10));
61: int px, py;
62: int i = 1;
63: py = yorigin + 12;
64: g.drawString("0", xorigin + 2, py);
65: for (px = xorigin + xratio; px < d.width; px += xratio) {
66: g.drawString(Integer.toString(i++), px - 2, py);
67: g.drawLine(px, yorigin - 2, px, yorigin + 2);
68: }
69:
70: i = -1;
71: for (px = xorigin - xratio; px >= 0; px -= xratio) {
72: g.drawString(Integer.toString(i--), px - 2, py);
73: g.drawLine(px, yorigin - 2, px, yorigin + 2);
74: }
75:
76: i = 1;
77: px = xorigin + 4;
78: for (py = yorigin - yratio; py >= 0; py -= yratio) {
79: g.drawString(Integer.toString(i++), px, py + 4);
80: g.drawLine(xorigin - 2, py, xorigin + 2, py);
81: }
82:
83: i = -1;
84: for (py = yorigin + yratio; py < d.height; py += yratio) {
85: g.drawString(Integer.toString(i--), px, py + 4);
86: g.drawLine(xorigin - 2, py, xorigin + 2, py);
87: }
88:
89: }
90:
91: }