Java Paint Program Question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 5
Reputation: mdelaossa is an unknown quantity at this point 
Solved Threads: 0
mdelaossa mdelaossa is offline Offline
Newbie Poster

Java Paint Program Question

 
0
  #1
Feb 20th, 2008
Hello,

In my java class we've been asked to create a painter program that allows the user to draw on the screen (as with a paintbrush). I had no trouble creating the program and having it perform up to standards with what was asked, but something that troubles me and that the teacher could not really answer for me was this: how do I manage to make the thing drawn be fluid lines instead of random dots?

The program performs great when drawing slowly, but when drawing fast, the image drawn becomes 'dotted' and has a lot of gaps. Any help scratching this itch would be greatly appreciated.

p.s. I've tried rendering off-screen first, same results. I believe the problem is the way the mouse event is handled, but I have no idea how else to handle it (app must draw when left mouse button is down and erase when right mouse button is down)

Here is the relevant part of my code:

Handling the mouse dragged event
  1. private void formMouseDragged(java.awt.event.MouseEvent evt)
  2. {
  3.  
  4. if (evt.isAltDown() == false && evt.isControlDown() == false && evt.isMetaDown() == false && evt.isShiftDown() == false)
  5. {
  6. if (pointCount < points.length)
  7. {
  8. point = new ColorPoint(getBrushSize(), getColor(), evt.getX(), evt.getY());
  9. points[pointCount] = point;
  10. pointCount++;
  11. repaint();
  12. }
  13. }
  14. else if (evt.isMetaDown())
  15. {
  16. if (pointCount < points.length)
  17. {
  18. point = new ColorPoint(getBrushSize(), this.getBackground(), evt.getX(), evt.getY());
  19. points[pointCount] = point;
  20. pointCount++;
  21. repaint();
  22. }
  23. }
  24.  
  25. }

My paintComponent method
  1. public void paintComponent (java.awt.Graphics g)
  2. {
  3.  
  4. super.paintComponent(g);
  5.  
  6. for (int i = 0 ; i < pointCount ; i++)
  7. {
  8.  
  9. g.setColor(points[i].getColor());
  10. g.fillOval(points[i].x, points[i].y, points[i].getBrushS(), points[i].getBrushS());
  11. }
  12.  
  13. }

Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,867
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Java Paint Program Question

 
-1
  #2
Feb 20th, 2008
I remmeber coding it 4 years back from the java book by deitel and deitel.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: mdelaossa is an unknown quantity at this point 
Solved Threads: 0
mdelaossa mdelaossa is offline Offline
Newbie Poster

Re: Java Paint Program Question

 
0
  #3
Feb 20th, 2008
That... doesn't really help much, but thanks anyways, I guess I'll try to see if any examples for that book are around on the 'net....

Anyone else?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java Paint Program Question

 
0
  #4
Feb 20th, 2008
Your best bet is to construct a GeneralPath with the points from the mouse listener and use the Graphics2D.draw(Shape) method to render it. You can append to the GeneralPath as new points are added, so you don't have to even maintain a point collection at all.

Examples of this can be found here: http://java.sun.com/docs/books/tutor...arbitrary.html
Last edited by Ezzaral; Feb 20th, 2008 at 1:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: mdelaossa is an unknown quantity at this point 
Solved Threads: 0
mdelaossa mdelaossa is offline Offline
Newbie Poster

Re: Java Paint Program Question

 
0
  #5
Feb 20th, 2008
That sounds great, the only problem is that if I do it that way, the width can not be changed, and that's a necessary feature for the program
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Java Paint Program Question

 
0
  #6
Feb 20th, 2008
You can use a stroked shape for that: http://java.sun.com/javase/6/docs/ap...java.awt.Shape)
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: mdelaossa is an unknown quantity at this point 
Solved Threads: 0
mdelaossa mdelaossa is offline Offline
Newbie Poster

Re: Java Paint Program Question

 
0
  #7
Feb 21st, 2008
Thank you SO MUCH!! I'll try this out tomorrow
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1
Reputation: sandeep sidhu is an unknown quantity at this point 
Solved Threads: 1
sandeep sidhu sandeep sidhu is offline Offline
Newbie Poster

Re: Java Paint Program Question

 
0
  #8
Feb 21st, 2008
hi sir;
my name is sandeep . i recently cleared scjp exam . sir now i am looking to start a porject with java can you suggest me something.
and sir what was that paint program question.

and if you can give me some hint for this it will be good for me. not code but some general lines how to handle it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: mdelaossa is an unknown quantity at this point 
Solved Threads: 0
mdelaossa mdelaossa is offline Offline
Newbie Poster

Re: Java Paint Program Question

 
1
  #9
Feb 28th, 2008
Just in case anybody wishes to know, I ended up creating an array of GeneralPaths, an array of int for brushsizes, and an array of Color for brush color, plus an extra GeneralPath outside the array to be able to draw as the user inputs.

Following are my event handlers:

  1. private void formMouseReleased(java.awt.event.MouseEvent evt)
  2. {
  3.  
  4. /*
  5.   *This event handler is used to be able to tell when the user lets go of the mouse button,
  6.   *which is necessary so the image drawn has separate lines instead of one big mess of connected
  7.   *things. The color and size of each path are finalized in this event handler.
  8.   */
  9.  
  10. if (pathCount < paths.length)
  11. {
  12. paths[pathCount] = path; //finalize the path
  13. if(!delete)
  14. pColor[pathCount] = getColor(); //Set the color if not deleting
  15. else if (delete)
  16. {
  17. pColor[pathCount] = this.getBackground(); //Else set the color to the background
  18. delete = false;
  19. }
  20. pSize[pathCount] = getBrushSize(); //Set the brush size for this path
  21. pathCount++;
  22. path = new GeneralPath(); //Reset the path
  23. }
  24. stillPressed = false; //Starts a new path next time user drags the mouse
  25. }
  26.  
  27. private void formMouseDragged(java.awt.event.MouseEvent evt) {
  28.  
  29. if (!evt.isAltDown() && !evt.isControlDown() && !evt.isMetaDown() && !evt.isShiftDown())
  30. {
  31. if (!stillPressed) //Start a new path
  32. {
  33. path.moveTo(evt.getX(), evt.getY());
  34. stillPressed = true;
  35. repaint();
  36. }
  37. else if (stillPressed) //Continues the path if user has not let go of the mouse button
  38. {
  39. path.lineTo(evt.getX(), evt.getY());
  40. repaint();
  41. }
  42.  
  43. }
  44. else if (evt.isMetaDown()) //Erase stuff on screen
  45. {
  46. if (!stillPressed) //Starts a new path, this path will have the color be the same as background (see event handler above)
  47. {
  48. path.moveTo(evt.getX(), evt.getY());
  49. stillPressed = true;
  50. delete = true;
  51. repaint();
  52. }
  53. else if (stillPressed) //Continues same path
  54. {
  55. path.lineTo(evt.getX(), evt.getY());
  56. repaint();
  57. }
  58. }
  59.  
  60. }

Following is my paintComponent method:

  1. public void paintComponent (java.awt.Graphics g)
  2. {
  3.  
  4. super.paintComponent(g);
  5.  
  6. Graphics2D g2 = (Graphics2D) g; // Necesary for the setStroke method
  7.  
  8. for (int i = 0 ; i < pathCount ; i++) //Draws existing paths
  9. {
  10. stroke = new BasicStroke(pSize[i], stroke.CAP_ROUND, stroke.JOIN_ROUND);
  11. g.setColor(pColor[i]);
  12. g2.setStroke(stroke);
  13. g2.draw(paths[i]);
  14. }
  15.  
  16. //The following draws the current path the user is working on
  17.  
  18. stroke = new BasicStroke(getBrushSize(), stroke.CAP_ROUND, stroke.JOIN_ROUND);
  19. if (!delete)
  20. g.setColor(getColor()); //Uses user-defined color if not deleting
  21. else if (delete)
  22. g.setColor(this.getBackground()); //Uses background as color if deleting (right-click)
  23. g2.setStroke(stroke);
  24. g2.draw(path);
  25. }

Hope this helps someone
Last edited by mdelaossa; Feb 28th, 2008 at 1:45 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 2300 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC