Hi!!
I am trying to read Point objects from square.drw file which contains endpoints of line segments, and draw lines making a square on panel when I run the program. This is what I have got so far. Could you please give some ideas on how to get the points using array for drawing ?
// Initializing fields private Point[] endPoints ; FileInputStream fos; ObjectInputStream oos; int sampleSize = 0; / Method to read Point objects from the square.drw file public void readFile(File file) { try { fos = new FileInputStream("square.drw"); oos = new ObjectInputStream(fos); while(fos.available() > 0) { oos.readObject(); } //oos.close(); } catch(ClassNotFoundException e) { JOptionPane.showMessageDialog(this, e.getMessage() , "can't read this file" , JOptionPane.ERROR_MESSAGE); } catch(IOException e) { JOptionPane.showMessageDialog(this , e.getMessage() , "Error opening file" , JOptionPane.ERROR_MESSAGE); } } // method to insert those points in an array public void fillArray(File file) throws ClassNotFoundException { try { fos = new FileInputStream("square.drw"); oos = new ObjectInputStream(fos); endPoints = new Point[sampleSize]; // creating an object of Point class // Creating the Points to put in the array (Deserialize the objects) for(int i = 0 ; i < endPoints.length+1 ; i++ ) { endPoints[i] = (Point) oos.readObject(); endPoints[sampleSize-i+1] = new Point(); // initializing the value before accessing it // now setting the values , since the point's aren't null endPoints[0].x = x1; // sample points endPoints[0].y = y1; endPoints[1].x = x2; endPoints[1].y = y2; // how to initialize every point in the array using for?? } //oos.close(); } catch(IOException e) { JOptionPane.showMessageDialog(this , e.getMessage() , "Error opening file" , JOptionPane.ERROR_MESSAGE); } }public void paintComponent(Graphics g) { // draw parts of the drawing using lineColour super.paintComponent(g); //Point p = new Point[endPoints]; int numPoints = endPoints.length; for(int i = 0; i < numPoints ; i++) { g.drawLine((int)x1, (int)y1, (int)x2, (int)y2); } }