• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having trouble making simple drawing application  RSS feed

 
Andrea Menjivar
Greenhorn
Posts: 25
1
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to create a simple drawing application for my assignment where the user can select one of three colors to draw in. To activate the "pen", the user needs to click on the window, then drag the mouse around (NOT pressed down) to draw lines on the screen. I'm having a hard time with a few things. One of them is when changing the colors, I'd like to change the color from that point on (after the user selects a different color) instead of all of the previous points (drawing) the user made. I'm also having a hard time implementing the mouse listeners correctly. I'm not sure how to activate the mouseMoved function after the user clicks on the screen (mousePressed?) and deactivate mouseMoved when the user clicks a second time.
Here is what I have so far (any help is appreciated!):


 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a flag to show that you are in a paint scenario or not, so once you click, you set the flag another click resets the flag.  If your flag is set, then you draw when you move.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An idea is to have a dedicated class, say MyLine, with fields diameter, color and a List<Point2D>.
Each time a mouse is pressed, create a new MyLine instance, with color the selected color and possibly with a diameter equal to a selected strokewidt, add it to a List<MyLine>, and on each mouseMoved, add the mouseX and mouseY to the current MyLine instance.

In the paintComponent, simply draw each MyLine instance from the List<MyLine>. It goes without saying that a MyLine instance knows how to draw itself, using its color and strokewidth.
Now, that means that you can have as many MyLines as you wish, but that the redrawing after each mouseMoved may become a little slow. As soon as you notice this effect let us know.

But foremost: implement what Les suggested: have a boolean that indicates whether you are drawing or not. If you are not drawing, then it might be an idea to remove the mouseMotionListener, to be added again after the next mousePressed event.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!