Results 1 to 15 of 15
  1. #1
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default drawing Line by dragging mouse !!!

    Hello guys,
    I have a class that extends JComponent. My problem is i can't draw line ( such a pencil ) by dragging mouse -- there is only one dot moved. Give me reasons ! Thank you !

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    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

  3. #3
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    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 !

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    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

  5. #5
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Custom Painting Approaches gives a working example you should be able to customize.

  6. #6
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    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());
    }
    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by h9h View Post
    I did try this but still not succeed..
    Best to show us the code and the specific problem.

    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
    Huh? This doesn't make much sense.

  8. #8
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Best to show us the code and the specific problem.


    Huh? This doesn't make much sense.
    The code is above ( on top )

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Correction: best to show us compilable code . And please don't forget the code tags.
    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);
    	}
    }
    }

  11. #11
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Correction: best to show us compilable code . And please don't forget the code tags.
    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);
    	}
    }
    }

  12. #12
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    That is not 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.
    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.

  13. #13
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    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.
    It's helpful. But my pencil still doesn't work. ( rectangle is ok ! )

  14. #14
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    thank all ! I have fixed my problem ! that is because i used the wrong algorithm !

  15. #15
    h9h
    h9h is offline Member
    Join Date
    Oct 2009
    Posts
    19
    Rep Power
    0

    Default

    [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

  1. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  2. drawing with mouse
    By aveek in forum Java 2D
    Replies: 1
    Last Post: 06-27-2009, 01:38 PM
  3. drawing points with mouse
    By josephdcoleman in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 12:39 AM
  4. Scaling-ache and mouse dragging
    By willemjav in forum Java Applets
    Replies: 19
    Last Post: 07-19-2008, 12:17 AM
  5. Help with Drawing a line
    By Rgfirefly24 in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •