Grapherpublic class Grapher extends JPanel
Fields Summary |
---|
public static final float | BORDERFACTORMultiplier for range to allow room for a border | protected Vector | dataThe list of Apoint points. | protected float | minxThe minimum and maximum X values | protected float | maxx | protected float | minyThe minimum and maximum Y values | protected float | maxy | protected int | nThe number of data points | protected float | xrangeThe range of X and Y values | protected float | yrange |
Constructors Summary |
---|
public Grapher()
data = new Vector();
|
Methods Summary |
---|
public java.awt.Dimension | getPreferredSize()
return new Dimension(150, 150);
| public static void | main(java.lang.String[] rgs)
final JFrame f = new JFrame("Grapher");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.setVisible(false);
f.dispose();
System.exit(0);
}
});
Grapher g = new Grapher();
f.setContentPane(g);
f.setLocation(100, 100);
f.pack();
if (rgs.length == 0)
g.read("Grapher.dat");
else
g.read(rgs[0]);
f.setVisible(true);
| public void | paintComponent(java.awt.Graphics g)Called when the window needs painting.
Computes X and Y range, scales.
super.paintComponent(g);
Dimension s = getSize();
if (n < 2) {
g.drawString("Insufficient data", 10, 40);
return;
}
// Compute scale factors
float xfact = s.width / xrange;
float yfact = s.height / yrange;
// Scale and plot the data
for (int i=0 ; i < n; i++) {
Apoint d = (Apoint)data.elementAt(i);
float x = (d.x-minx) * xfact;
float y = (d.y-miny) * yfact;
Debug.println("point", "AT " + i + " " + d + "; " +
"x = " + x + "; y = " + y);
// Draw a 5-pixel rectangle centered, so -2 both x and y.
// AWT numbers Y from 0 down, so invert:
g.drawRect(((int)x)-2, s.height-2-(int)y, 5, 5);
}
| public void | read(java.lang.String fname)Read the data file named. Each line has an x and a y coordinate.
LineNumberReader is = null;
try {
is = new LineNumberReader(new FileReader(fname));
String txt;
// Read the file a line at a time, parse it, save the data.
while ((txt = is.readLine()) != null) {
StringTokenizer st = new StringTokenizer(txt);
try {
Apoint d = new Apoint();
d.x = Float.parseFloat(st.nextToken());
d.y = Float.parseFloat(st.nextToken());
data.add(d);
} catch(NumberFormatException nfe) {
System.err.println("Invalid number on line " +
is.getLineNumber());
} // XXX catch out of range exception
}
} catch (FileNotFoundException e) {
System.err.println("File " + fname + " unreadable: " + e);
} catch (IOException e) {
System.err.println("I/O error on line " + is.getLineNumber());
}
n = data.size();
if (n < 2) {
System.err.println("Not enough data points!");
return;
}
// find min & max
for (int i=0 ; i < n; i++) {
Apoint d = (Apoint)data.elementAt(i);
if (d.x < minx) minx = d.x;
if (d.x > maxx) maxx = d.x;
if (d.y < miny) miny = d.y;
if (d.y > maxy) maxy = d.y;
}
// Compute ranges
xrange = (maxx - minx) * BORDERFACTOR;
yrange = (maxy - miny) * BORDERFACTOR;
Debug.println("range", "minx,x,r = " + minx +' "+ maxx +' "+ xrange);
Debug.println("range", "miny,y,r = " + miny +' "+ maxy +' "+ yrange);
|
|