• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I am trying to read Point objects from .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 ?

// inside the method to read points objects from .drw file

 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your code. Always use them. Doesn't it look better like that
If you have a FileInputStream, don't call it fos! I presume you are closing the Streams in a finally after the try?

I think we need more details to understand the problem.
What format are the points after you have read them from that file? Have you displayed any points, e.g. by printing them to the screen? Is that .drw file a serialised file? What did you serialise into the file? What are x1 x2 y1 and y3? Where do you get them from? Why have you got + 1 in the header of the for loop?

By the way: give paintComponent protected access not public.
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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.


 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted the duplicate post.

And what are you getting out of the file? What format is the file? Are you actually getting points? Can you print those points to screen? What sort of point are they? Are they java.awt.Points? Why have you written + 1 in the loop (line 44)? Doesn't that produce Exceptions? Why are you not closing the stream in a finally? You appear to be reading something from the file and not using it (line 18).
Why put those points into an array? Why not a List<Point>?
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I deleted the duplicate post.

And what are you getting out of the file? What format is the file? Are you actually getting points? Can you print those points to screen? What sort of point are they? Are they java.awt.Points? Why have you written + 1 in the loop (line 44)? Doesn't that produce Exceptions? Why are you not closing the stream in a finally? You appear to be reading something from the file and not using it (line 18).
Why put those points into an array? Why not a List<Point>?



getting end points of connected line segments in the drawing where null point indicates a break in the lines.
it's a data file ("square.drw") .. I am not being able to get those points that's where im struggling .
yes, you are right they are java.awt.Point .
That my mistake i should remove the +1 but couldn't find the edit option now .
As far as i know , It's possible using arrayList <Point> but I was not too sure this way .
Could you give some idea in which way would you think it easy to understand . Please help.
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kamal lamgade wrote: . . .
getting end points of connected line segments in the drawing where null point indicates a break in the lines.
Have you found classes like GeneralPath? I think that may be legacy code now, but there is something else similar

it's a data file ("square.drw") .. I am not being able to get those points that's where im struggling .
And what happens when you print out the points in the loop

yes, you are right they are java.awt.Point .
That my mistake i should remove the +1 but couldn't find the edit option now .
It has been disabled

As far as i know , It's possible using arrayList <Point> but I was not too sure this way .
Could you give some idea in which way would you think it easy to understand . Please help.
You still haven't provided all the information. What happens when you change line 18 to this?
System.out.println(oos.readObject());
What happens if you create a List?
List<Point> points = new ArrayList<>();
Then you add each Point to the List?
points.add((Point)oos.readObject());
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
kamal lamgade wrote: . . .
getting end points of connected line segments in the drawing where null point indicates a break in the lines.
Have you found classes like GeneralPath? I think that may be legacy code now, but there is something else similar

it's a data file ("square.drw") .. I am not being able to get those points that's where im struggling .
And what happens when you print out the points in the loop

yes, you are right they are java.awt.Point .
That my mistake i should remove the +1 but couldn't find the edit option now .
It has been disabled

As far as i know , It's possible using arrayList <Point> but I was not too sure this way .
Could you give some idea in which way would you think it easy to understand . Please help.
You still haven't provided all the information. What happens when you change line 18 to this?
System.out.println(oos.readObject());
What happens if you create a List?
List<Point> points = new ArrayList<>();
Then you add each Point to the List?
points.add((Point)oos.readObject());


Thanks. I tried the same way and finally at least now its drawing a shape from those point objects in file "square.drw" .
This is how i tried:

// Inside constructor (which has File file as a parameter ). then I read the file "square .drw" wrapping it with ObjectOutputStream , then created a ArrayList of Point object ,added all those Points to the list .


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. SORRY I TRIED TO WRAP WITH CODE TAGS BUT I DON'T KNOW WHERE I MADE MISTAKE.
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, we're making progress
You obviously are reading the points correctly from the file and you are now adding them to the List. It is better not to declare the reference as a particular type of List, however. Better to write
List<Point> pointsList = new ArrayList<>();
The <> only works in Java7+

What do you know about listeners?
What do you know about timers? Beware: there are at least three kinds of Timer in the standard JVM APOI; you probably want this one. Make sure to import it by its full name, without a *. You can read about timers in the Java Tutorials.

I suggest you create a method which draws the square, and call that from your listener. If you have the square drawing in paintComponent, never call that method. Try
loadPoints(); repaint();
The repaint method calls paintComponent. You will need something different if you are using a Timer.

I think you should try developing your app bit by bit. If you try listeners and timers together, you will get all confused. Get the app working without the timer and consider adding the timer later.


Moving this discussion to our GUIs forum.
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. Yes , Progressing . Thanks a lot.

Yes , I know about Listener . I used actionLister and inside actionPerformed method I tried :
if (source == drawButton)
{
repaint();
}
But, it didn't work. I reckon it's because as per the question I have got a drawing class where the paintComponent draws square using Points from file and I have another DrawingDisplayer class with GUI where i have draw button. So , I am trying to connect this two. I used DrawingDisplayer extends Drawing and also created an object of that class then did the code above (repaint()) but didn't work.
Could you please help how can I connect these those class and get the drawing to display on panel when i click draw button?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like anything with getSource in. You end up with lots of == and lots of if statements and the method looks a right mess if you have lots of buttons. Never mind that you find that sort of thing in books or the Java Tutorials, I still think it is non‑object‑oriented programming.

I think the object‑oriented way to do it is:-
Add the listener to the button.

Or alternatively the functional way to do it is:-
Add the function code to the button,
but the functional way only works in Java8.

Before you try adding any Listeners: I presume you have a method which loads the points from the file into the List. (Or you are using the same points unchanged.) And I presume you are drawing the points in the paintComponent method. And I presume it all works without Exceptions etc.

You will not simply get repaint() to work because you have not loaded the points into the List. You should have a blank screen appear and you should have a method which loads the points from a file and you should have a method (here it will be an overridden paintComponent method) which draws the lines. As I said earlier you need to call the loadPoints (or whatever) method before you call repaint().
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know where your classes are, so I cannot comment about Drawing classes or similar.
I don't like extending Swing classes. A button is a button and you should not need to override any of its methods. A frame is a frame and you should not need to subclass it.

One frame object per GUI. You can add panels, buttons, labels, etc etc to that frame.You can create a class called PointsPanel which extends JPanel and it can be a public class, a package‑private class in the same package, or a private inner class. There is even an example of how to write it an an anonymous class here. But paintComponent should have protected access.
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// In my drawingDisplayer where I have created all my controls , inside the constructor I have this for draw JButton with adding ActionListener

// And, Outside the constructor I implemented Listenr to ButtonHandler class. Now, As I have added the points to list in constructor but not inside a seprate
//method , how can i call the action inside the actionPerformed method to draw that square when i click the button?? please help.

P.S : And, What's my mistake in code tags ? Isn't it correct?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, don't quote everything and don't write code tasgs by hand. I have removed the unnecessary quotes and corrected the code tags.

I also told you that simply calling repaint will not work; you need to call a method to load the points from the file.
Your class is poorly named: ButtonListener does not tell anybody what the class does.
It is incorrect to make the class extend a panel. It should simply implement the interface. And if you wish to call instance methods like repaint, it does not “know” where to find those methods. It can find those methods if you pass a reference to the square drawing object to it, or if it is a [private] inner class.
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . don't quote everything and don't write code tasgs by hand. . . .


Thanks I will try that in next quote .
Did you mean calling the (Point)ois.readObject(); method ?? inside actionPerformed ?
I am not sure which one you mean because i have done those inside a constructor not inside a method. Is it better you do that inside a method ?
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have done that in the constructor then the List is regarded as a standard field in the object and the overridden paintComponent method will draw the square straight off. In which case, why do you want a Listener at all?
 
kamal lamgade
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you have done that in the constructor then the List is regarded as a standard field in the object and the overridden paintComponent method will draw the square straight off. In which case, why do you want a Listener at all?


Thanks a lot . I am finally done with that now it's drawing.
I stored the points in Point1 and Point2 then inside drawLine i did (drawLine ((int)Point1.x , (int) Point1.y , (int) Point2.x , .. so on
 
Campbell Ritchie
Marshal
Posts: 55770
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!