public class TryPolyLine
1: //TryPolyLine.java
2:
3: import java.io.*;
4:
5: public class TryPolyLine
6: {
7: public static void main(String[] args)
8: {
9: //Create an array of coordinate pairs
10: double[][] coords = { {1., 1.}, {1., 2.}, { 2., 3.},
11: {-3., 5.}, {-5., 1.}, {0., 0.} };
12:
13: //Create a polyline from the coordinates and display it
14: PolyLine polygon = new PolyLine(coords);
15: System.out.println(polygon);
16:
17: //Add a point and display the polyline again
18: polygon.addPoint(10., 10.);
19: System.out.println(polygon);
20:
21: //Create Point objects from the coordinate array
22: Point[] points = new Point[coords.length];
23: for(int i = 0; i < points.length; i++)
24: points[i] = new Point(coords[i][0],coords[i][1]);
25:
26: //Use the points to create a new polyline and display it
27: PolyLine newPoly = new PolyLine(points);
28: System.out.println(newPoly);
29:
30: //Write both polyline objects to the file
31: try
32: {
33: //Create the object output stream
34: ObjectOutputStream objectOut =
35: new ObjectOutputStream(
36: new BufferedOutputStream(
37: new FileOutputStream("Polygons.bin")));
38:
39: objectOut.writeObject(polygon); //Write first object
40: objectOut.writeObject(newPoly); //Write second object
41: objectOut.close(); //Close the output stream
42:
43: }
44: catch(IOException e)
45: {
46: e.printStackTrace(System.err);
47: System.exit(1);
48: }
49:
50: //Read the objects back from the file
51: System.out.println("\nReading objects from the file: ");
52: try
53: {
54: ObjectInputStream objectIn =
55: new ObjectInputStream(
56: new BufferedInputStream (
57: new FileInputStream("Polygons.bin")));
58:
59: PolyLine theLine = (PolyLine)objectIn.readObject();
60: System.out.println(theLine); //Display the first object
61: theLine = (PolyLine)objectIn.readObject();
62: System.out.println(theLine); //Display the second object
63: objectIn.close(); //Close the input stream
64:
65: }
66: catch(ClassNotFoundException e)
67: {
68: e.printStackTrace(System.err);
69: System.exit(1);
70:
71: }
72: catch(IOException e)
73: {
74: e.printStackTrace(System.err);
75: System.exit(1);
76: }
77: }
78: }