| | |
Java Paint Program Question
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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
My paintComponent method
Thanks!
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
java Syntax (Toggle Plain Text)
private void formMouseDragged(java.awt.event.MouseEvent evt) { if (evt.isAltDown() == false && evt.isControlDown() == false && evt.isMetaDown() == false && evt.isShiftDown() == false) { if (pointCount < points.length) { point = new ColorPoint(getBrushSize(), getColor(), evt.getX(), evt.getY()); points[pointCount] = point; pointCount++; repaint(); } } else if (evt.isMetaDown()) { if (pointCount < points.length) { point = new ColorPoint(getBrushSize(), this.getBackground(), evt.getX(), evt.getY()); points[pointCount] = point; pointCount++; repaint(); } } }
My paintComponent method
java Syntax (Toggle Plain Text)
public void paintComponent (java.awt.Graphics g) { super.paintComponent(g); for (int i = 0 ; i < pointCount ; i++) { g.setColor(points[i].getColor()); g.fillOval(points[i].x, points[i].y, points[i].getBrushS(), points[i].getBrushS()); } }
Thanks!
I remmeber coding it 4 years back from the java book by deitel and deitel.
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
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.
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Solved Threads: 1
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.
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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:
Following is my paintComponent method:
Hope this helps someone
Following are my event handlers:
java Syntax (Toggle Plain Text)
private void formMouseReleased(java.awt.event.MouseEvent evt) { /* *This event handler is used to be able to tell when the user lets go of the mouse button, *which is necessary so the image drawn has separate lines instead of one big mess of connected *things. The color and size of each path are finalized in this event handler. */ if (pathCount < paths.length) { paths[pathCount] = path; //finalize the path if(!delete) pColor[pathCount] = getColor(); //Set the color if not deleting else if (delete) { pColor[pathCount] = this.getBackground(); //Else set the color to the background delete = false; } pSize[pathCount] = getBrushSize(); //Set the brush size for this path pathCount++; path = new GeneralPath(); //Reset the path } stillPressed = false; //Starts a new path next time user drags the mouse } private void formMouseDragged(java.awt.event.MouseEvent evt) { if (!evt.isAltDown() && !evt.isControlDown() && !evt.isMetaDown() && !evt.isShiftDown()) { if (!stillPressed) //Start a new path { path.moveTo(evt.getX(), evt.getY()); stillPressed = true; repaint(); } else if (stillPressed) //Continues the path if user has not let go of the mouse button { path.lineTo(evt.getX(), evt.getY()); repaint(); } } else if (evt.isMetaDown()) //Erase stuff on screen { if (!stillPressed) //Starts a new path, this path will have the color be the same as background (see event handler above) { path.moveTo(evt.getX(), evt.getY()); stillPressed = true; delete = true; repaint(); } else if (stillPressed) //Continues same path { path.lineTo(evt.getX(), evt.getY()); repaint(); } } }
Following is my paintComponent method:
java Syntax (Toggle Plain Text)
public void paintComponent (java.awt.Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // Necesary for the setStroke method for (int i = 0 ; i < pathCount ; i++) //Draws existing paths { stroke = new BasicStroke(pSize[i], stroke.CAP_ROUND, stroke.JOIN_ROUND); g.setColor(pColor[i]); g2.setStroke(stroke); g2.draw(paths[i]); } //The following draws the current path the user is working on stroke = new BasicStroke(getBrushSize(), stroke.CAP_ROUND, stroke.JOIN_ROUND); if (!delete) g.setColor(getColor()); //Uses user-defined color if not deleting else if (delete) g.setColor(this.getBackground()); //Uses background as color if deleting (right-click) g2.setStroke(stroke); g2.draw(path); }
Hope this helps someone
Last edited by mdelaossa; Feb 28th, 2008 at 1:45 am.
![]() |
Similar Threads
- Applet Help (Java)
- methods (Java)
- Java Applet Help (Java)
Other Threads in the Java Forum
- Previous Thread: String to int
- Next Thread: Construct simple compiler
Views: 2300 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api apple applet application arguments array arrays automation binary blackberry block bluetooth chat class classes client code component database detection developmenthelp draw eclipse encode error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie number object online oracle os page print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing template test textfields threads time title transfer tree tutorial-sample update windows working






