Results 1 to 15 of 15
- 10-08-2009, 08:05 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
- 10-08-2009, 08:14 PM #2
probably because you would like the compiler to read your mind as you would like us to read your mind when you don't post any code! :P
My Hobby Project: LegacyClone
- 10-08-2009, 08:21 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
OK ! this is the parts of code to process like a pencil
class DrawingArea extends JComponent implements MouseListener, MouseMotionListener {
....
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startPencilDrag, endPencilDrag;
....
public void mousePressed(MouseEvent me) {
// pencil
if(cases == 6) {
startPencilDrag = new Point(me.getX(),me.getY());
endPencilDrag = startPencilDrag;
}
....
public void mouseReleased(MouseEvent me) {
// pencil
if( cases == 6) {
Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.ge tX(),me.getY());
pencils.add(pencil);
shapes.add(pencil);
startPencilDrag = null;
endPencilDrag = null;
}
...
public void mouseDragged(MouseEvent me) {
// pencil
if( cases == 6) {
startPencilDrag = endPencilDrag;
endPencilDrag = new Point(me.getX(),me.getY());
}
....
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
case 6: // pencil
{
if( startPencilDrag != null && endPencilDrag != null) {
Shape p = makeLine(startPencilDrag.x,startPencilDrag.y,
endPencilDrag.x,endPencilDrag.y);
g2.draw(p);
startPencilDrag = endPencilDrag;
}
//for(Shape s: pencils) {
for(Shape s: shapes) {
g2.draw(s);
}
...
private Line2D.Float makeLine(int x1,int y1,int x2,int y2) {
return new Line2D.Float(x1,y1,x2,y2);
}
that is all codes i do to draw on JComponent like a pencil ! I can't post all code of my program because it is too long ! Thanks !
- 10-08-2009, 08:41 PM #4
Mouse drag event fires off after an interval. So one one drag of the mouse, the mouse event can be fired off many times all at short distances. Unless you drag your mouse fast, your line will only be a few pixels long.
It might be better to store the start point when you initially click the mouse and have the end point the current position of the drag. Or you could store all the points along the drag and draw them all.
Edit:
Try modifying to this:
Java Code:public void mouseDragged(MouseEvent me) { // pencil if( cases == 6) { //startPencilDrag = endPencilDrag; //leave the start alone. endPencilDrag = new Point(me.getX(),me.getY()); }Last edited by mrmatt1111; 10-08-2009 at 08:44 PM.
My Hobby Project: LegacyClone
- 10-08-2009, 08:48 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Custom Painting Approaches gives a working example you should be able to customize.
- 10-09-2009, 03:34 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
I did try this but still not succeed.. as the same code when i use the class extends JPanel it runs well but because i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent ? help me with other approachs on JComponent.
-
Best to show us the code and the specific problem.
Huh? This doesn't make much sense.as the same code when i use the class extends JPanel it runs well but because i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent
- 10-09-2009, 03:59 AM #8
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
-
Correction: best to show us compilable code, an SSCCE (check out the link). And please don't forget the code tags.
- 10-09-2009, 04:25 AM #10
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
OK. this is shortened code but it is compilable
}Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.awt.image.*; import javax.imageio.*; import java.awt.geom.*; import java.util.*; class DrawingArea extends JComponent implements MouseListener, MouseMotionListener { ArrayList<Shape> shapes = new ArrayList<Shape>(); Point startLineDrag,endLineDrag; DrawingArea() { startLineDrag = null; endLineDrag = null; } public void mousePressed(MouseEvent me) { // pencil if(cases == 6) { startPencilDrag = new Point(me.getX(),me.getY()); endPencilDrag = startPencilDrag; } } public void mouseReleased(MouseEvent me) { // pencil if( cases == 6) { Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.getX(),me.getY()); pencils.add(pencil); shapes.add(pencil); startPencilDrag = null; endPencilDrag = null; } } public void mouseDragged(MouseEvent me) { // cap nhat cho pencil if( cases == 6) { startPencilDrag = endPencilDrag; endPencilDrag = new Point(me.getX(),me.getY()); } } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; switch(cases) { case 6: // pencil { for(Shape s: shapes) { g2.draw(s); } if( startPencilDrag != null && endPencilDrag != null) { Shape p = makeLine(startPencilDrag.x,startPencilDrag.y, endPencilDrag.x,endPencilDrag.y); g2.draw(p); startPencilDrag = endPencilDrag; } } private Line2D.Float makeLine(int x1,int y1,int x2,int y2) { return new Line2D.Float(x1,y1,x2,y2); } }
- 10-09-2009, 04:26 AM #11
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
OK. this is shortened code but it is compilable
}Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.io.*; import java.awt.image.*; import javax.imageio.*; import java.awt.geom.*; import java.util.*; class DrawingArea extends JComponent implements MouseListener, MouseMotionListener { ArrayList<Shape> shapes = new ArrayList<Shape>(); Point startLineDrag,endLineDrag; DrawingArea() { startLineDrag = null; endLineDrag = null; } public void mousePressed(MouseEvent me) { // pencil if(cases == 6) { startPencilDrag = new Point(me.getX(),me.getY()); endPencilDrag = startPencilDrag; } } public void mouseReleased(MouseEvent me) { // pencil if( cases == 6) { Shape pencil = makeLine(startPencilDrag.x,startPencilDrag.y,me.getX(),me.getY()); pencils.add(pencil); shapes.add(pencil); startPencilDrag = null; endPencilDrag = null; } } public void mouseDragged(MouseEvent me) { // pencil if( cases == 6) { startPencilDrag = endPencilDrag; endPencilDrag = new Point(me.getX(),me.getY()); } } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; switch(cases) { case 6: // pencil { for(Shape s: shapes) { g2.draw(s); } if( startPencilDrag != null && endPencilDrag != null) { Shape p = makeLine(startPencilDrag.x,startPencilDrag.y, endPencilDrag.x,endPencilDrag.y); g2.draw(p); startPencilDrag = endPencilDrag; } } private Line2D.Float makeLine(int x1,int y1,int x2,int y2) { return new Line2D.Float(x1,y1,x2,y2); } }
- 10-09-2009, 04:55 AM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
That is not a SSCCE.
I gave you a working example of drawing rectangle on a JPanel. Apparently you didn't bother to read that link or the link on how to create a SSCCE.i have shapes like rectangles or circles which do not run wel on JPanel. So i need to keep it extend from JComponent ? help me with other approachs on JComponent.
- 10-09-2009, 05:26 AM #13
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
- 10-09-2009, 10:17 AM #14
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
thank all ! I have fixed my problem ! that is because i used the wrong algorithm !
- 10-23-2009, 05:10 AM #15
Member
- Join Date
- Oct 2009
- Posts
- 19
- Rep Power
- 0
[QUOTE=camickr; gives a working example you should be able to customize.[/QUOTE]
I follow your guide , it works perfectly in my program ( i am developing a program like MS Paint). But problem is when i fill color , i have to use BufferedImage and then i use BufferedImage graphics context to draw. At this moment it works wrong like draw a lot of rectangles from start point. When I use normal Graphis2D context it works on the unfilled area and hide on the filled area. Please camickr help me. I don't have much time anymore.
Similar Threads
-
Major help needed with drawing rectangles using JFrame and Mouse Events
By DamienCurr in forum New To JavaReplies: 1Last Post: 07-16-2009, 02:15 PM -
drawing with mouse
By aveek in forum Java 2DReplies: 1Last Post: 06-27-2009, 01:38 PM -
drawing points with mouse
By josephdcoleman in forum New To JavaReplies: 4Last Post: 03-03-2009, 12:39 AM -
Scaling-ache and mouse dragging
By willemjav in forum Java AppletsReplies: 19Last Post: 07-19-2008, 12:17 AM -
Help with Drawing a line
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 08:40 AM


LinkBack URL
About LinkBacks


Bookmarks