Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 3 of 3 FirstFirst 123
Results 51 to 75 of 75

Thread: Canvas and MouseListener...

  1. #51
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Look: I press the button e.g Pencil and I draw something on my JPanel. After that, I want to sketch in something on JPanel by brush, so I press the button, which calls Action of Paintbrush. I draw something, and I note that, the older "picture" disappear. It is replaced by new "picture". When I press Pencil button again, latest picture is replaced by new one, and so on.
    Something like that. Sorry for my English in advance. :}

  2. #52
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Use a collection like an ArrayList to save the points used to make the drawing.
    Then save that list in another list.
    That way all the previous drawings will be saved and can be retrieved by going through the list getting the lists it contains to draw the older pictures.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #53
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, should I have to create ArrayList of ArrayList in Drawing class? I have no idea, how to "include" there Arrays from e.g Pencil class (which include drawn points).
    Thanks

    edit//
    I have a problem with NullPointerException. I am looking for an error for two days, and I can not find where is the problem...

    Here is the class:

    public class Lines extends Tool {
     
    	private int x, y;
    	private final ArrayList<Point[]> pointsList = new ArrayList<>();
    	private final ArrayList<Color> Colors = new ArrayList<>();
    	private Drawing drawingArea;
            private Point[] points = new Point[2];
     
    	public Lines() {}
    	public Lines(Drawing drawingArea) {
    			System.out.println("LINES SELECTED!"); 
    			this.drawingArea = drawingArea;
    	}
     
    	// ================================================================= //
     
    	@Override
    	public void paintComponent(Graphics g) {
    		((Graphics2D) g).setStroke(new BasicStroke(2));
                for (Point[] Points : pointsList) {
                    Point p1 = Points[0];
                    Point p2 = Points[1];
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
                }
    	}
     
    	// ================================================================= //
     
    	@Override
    	public void mousePressed(MouseEvent e) {
            points[0] = null;
            points[1] = null;
                pointsList.add(points);
    		Colors.add(GUI.getColour());
    		x = e.getX();
    	    y = e.getY();
            pointsList.get(pointsList.size()-1)[0] = new Point(x, y);
    	}
     
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            pointsList.get(pointsList.size()-1)[1] = new Point(x, y);
    		drawingArea.repaint();
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            pointsList.get(pointsList.size()-1)[1] = new Point(x, y);
    		drawingArea.repaint();
    	}
     
    	@Override
    	public void mouseMoved(MouseEvent e) {}
    	@Override
    	public void mouseClicked(MouseEvent e) {
     
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {}
    	@Override
    	public void mouseExited(MouseEvent e) {}
    }

    and compiler says:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Toolkit.Lines.paintComponent(Lines.java:39)
    	at GUI.Drawing.paintComponent(Drawing.java:43)
    	at javax.swing.JComponent.paint(JComponent.java:1053)
    	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1532)
    	at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1455)
    	at javax.swing.RepaintManager.paint(RepaintManager.java:1252)
    	at javax.swing.JComponent._paintImmediately(JComponent.java:5165)
    	at javax.swing.JComponent.paintImmediately(JComponent.java:4976)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:811)
    	at javax.swing.RepaintManager$3.run(RepaintManager.java:794)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:794)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    	at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1680)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    	at java.awt.EventQueue.access$400(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:697)
    	at java.awt.EventQueue$3.run(EventQueue.java:691)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

    edit:// I noted, that Points[1] is null, but I don't know why...

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Toolkit.Lines.paintComponent(Lines.java:39)
    What variable on line 39 has a null value when that line is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #55
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, debugger said, that
                    Point p2 = Points[1];
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);
    there is null.

  6. #56
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    You posted two lines. Which one is line 39? What variable on line 39 had a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #57
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, it is line 39:
                    g.drawLine(p1.x, p1.y, p2.x, p2.y);

    Point p2 and Points[1] had a null value.

  8. #58
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    p2 and Points[1] had a null value
    The next question is: why does Points[1] have a null value?

    What is in pointsList? Print its contents to see.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #59
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, I don't know why Points[1] have a null value... :/ in my opinion it should iclude any value.
    pointsList is a list with all points which mouse intercepted.

  10. #60
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    What is in pointsList? Print its contents to see.
    You are hoping it will have something in it. What is there?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #61
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, ok, I printed a content with this code:
    		for(Point[] array: pointsList) {
    			System.out.println(array);
    		}

    and in the console it printed:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Toolkit.Lines.paintComponent(Lines.java:40)
    	at GUI.Drawing.paintComponent(Drawing.java:45)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$1100(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$400(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    [Ljava.awt.Point;@72efb4de
    [Ljava.awt.Point;@63449552
    [Ljava.awt.Point;@7acd519e
    [Ljava.awt.Point;@4f564b40
    [Ljava.awt.Point;@1cb6b662
    [Ljava.awt.Point;@14c579b0

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Try printing pointsList BEFORE accessing it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #63
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, it prints only one point
    [Ljava.awt.Point;@3ff8bf54
    It is what it prints after one drawing with mouse.

    I don't know, if I understand what you mean, but printing before accessing shows nothing, empty field.

  14. #64
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Please post the code that defines pointsList and the print statement that printed what was posted in post#63.

    The print out in post#63 is for a Point array, not the contents of an ArrayList.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #65
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, here is the code that defines pointsList:
    	private final ArrayList<Point[]> pointsList = new ArrayList<>();

    and the code with the print statement:
    		for(Point[] array: pointsList) {
    			System.out.println(array);
    		}

    I don't know if it is what you ment, but I hope so.

  16. #66
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    When you print pointsList, I'd expect to see something like this:
    [[Ljava.awt.Point;@5df3c5]

    Note [[ in front and ] at the end.

    What you have printed is the first item in pointsList, NOT the whole thing like the following:
    System.out.println("pointsList="+pointsList);

    Change your code to this:
      for(Point[] array: pointsList) {
    	System.out.println(java.util.Arrays.toString(array));
      }
    to print out the contents of the Point array.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #67
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, oh, sorry for that...

    Here is what shows in console:
    [java.awt.Point[x=400,y=219], java.awt.Point[x=404,y=219]]

    Printing before accessing shows nothing, empty field.

  18. #68
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Nothing null there. That looks like good values.
    Where is the null value coming from?

    Is that being printed in the paintComponent() method just before the statement where the exception happens?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #69
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, sorry, I cut something.
    Here is the full code:
    [java.awt.Point[x=286,y=371], null]
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Toolkit.Lines.paintComponent(Lines.java:43)
    	at GUI.Drawing.paintComponent(Drawing.java:45)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at javax.swing.RepaintManager$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$1100(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$400(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    [java.awt.Point[x=286,y=371], java.awt.Point[x=289,y=369]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=297,y=365]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=310,y=362]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=322,y=358]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=336,y=354]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=348,y=351]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=358,y=346]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=366,y=342]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=373,y=338]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=378,y=335]]
    [java.awt.Point[x=286,y=371], java.awt.Point[x=380,y=333]]

    I'm so sorry for my weird responses, its very late.

  20. #70
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    [java.awt.Point[x=286,y=371], null]
    That shows that the second point in the array is null. Why doesn't it have a valid value?
    Either make sure it has a valid value before it is used
    or test for a null value and don't use it
    If you don't understand my answer, don't ignore it, ask a question.

  21. #71
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, yes, Points[1] is null... but I don't know why. My code looks ok and I don't see any mistakes in it.

  22. #72
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Where is the value set? Why does the code use it before it is given a value?
    Add some println statements to show when the code that sets it is executed and when the paintComponent() method is called. That should show you the order that the code is executed.

    Add a test in the code to not use it if it is null.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #73
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, I added test, as you said and it works! Thank you again! Let God bless you! Thank you for all your help! If you will have any problems with everything, ask me, and I will help you also!
    Thanks again!

  24. #74
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Canvas and MouseListener...

    Glad you have it working. You might still want to see why the second point doesn't have a value when the paintComponent() method is called.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #75
    Member
    Join Date
    Mar 2014
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default Re: Canvas and MouseListener...

    Norm, yes, I'm curious about it, but I still have no idea, why it happened. :/

Page 3 of 3 FirstFirst 123

Similar Threads

  1. MouseListener
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2012, 02:18 AM
  2. [SOLVED] Image on JPanel ---> Canvas
    By justyStepi in forum AWT / Java Swing
    Replies: 19
    Last Post: May 8th, 2012, 07:20 PM
  3. Need help to save Canvas content to a file
    By piulitza in forum AWT / Java Swing
    Replies: 3
    Last Post: December 27th, 2011, 11:20 AM
  4. MouseListener
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 11:43 AM
  5. make textbox in canvas
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 2
    Last Post: October 6th, 2009, 07:10 AM