Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Reading point objects from .drw file and drawing lines on panel

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading point objects from .drw file and drawing lines on panel

    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);
    		}
     
     
    	}
    Last edited by klamgade; September 15th, 2014 at 05:16 AM.


  2. #2
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    61
    My Mood
    Cool
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Reading point objects from .drw file and drawing lines on panel

    please wrap your code and ask a spercific question


    read this topic to learn how to wrap your code

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading point objects from .drw file and drawing lines on panel

    Thanks. I tried to wrap the code(hope it looks ok ) and added some more of what I tried..
    My actual question is : I am trying to read Point objects from the data file square.drw which are the endpoints of connected line segments in the drawing (square), where null point indicates a break in the lines. Then, overriding paintComponent method of JPanel and trying to draw parts of the drawing . I am not too sure about the way i tried to insert points in an array.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Reading point objects from .drw file and drawing lines on panel

    Thanks for fixing your code and adding more detail.

    Show an example of the *.drw file and describe what problems you're having, if any. My thoughts are that you'd read 3 (x, y) point pairs, the 3rd would connect back to the first, and then create a Square object with the collected points. The points could be stored if you'd like, but once the Square objects are created, the points aren't needed. The Square object could be collected in a collection which would be iterated and drawn in a paintComponent() method.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading point objects from .drw file and drawing lines on panel

    Thanks . Today , finally I ended up managing to read Points objects from .drw file , store it in an arrayList and later iterate inside the paint Componenet as you said above and drawline using two endPoints as below:
    ArrayList<Point> list = new ArrayList<Point>(); 
     
    while(fis.available() > 0) 
    { 
    Point p = (Point)ois.readObject(); 
    list.add(p); 
    }
    // inside paintComponent(iterate through all the points and draw lines using two endPoints
    for(int i = 0; i < getNumDraw() ; i++) 
    { 
    p1 = endPoints[i]; 
    p2 = endPoints[i+1]; 
     
    g.drawLine((int)p1.x ,(int)p1.y , (int)p2.x , (int)p2.y);

    Now, What I am trying is to set an ActionListener to the draw JButton when clicked, want it to perform the same action to draw a square .. Additionally, after that trying to use timer class and it's method to draw those lines one at a time so when you click the draw button , you can see drawing one line at a time but ending up with a square. If any idea please help. Sorry I tried to wrap the code but don't know where i made mistake.
    Last edited by Norm; September 16th, 2014 at 07:02 AM. Reason: removed spaces in tags

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Reading point objects from .drw file and drawing lines on panel

    If any idea please help
    Can you post code that will compile and execute for testing?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. File reading from a specific point on a line
    By lordofrandom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 13th, 2013, 10:25 PM
  2. Reading lines from a txt file
    By neliJav in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2013, 01:54 PM
  3. Reading a lines from text file
    By kiran in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 26th, 2010, 10:54 PM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 06:49 PM
  5. Reading from a file, multiple lines
    By MysticDeath in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 15th, 2009, 02:40 AM

Tags for this Thread