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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 75

Thread: Canvas and MouseListener...

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    when I stop drawing, and start in another
    Can the program detect when one drawing stops and another starts?
    If so, the current ArrayList is finished being updated and another ArrayList for the new drawing needs to be created.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm I did, what you said. I created ArrayList of ArrayLists (of Points).
    Here is my code:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
    import javax.swing.JPanel;
     
    public class Drawing extends JPanel implements MouseListener, MouseMotionListener {
     
    	private int x, y;
    	private ArrayList<ArrayList<Point>> Lists = new ArrayList<ArrayList<Point>>();
     
    	public Drawing() {
    		setBackground(Color.WHITE);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    	}
     
    	 public void paintComponent(Graphics g) {
             super.paintComponent(g);
    	     Color colour = GUI.getColour();
    		 g.setColor(colour);
    		 ((Graphics2D) g).setStroke(new BasicStroke(2));
    			for(int i=0; i<Lists.size()-1; i++) {
    				ArrayList<Point> p_list = Lists.get(i);
    					for(int j=0; j<=p_list.size()-2; j++) {
    						Point p1, p2 = null;
    						p1 = p_list.get(j);
    						p2 = p_list.get(j+1);
    						g.drawLine(p1.x, p1.y, p2.x, p2.y);
    					}
    			}
    	}
     
    	public void mouseDragged(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
            repaint();
    	}
     
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
    		repaint();
    	}
    	public void mouseMoved(MouseEvent e) {}
    	public void mouseClicked(MouseEvent e) {
    		ArrayList<Point> pointsList = new ArrayList<Point>();
    		Lists.add(pointsList);
    		x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
            repaint();
    	}
    	public void mouseEntered(MouseEvent e) {}
    }

    Why does not it work properly?
    There are no warnings, but while compiling, there are lots of errors:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    	at java.util.ArrayList.elementData(Unknown Source)
    	at java.util.ArrayList.get(Unknown Source)
    	at Menu.DrawingArea.mouseReleased(DrawingArea.java:54)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.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)

    Thanks for understanding and your help!

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    while compiling, there are lots of errors:
    The posted error is not a compiler error. That error happened when the code was executed.
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at Menu.DrawingArea.mouseReleased(DrawingArea.java:54 )
    The code at line 54 uses an index (-1) that is out of bounds. Look at the code on line 54 and see why it used that invalid index.

    One problem with the code is that there are no comments in it describing what it is going to do and how it is going to do it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm, I don't see it. Hmm, maybe when I add the first item to the list, then it can't "appeal" to him? I have no idea how to solve it.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    I don't see it.
    Try debugging the code by adding lots of println() statements that print out messages when all the methods are executed. Include the size of the arraylists in all the messages so you can see what the computer ssees when the code is executed. The print outs will show you what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm, it stops before
    g.drawLine(p1.x, p1.y, p2.x, p2.y);
    I cant handle with it... what is the solution?

  7. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    What was printed out by the println() statements I suggested that you add to all the methods so you can see what the computer sees when it executes?

    it stops
    You need to find out why the exception happens so it can be fixed. Why is the index -1?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (May 31st, 2014)

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

    Default Re: Canvas and MouseListener...

    Norm, I have changed the code, and now there are no errors, but it still connects last Point with another from new List, so while drawing after Mouse Release and again Mouse Click last Point connects with new one from new Mouse Click.
    Here is the code:
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
     
    import javax.swing.JPanel;
     
    import GUI.GUI;
     
    import java.awt.Graphics2D;
    import java.util.ArrayList;
     
    public class Drawing extends JPanel implements MouseListener, MouseMotionListener {
    public Drawing() {
    		setBackground(Color.WHITE);
    		addMouseListener(this);
    		addMouseMotionListener(this);
    	}
     
    	private int x, y;
    	private ArrayList<ArrayList<Point>> Lists = new ArrayList<ArrayList<Point>>();
     
    	 public void paintComponent(Graphics g) {
             super.paintComponent(g);
    	     Color colour = GUI.getColour();
    		 g.setColor(colour);
    		 ((Graphics2D) g).setStroke(new BasicStroke(2));
    			for(int i=0; i<Lists.size(); i++) {
    				ArrayList<Point> p_list = Lists.get(i);
    					for(int j=0; j<p_list.size()-1; j++) {
    						Point p1, p2 = null;
    						p1 = p_list.get(j);
    						p2 = p_list.get(j+1);
    						g.drawLine(p1.x, p1.y, p2.x, p2.y);
    					}
    			}
    	}
     
    	public void mouseDragged(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
            repaint();
    	}
    	public void mousePressed(MouseEvent e) {
            ArrayList<Point> pointsList = new ArrayList<Point>();
    	Lists.add(pointsList);
    	x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
            repaint();
    	}
    	public void mouseReleased(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            Lists.get(Lists.size()-1).add(new Point(x, y));
    		repaint();
    	}
    	public void mouseMoved(MouseEvent e) {}
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    }

    Could you take a look at this? Thank you for you engagement!

    EDIT:// Solved it! Pasted correct code. Thank you so much for your answers! I appreciate it
    Last edited by fkmk; May 31st, 2014 at 05:21 PM.

  10. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    Where are the comments in the code that describe the logic that the program is using to do its job? As requested in post#28
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (May 31st, 2014)

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

    Default Re: Canvas and MouseListener...

    Norm, I have deleted it, because I solved the problem. Thank you so much for all your help! I owe you a lot!

  13. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    Glad you solved the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (May 31st, 2014)

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

    Default Re: Canvas and MouseListener...

    Hi guys! I have another problem...

    I have two classes:
    import java.awt.Color;
    import javax.swing.JPanel;
    import Toolkit.Pencil;
     
    public class Drawing extends JPanel {
     
    	private Pencil pencil;
     
    	public Drawing() {
    		setBackground(Color.WHITE);
    		pencil = new Pencil();
    		addMouseListener(pencil);
    		addMouseMotionListener(pencil);
    	}
    }

    and

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
     
    import GUI.Drawing;
    import GUI.GUI;
     
    public class Pencil implements MouseListener, MouseMotionListener {
     
    	private int x, y;
    	private ArrayList<ArrayList<Point>> pointsList = new ArrayList<ArrayList<Point>>();
     
    	public void paintComponent(Graphics g) {
             super.paintComponent(g);
    		 ((Graphics2D) g).setStroke(new BasicStroke(2));
    			for(int i=0; i<pointsList.size(); i++) {
    				ArrayList<Point> p_list = pointsList.get(i);
    					for(int j=0; j<p_list.size()-1; j++) {
    					    Color colour = GUI.getColour();
    						g.setColor(colour);
    						Point p1, p2 = null;
    						p1 = p_list.get(j);
    						p2 = p_list.get(j+1);
    						g.drawLine(p1.x, p1.y, p2.x, p2.y);
    					}
    			}
    	}
     
    	public void mousePressed(MouseEvent e) {
    		ArrayList<Point> Points = new ArrayList<Point>();
    		pointsList.add(Points);
    		x = e.getX();
            y = e.getY();
            pointsList.get(pointsList.size()-1).add(new Point(x, y));
            repaint();
    	}
     
    	public void mouseDragged(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            pointsList.get(pointsList.size()-1).add(new Point(x, y));
            repaint();
    	}
     
    	public void mouseReleased(MouseEvent e) {
    		x = e.getX();
            y = e.getY();
            pointsList.get(pointsList.size()-1).add(new Point(x, y));
            repaint();
    	}
     
    	public void mouseMoved(MouseEvent e) {}
    	public void mouseClicked(MouseEvent e) {}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    }

    Class Drawing is something like "Drawing Area" and Pencil is specific tool to draw in this Area. By default there is Pencil.
    Compiler does not see repaint() method in the Pencil class.
    I have been trying lots of ways to solve it, included extending Drawing() to Pencil, but then there is a problem with StackOverflowError.
    Any ideas?

  16. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    If you have errors, copy the error messages and paste it here. If its huge from a stack overflow only copy and paste enough to see where the recursive calls are.

    Compiler does not see repaint()
    What does that mean?
    Hint: Always put a @Override annotation statement before any method that a class overrides to allow the compiler to find errors.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    For above code compiler shows:

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    	The method repaint() is undefined for the type Pencil
     
    	at Toolkit.Pencil.mousePressed(Pencil.java:45)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.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)

    For code with Pencil extending Drawing() compiler shows:
    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    	at java.awt.Component.setForeground(Unknown Source)
    	at javax.swing.JComponent.setForeground(Unknown Source)
    	at javax.swing.LookAndFeel.installColors(Unknown Source)
    	at javax.swing.LookAndFeel.installColorsAndFont(Unknown Source)
    	at javax.swing.plaf.basic.BasicPanelUI.installDefaults(Unknown Source)
    	at javax.swing.plaf.basic.BasicPanelUI.installUI(Unknown Source)
    	at javax.swing.JComponent.setUI(Unknown Source)
    	at javax.swing.JPanel.setUI(Unknown Source)
    	at javax.swing.JPanel.updateUI(Unknown Source)
    	at javax.swing.JPanel.<init>(Unknown Source)
    	at javax.swing.JPanel.<init>(Unknown Source)
    	at javax.swing.JPanel.<init>(Unknown Source)
    	at GUI.Drawing.<init>(Drawing.java:21)
    	at Toolkit.Pencil.<init>(Pencil.java:18)

  18. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    The method repaint() is undefined for the type Pencil
    That error seems to say what the problem is: Pencil does NOT have a repaint() method.


    For code with Pencil extending Drawing() compiler shows:
    That looks like an execution error, not a compiler error. The first error you posted was a compiler error. IDEs are trying to muddle and lose the concepts of compiling and execution. I have no idea why they are doing that. I find the concepts useful.

    If that is all the error message, I don't see why there is a stack overflow.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm, yes, Pencil does not have repaint() method, but I need Pencil have it, without extending it with JPanel.

  20. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    What would it do?

    Did you add the @Override statement before the methods that Pencil is trying to override?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm, I have added @Override adnotation before each MouseListener, and MouseMotionListener method already, but it still does not work, same problem occures. repaint() is undefined for the type Pencil. Maybe I did it wrong? :/ I don't know.
    Last edited by fkmk; June 1st, 2014 at 08:46 AM.

  22. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    Where is the repaint() method defined? If it is not defined you can't call it. That's very basic java.

    added @Override adnotation before each MouseListener, and MouseMotionListener method
    What about the other methods? For example: paintComponent()

    It looks like you need to do some reading about how java works. These are some basic concepts that you need to understand.
    There are lots of good topics here: The Really Big Index
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm, thank you! But I just wonder if there is a way to connect these to classes (Drawing and Pencil).

  24. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    Define what each of those classes is supposed to do.

    If Drawing has the GUI stuff, and Pencil has the logic. Have method(s) in the Drawing class call method(s) in the Pencil class and pass a reference to the Graphics object that is passed to the paintComponent() method that is overridden in the Drawing class.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    fkmk (June 2nd, 2014)

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

    Default Re: Canvas and MouseListener...

    Norm thank you, I have solved it!
    But another problem has occured...

    The problem is, that after pressing buttons, there is no action in JPanel. Take a look at this:

    Here is my code for calling buttons (Pencil, Paintbrush etc.):
        if(action.getSource() == newDrawingArea) { drawingArea = new Drawing(); }
        if(action.getSource() == pencilButton) { Drawing.pencil = new Pencil(drawingArea); pencil_condition = true; 
        lines_condition = false; paintbrush_condition = false; Drawing.paintbrush = null; Drawing.lines = null; }
        if(action.getSource() == linesButton) { Drawing.lines = new Lines(drawingArea); lines_condition = true; 
        pencil_condition = false;  paintbrush_condition = false; Drawing.paintbrush = null; Drawing.pencil = null; }
        if(action.getSource() == paintbrushButton) { Drawing.paintbrush = new Paintbrush(drawingArea); paintbrush_condition = true;
        lines_condition = false; pencil_condition = false; Drawing.lines = null; Drawing.pencil = null; }

    The class, where Objects should create is:

    public class Drawing extends JPanel {
     
        public static Pencil pencil;
        public static Lines lines;
        public static Paintbrush paintbrush;
     
        public Drawing() {
            setBackground(Color.WHITE);
            pencil = new Pencil(this);
            GUI.pencil_condition = true;
     
            addMouseListener(pencil);
            addMouseMotionListener(pencil);
            addMouseListener(lines);
            addMouseMotionListener(lines);
            addMouseListener(paintbrush);
            addMouseMotionListener(paintbrush);
            this.repaint();
        }
     
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(GUI.pencil_condition == true) {
                GUI.lines_condition = false; GUI.paintbrush_condition = false; paintbrush = null; lines = null;
            pencil.paintComponent(g);
            }
            if(GUI.lines_condition == true) {
                System.out.println("include lines!");
                GUI.pencil_condition = false;  GUI.paintbrush_condition = false; paintbrush = null; pencil = null;
                lines.paintComponent(g);
            }
            if(GUI.paintbrush_condition == true)
            paintbrush.paintComponent(g);
        }
    }
    As you can see, I have added Boolean type conditions, but it does not work properly.

    How can I fix it?

    Thanks for your help!
    Last edited by fkmk; June 2nd, 2014 at 05:52 PM.

  27. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    Don't try to save space by putting more than one statement on a line. It makes the code hard to read.

    it does not work properly.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Canvas and MouseListener...

    Norm Ok, sorry.

    I changed the code a bit:

    Here is a part code of calling specific tools in one class:
    	static int warunek;
     
    		if(action.getSource() == newDrawingArea) { 
                        drawingArea = new Drawing(); 
                    }
    		if(action.getSource() == pencilButton) { 
                        setWarunek(1);
                    }
    		if(action.getSource() == linesButton) { 
                        setWarunek(2); 
                    }
    		if(action.getSource() == paintbrushButton) { 
                        setWarunek(3); 
                    }
     
    	public void setWarunek(int warunek) {
    		GUI.warunek = warunek;
    	}

    and another class od Drawing panel:

    public class Drawing extends JPanel {
     
    	private Tool pencil;
    	private Tool lines;
    	private Tool paintbrush;
     
    	public Drawing() {
    			pencil = new Pencil(this);
    			addMouseListener(pencil);
    			addMouseMotionListener(pencil);
    			lines = new Lines(this);
    			addMouseListener(lines);
    			addMouseMotionListener(lines);
    			paintbrush = new Paintbrush(this);
    			addMouseListener(paintbrush);
    			addMouseMotionListener(paintbrush);
    		setBackground(Color.WHITE);
    		this.repaint();
    	}
     
    	@Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		if(GUI.warunek == 1)
    			pencil.paintComponent(g);
     
    		if(GUI.warunek == 2)
    			lines.paintComponent(g);
     
    		if(GUI.warunek == 3)
    			paintbrush.paintComponent(g);	
    	}
    }

    After launching program, pencil is selected and drawing is possible, but only after selecting other button, e.g lines, previous "drawing" disappear.

    EDIT:// Ok, I know, why it crashes, but it still does not work, as I want. After pressing button previous "picture" does not save.

  29. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,359
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Canvas and MouseListener...

    After pressing button previous "picture" does not save.
    Sorry, I don't understand.
    What is previous picture?
    What does save mean? What is being saved? Where is it being saved?
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. MouseListener
    By Karthik Prabhu in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2012, 03:18 AM
  2. [SOLVED] Image on JPanel ---> Canvas
    By justyStepi in forum AWT / Java Swing
    Replies: 19
    Last Post: May 8th, 2012, 08: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, 08:10 AM