• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating an 'eraser' function for a drawing app...

 
Scott Presley
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little stuck and thought I would see if anyone out there has any suggestions for this...

I have a simple panel that I'm allowing the user to draw a line while dragging the mouse inside the 'drawable' area. I'm using the mousePressed, mouseReleased, and MouseDragged methods to capture a series of points and drawing lines between those points during above mentoined mouseXX methods as well as in the paintComponents() method.

Each 'full draw stoke', (press+drag+release) is stored as a separate array of points. This gives me the ability to undo the last line drawn. This undo seems a little to drastic in the case that a lot of drawing took place before the mouse was released.

I'd like to make an eraser that would function as prety much the reverse of the draw, removing any previously drawn lines in the path of a drag of the 'eraser'.

initially I was simply drawing in the mouseXX methods, but on a repaint the lines would be lost. Now that I'm storing arrays of points, I can iterate through these arrays and redraw my lines in the paintComponent() method. This tatic makes an eraser all but impossible? I was initially thinking of simply drawing another line on top, in the same color as the background, but the uses for this drawing panel will vary such that there may be a gradient background or even an image as the background...

I may be approaching the whole thing wrong with recording the points the way that I am... does anyone have any suggestions?

Thanks in advance,
Scott
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're kind of mixing metaphors. Computer graphics programs fall into two categories, generally: "paint" programs and "draw" programs. "Paint" programs represent a drawing as a bitmap -- an array of screen pixels. Painting is just a matter of coloring pixels in some color, and erasing is the same as painting, except with white paint.

"Drawing" (or "vector graphics") programs do what you're doing: store figures as sets of points, lines, or other primitives. You can select and move primitives, you can edit them, and you can delete them -- but not "erase" them.

So you should probably pick one standard metaphor or another, Either represent a drawing as an array of pixels to be colored in, and implement an eraser as a white paintbrush; or use your current representation, but have a "select" tool and wire the "delete" key and "Edit | Clear" menu item up to delete items.
 
Scott Presley
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the comments... i guess I am mixing up the two... I think I'm going to stick with the vector drawing and just undo the last 'line'
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To remove the last 'full draw stroke' the eraser needs access to it in object form, ie, as a discrete object. You might achieve this by making each 'full draw stroke' a GeneralPath: moveTo the first mouse position and lineTo the rest. Then the eraser can remove the last GeneralPath from your Vector or ArrayList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!