Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: mouseDragged and drawOval problem

  1. #1
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default mouseDragged and drawOval problem

    i simple drawing program, so every time i drag the mouse it draws filled in circles but when i do it rapidly it doesnt draw them continuously it just makes a dotted line

    Java Code:
    addMouseListener(new MouseAdapter() {
    			public void mousePressed(MouseEvent e) {
    				oldX = e.getX();
    				oldY = e.getY();
    				g2d.fillOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    			}
    		});
    
    		addMouseMotionListener(new MouseMotionAdapter() {
    			public void mouseDragged(MouseEvent e) {
    				currentX = e.getX();
    				currentY = e.getY();
    				
    				g2d.setStroke(new BasicStroke(strokeWidth));
    				if (g2d != null)
    					g2d.drawOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    				oldX = currentX;
    				oldY = currentY;
    			}
    
    		});
    ignore the oldX and oldY.
    thanks!

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

    Default Re: mouseDragged and drawOval problem

    For the best chance at rapid helpful help, consider creating and posting an SSCCE
    DarrylBurke likes this.

  3. #3
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: mouseDragged and drawOval problem

    Okay i read over that and this is the smallest could get. Any help would be appreciated!

    Java Code:
    addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    g2d.fillOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
                    revalidate();
                    repaint();
                }
            });
     
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    currentX = e.getX();
                    currentY = e.getY();
                     
                    g2d.setStroke(new BasicStroke(strokeWidth));
                    if (g2d != null)
                        g2d.drawOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
                    revalidate();
                    repaint();
                }
     
            });

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: mouseDragged and drawOval problem

    Okay i read over that and this is the smallest could get
    You got the first letter of that acronym, but are missing the rest eg Self-contained and Correct. We can what the variables you posted represent, but why waste time guessing when you can post an example we can actually look at, understand, and actually try and run to reproduce the problem?

  5. #5
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: mouseDragged and drawOval problem

    OKay sorry about that,

    Java Code:
    /**
     * 
     */
    package testpackage;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.image.BufferedImage;
    
    import javax.swing.JComponent;
    
    /**
     * @author Alejandro
     * 
     */
    public class DrawingPad extends JComponent {
    
    	private BufferedImage image;
    	private Graphics2D g2d;
    	private int strokeWidth;
    
    	public DrawingPad() {
    
    		strokeWidth = 1;
    		setDoubleBuffered(false);
    
    		addMouseListener(new MouseAdapter() {
    			public void mousePressed(MouseEvent e) {
    				g2d.fillOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    			}
    		});
    
    		addMouseMotionListener(new MouseMotionAdapter() {
    			public void mouseDragged(MouseEvent e) {
    				g2d.setStroke(new BasicStroke(strokeWidth));
    				if (g2d != null)
    					g2d.drawOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    			}
    
    		});
    	}
    
    	public void paintComponent(Graphics g) {
    		if (image == null) {
    			image = (BufferedImage) createImage(getSize().width,
    					getSize().height);
    
    			g2d = (Graphics2D) image.getGraphics();
    
    			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON);
    
    			clear();
    
    		}
    
    		g.drawImage(image, 0, 0, null);
    	}
    
    	public void clear() {
    
    		g2d.setPaint(Color.white);
    		g2d.fillRect(0, 0, getSize().width, getSize().height);
    		g2d.setPaint(Color.black);
    		repaint();
    	}
    
    	public void changeColor(Color theColor) {
    		g2d.setPaint(theColor);
    		repaint();
    	}
    
    	public void setStrokeWidth(int x) {
    		strokeWidth = x;
    	}
    
    }
    should i also put the main method that adds this the jframe ? or is this good enough

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: mouseDragged and drawOval problem

    1. The frequency of MouseEvents is determined by the reporting frequency of the underlying OS. With rapid mouse movement, you will not get a MouseEvent for every pixel moved.
    2. You shouldn't be initializing a BufferedImage in a painting method override. Since the BufferedImage is a field, you could create it in the constructor.
    3. Unused methods changeColor() and setStrokeWidth(...). So much for short.

    Why do you setDoubleBuffered(false)?

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: mouseDragged and drawOval problem

    So for 1. there is no way to get around that?
    2. So i try to initiate the Buffered Image in the constructor but i received several errors, first exception errors, but then i added a try catch to it, the program ran but as i tried to paint something on the screen my console just began to throw bunch of errors.
    3. the changeColor() and setStrokeWidth() methods are used in another class

    i was following an example where they set that to false, but i tried it with true and it worked alot better.

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

    Default Re: mouseDragged and drawOval problem

    Quote Originally Posted by ale626 View Post
    So for 1. there is no way to get around that?
    No since it is again it is not controlled by Java, but regardless there's no need to either.

    2. So i try to initiate the Buffered Image in the constructor but i received several errors, first exception errors, but then i added a try catch to it, the program ran but as i tried to paint something on the screen my console just began to throw bunch of errors.
    If you need help with errors, then by all means show us the error message and the erroneous code.

    3. the changeColor() and setStrokeWidth() methods are used in another class
    If they're not necessary for the SSCCE, then please leave them out.

  9. #9
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: mouseDragged and drawOval problem

    Okay so i have worked on it and got it down to a couple of nullpointerexceptions

    Java Code:
    /**
     * 
     */
    package testpackage;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.image.BufferedImage;
    import javax.swing.JComponent;
    
    public class DrawingPad extends JComponent {
    
    	private Image image;
    	private Graphics2D g2d;
    	private int strokeWidth;
    
    	public DrawingPad() {
    		if (image == null) {
    			image = createImage(getSize().width,
    					getSize().height);
    
    			try{
    			g2d = (Graphics2D) image.getGraphics();
    
    			g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    					RenderingHints.VALUE_ANTIALIAS_ON);
    			}catch(Exception e){
    				System.out.println("Error");
    			}
    			
    
    			clear();
    
    		}
    		strokeWidth = 1;
    		addMouseListener(new MouseAdapter() {
    			public void mousePressed(MouseEvent e) {
    				g2d.fillOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    			}
    		});
    
    		addMouseMotionListener(new MouseMotionAdapter() {
    			public void mouseDragged(MouseEvent e) {
    				g2d.setStroke(new BasicStroke(strokeWidth));
    				if (g2d != null)
    					g2d.drawOval(e.getX(), e.getY(), strokeWidth, strokeWidth);
    				revalidate();
    				repaint();
    			}
    
    		});
    	}
    
    	public void paintComponent(Graphics g) {
    
    		g.drawImage(image, 0, 0, null);
    	}
    
    	public void clear() {
    
    		try{
    			g2d.setPaint(Color.white);
    			g2d.fillRect(0, 0, getSize().width, getSize().height);
         		        g2d.setPaint(Color.black);
    		}catch(Exception e){
    			
    		}
    		
    		
    		repaint();
    	}
    
    }
    this is the result in the console without the try catches
    but with them i just get the the printed message Error over and over

    Error
    Exception in thread "main" java.lang.NullPointerException
    at testpackage.DrawingPad.clear(DrawingPad.java:82)
    at testpackage.DrawingPad.<init>(DrawingPad.java:44)
    at testpackage.DrawMain.main(DrawMain.java:31)

    So its printing out error, so means im not creating the Graphics2D, i tried leaving it in the paintComponent as well but i recieve the same error
    cant seem to find a way to fix this

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: mouseDragged and drawOval problem

    Post the stack trace that corresponds to the code. Line 82 in the posted code is the final closing brace, and that can't possibly throw a NullPointerException.

    Also, there are JComponent methods getWidth() and getHeight(). There's no need to obtain the width and height from the size. Makes the code a little cleaner.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: mouseDragged and drawOval problem

    Sorry about that, i removed some methods for the whole short thing. line 82 should align with the clear method

Similar Threads

  1. Can't it be drawOval( int, double, int, int) ?
    By Josep_16 in forum Java Applets
    Replies: 1
    Last Post: 08-08-2011, 12:36 PM
  2. mouseDragged problem
    By ddatta8 in forum AWT / Swing
    Replies: 0
    Last Post: 11-15-2010, 02:41 AM
  3. How to duplicat drawOval horizontally?
    By ntagrafix in forum AWT / Swing
    Replies: 1
    Last Post: 12-03-2009, 10:37 PM
  4. Replies: 5
    Last Post: 11-02-2008, 07:41 AM
  5. MouseDragged Event
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 03-04-2008, 06:09 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
  •