Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add free hand drawing to arrayList  RSS feed

 
Charlie Wall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am buildning a paint application in java, where the user can choose from a combobox wheather he/she wants to draw a rectangle or by free hand. The objects are stored in an arrayList. The user can also choose in the menubar to go back one step, which means to remove the last drawn item from the arrayList and then loop through the arraylist to draw the remaining objects.
It works fine with rectangles. When I create a rectangle it creates only one rectangle, which I add to the arrayList.
My problem is with the free hand. When I draw by free hand (using mousePressed and mouseDragged) I add every "dot" of the line as an object. Which means that when I remove the last item in the arrayList it only removes one dot of the line, not the whole line.
I need to add the dots together as one line, but how can I do that?

Thank you for your help, I really appreciate it.


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual approach would be to create a java.awt.Polygon from all the points, and to store that instead of storing all points individually.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch

If your freehand drawing is not a Polygon, you should create an object to encapsulate it. Add that object, which encapsulates all the points, to the List.
You may do well to create a Shape (or similar) interface and have all your different shapes implement that interface.

Only use // comments for short things. For anything of any length use /* comments */. That way you won't have problems with line length. I shall try to change your comment's format.
 
Charlie Wall
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:… and welcome to the Ranch

If your freehand drawing is not a Polygon, you should create an object to encapsulate it. Add that object, which encapsulates all the points, to the List.
You may do well to create a Shape (or similar) interface and have all your different shapes implement that interface.

Only use // comments for short things. For anything of any length use /* comments */. That way you won't have problems with line length. I shall try to change your comment's format.



Thank you, I will use /* instead of // in the future.
My drawing is not a Polygon, it is a line. Mayby I should change it to Polygon instead? Do you think that will solve my problem?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A freehand line is a succession of (x,y) points, so a Polygon is a good choice of data structure. It would solve the problem of having one object in the list per "shape" that is drawn.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Custom Painting Approaches for one way to draw a Rectangle freehand.

Basically you just keep track of the point where the mouse was click and then as you drag the mouse you use the drawRect(...) method. Finally when the mouse is released you use the end point and starting point to form your Rectangle and you store the Rectangle in the ArrayList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!