• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic