[RESOLVED] How to reuse mouseClicked() in the same code to do diffrent tasks?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [RESOLVED] How to reuse mouseClicked() in the same code to do diffrent tasks?

  1. #1
    Join Date
    Dec 2009
    Posts
    5

    [RESOLVED] How to reuse mouseClicked() in the same code to do diffrent tasks?

    Hello EveryOne

    How can i reuse mouseClicked() in the same code to do diffrent tasks?

    This is my code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    Code:
    public class ClickToMovePanel extends JPanel implements MouseListener
    {
        // This is the Diameter of the ball;
        private static final int Diam = 40;
    
        // Ball coords, Changed by mouse listeners, Used by paintComponent;
        private int Xball = 50;
        private int Yball = 50;
    
        // Position in ball of mouse press to make moving look better;
        private int DragFromX;
        private int DragFromY;
    
        // true means mouse was pressed in ball and still in panel;
        private boolean CanMove = false;
        
        // Constructor sets size, colors, and adds mouse listeners;
        public ClickToMovePanel()
        {
            setPreferredSize(new Dimension(300, 300));
            setBackground(Color.BLUE);
            setForeground(Color.MAGENTA);
    
            // Add the mouse listeners;
            this.addMouseListener(this);
        }
    
        // Ball is drawn at the last recorded mouse listener coordinates;
        public void paintComponent(Graphics G)
        {
            super.paintComponent(G);    // Required for background;
    
            G.fillOval(Xball, Yball, Diam, Diam);
        }
    
    
        public void mouseClicked(MouseEvent E)
        {
            int x = E.getX();
            int y = E.getY();
    
            // The ball wont move till the user click inside the ball;
            if (x >= Xball & x <= (Xball + Diam) & y >= Yball & y <= (Yball + Diam))
            {
                CanMove = true;
                DragFromX = x - Xball;
                DragFromY = y - Yball;
            }
            else
            {
                CanMove = false;
            }
        }
    Code:
        public void mousePressed(MouseEvent E)
        {
            // After the user click inside the ball click else where to move it;
            if (CanMove == true)
            {
                Xball = E.getX() - DragFromX;
                Yball = E.getY() - DragFromY;
    
                // The ball wont move again till it's clicked first;
                CanMove = false;
    
                repaint();
            }
        }
    
        public void mouseReleased(MouseEvent E){}
    
        public void mouseExited(MouseEvent E){}
    
        public void mouseMoved(MouseEvent E){}
    
        public void mouseEntered(MouseEvent E){}
    
    }
    
    
    import javax.swing.*;   
      
    public class ClickToMove extends JApplet   
    {   
        public static void main(String Laythe[])   
        {   
            JFrame FrameX = new JFrame();   
            FrameX.setTitle("Click To Move Ball");   
            FrameX.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
            FrameX.setContentPane(new ClickToMovePanel());   
            FrameX.pack();   
            FrameX.setVisible(true);   
        }   
      
        // Applet constructor;   
        public ClickToMove()   
        {   
            this.setContentPane(new ClickToMovePanel());   
        }   
    }
    What i need to do is:
    use the mouseClicked() Method for both actions:
    - Dont move the ball till it's clicked;
    - Move the ball after it's clicked;

    I already tried this but it's not working:
    Code:
    public void mouseClicked(MouseEvent E)   
    {   
            int x = E.getX();   
            int y = E.getY();   
      
            // The ball wont move till the user click inside the ball;   
            if (x >= Xball & x <= (Xball + Diam) & y >= Yball & y <= (Yball + Diam))   
            {   
                CanMove = true;   
                DragFromX = x - Xball;   
                DragFromY = y - Yball;   
            }   
            else  
            {   
                CanMove = false;   
            }   
      
            // After the user click inside the ball click else where to move it;   
            if (CanMove == true)   
            {   
                Xball = E.getX() - DragFromX;   
                Yball = E.getY() - DragFromY;   
      
                // The ball wont move again till it's clicked first;   
                CanMove = false;   
      
                repaint();   
            }   
    }
    Thanks in advance

  2. #2
    Join Date
    Dec 2009
    Posts
    5

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    sorry i missed up posting the code using [code], i want to fix it by editing my thread but i seems to me that there is no edit thread option!

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Please use java naming conventions ie variables should start with a lower case letter.

    When testing a boolean you do not need to test it equals true, you just need.
    Code:
     
    if ( canMove ) { ... }
    Have you commented out/removed the code in the mousePressed() method?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Quote Originally Posted by Laythe View Post
    What i need to do is:
    use the mouseClicked() Method for both actions:
    - Dont move the ball till it's clicked;
    - Move the ball after it's clicked;

    I already tried this but it's not working:
    In what way is it 'not working' ? What happens that isn't supposed to happen? what doesn't happen that should happen? Do you get any error messages?

    I notice that your mouseClicked method sets 'CanMove' (shouldn't that be 'canMove'?) 'true' if the click is inside the ball, then immediately checks if it is 'true', in which case it sets it 'false'... Isn't this a logic error? Have you tried stepping through this code by hand? Why set a boolean 'true', then if it is 'true', set it 'false'?

    Good teaching is more a giving of the right questions than a giving of the right answers...
    J. Albers
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Dec 2009
    Posts
    5

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Please excuse me to reposte the code again:
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ClickToMovePanel extends JPanel implements MouseListener
    {
        // This is the Diameter of the ball;
        private static final int Diam = 40;
    
        // Ball coords, Changed by mouse listeners, Used by paintComponent;
        private int Xball = 50;
        private int Yball = 50;
    
        // Position in ball of mouse press to make moving look better;
        private int DragFromX;
        private int DragFromY;
    
        // true means mouse was pressed in ball and still in panel;
        private boolean CanMove = false;
        
        // Constructor sets size, colors, and adds mouse listeners;
        public ClickToMovePanel()
        {
            setPreferredSize(new Dimension(300, 300));
            setBackground(Color.BLUE);
            setForeground(Color.MAGENTA);
    
            // Add the mouse listeners;
            this.addMouseListener(this);
        }
    
        // Ball is drawn at the last recorded mouse listener coordinates;
        public void paintComponent(Graphics G)
        {
            super.paintComponent(G);    // Required for background;
    
            G.fillOval(Xball, Yball, Diam, Diam);
        }
    
    
        public void mouseClicked(MouseEvent E)
        {
            int x = E.getX();
            int y = E.getY();
    
            // The ball wont move till the user click inside the ball;
            if (x >= Xball & x <= (Xball + Diam) & y >= Yball & y <= (Yball + Diam))
            {
                CanMove = true;
                DragFromX = x - Xball;
                DragFromY = y - Yball;
            }
            else
            {
                CanMove = false;
            }
        }
    
        public void mousePressed(MouseEvent E)
        {
            // After the user click inside the ball click else where to move it;
            if (CanMove == true)
            {
                Xball = E.getX() - DragFromX;
                Yball = E.getY() - DragFromY;
    
                // The ball wont move again till it's clicked first;
                CanMove = false;
    
                repaint();
            }
        }
    
        public void mouseReleased(MouseEvent E){}
    
        public void mouseExited(MouseEvent E){}
    
        public void mouseMoved(MouseEvent E){}
    
        public void mouseEntered(MouseEvent E){}
    
    }
    Code:
    import javax.swing.*;
    
    public class ClickToMove extends JApplet
    {
        public static void main(String Laythe[])
        {
            JFrame FrameX = new JFrame();
            FrameX.setTitle("Click To Move Ball");
            FrameX.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            FrameX.setContentPane(new ClickToMovePanel());
            FrameX.pack();
            FrameX.setVisible(true);
        }
    
        // Applet constructor;
        public ClickToMove()
        {
            this.setContentPane(new ClickToMovePanel());
        }
    }
    In what way is it 'not working' ? What happens that isn't supposed to happen? what doesn't happen that should happen? Do you get any error messages?
    I tried it this way but the ball dont move(you can just run the code and see for yourself, if you care eanough):
    Code:
    public void mouseClicked(MouseEvent E)
        {
            int x = E.getX();
            int y = E.getY();
    
            // The ball wont move till the user click inside the ball;
            if (x >= Xball & x <= (Xball + Diam) & y >= Yball & y <= (Yball + Diam))
            {
                CanMove = true;
                DragFromX = x - Xball;
                DragFromY = y - Yball;
            }
            else
            {
                CanMove = false;
            }
    
            // After the user click inside the ball click else where to move it;
            if (CanMove == true)
            {
                Xball = E.getX() - DragFromX;
                Yball = E.getY() - DragFromY;
    
                // The ball wont move again till it's clicked first;
                CanMove = false;
    
                repaint();
            }
        }

    I notice that your mouseClicked method sets 'CanMove' (shouldn't that be 'canMove'?) 'true' if the click is inside the ball, then immediately checks if it is 'true', in which case it sets it 'false'... Isn't this a logic error? Have you tried stepping through this code by hand? Why set a boolean 'true', then if it is 'true', set it 'false'?
    havent you noticed this:
    Code:
     int x = E.getX();
            int y = E.getY();
    
            // The ball wont move till the user click inside the ball;
            if (x >= Xball & x <= (Xball + Diam) & y >= Yball & y <= (Yball + Diam))
            {
                CanMove = true;
                DragFromX = x - Xball;
                DragFromY = y - Yball;
            }
            else
            {
                CanMove = false;
            }
    it means if you click elsewhere then the ball coordination+ball diameter, the ball wont move, do you see the what's inside the 'else' statement.

    ps: What i need to do is:
    use the mouseClicked() Method for this actions:
    - Dont move the ball till it's clicked;
    - Move the ball after it's clicked;
    - if the ball is moved dont move it again till it's clicked again;

    Please someone help.
    Last edited by Laythe; December 17th, 2009 at 02:01 AM.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Quote Originally Posted by Laythe View Post
    I tried it this way but the ball dont move(you can just run the code and see for yourself, if you care eanough):
    I might care enough if you cared enough to tell us exactly what this code was supposed to do. The version with both mousePressed and mouseClicked methods doesn't seem to work exactly as the code comments suggest - if you click inside it, you can click multiple times outside it and it will follow, whereas the comments say "The ball wont move again till it's clicked first". However, that's not really important.

    havent you noticed this:

    <code removed>

    it means if you click elsewhere then the ball coordination+ball diameter, the ball wont move, do you see the what's inside the 'else' statement.
    Yes, I noticed it - you don't need to patronise me. It is exactly why I asked you if you really meant to follow it immediately with this:
    Code:
            // After the user click inside the ball click else where to move it;
            if (CanMove == true)
            {
                Xball = E.getX() - DragFromX;
                Yball = E.getY() - DragFromY;
    
                // The ball wont move again till it's clicked first;
                CanMove = false;
    You're welcome to ignore my comments, but they are intended to help you. I don't think that method is doing what you think it is doing. I've pointed out what looks like a problem and I've suggested a way for you to confirm it.

    ps: What i need to do is:
    use the mouseClicked() Method for this actions:
    - Dont move the ball till it's clicked;
    - Move the ball after it's clicked;
    - if the ball is moved dont move it again till it's clicked again;
    I can only repeat my suggestion that when you set 'CanMove' true, don't then set it false immediately afterwards, in the same method. If you don't want to take my word for it, step through that method by hand, tracking the relevant variable values and you'll see the problem for yourself.

    Please someone help.
    If you can't recognise help when it's given, there's little point.

    Incidentally, when posting code here, please try to use the Java coding conventions (variable & method names start with a lower-case letter), so your code is easy to follow. Oh, and you don't need to compare a boolean with 'true' in an 'if' expression, just use the boolean on its own.

    Teachers open the door, but you must enter by yourself...
    Chinese proverb
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Dec 2009
    Posts
    5

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    First I apologize if i seemed disrespectful in anyway, I really respect everybody especialy the ones that are more knowledgeable than me.

    Now i understand that the problem is i didnt understood your guidlines that's all, i will read them and read them again till i get what you want to show me(hopefully).

    And again i am sorry i hope you will accept my apology.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    No problem, I'm cool. I didn't intend to sound deeply upset - maybe I spend too much time in more 'plainspeaking' (impolite) forums

    You may find that if you put the 'if (canMove) { ...' part first, and put the rest of the method inside an 'else {' block, you'' get a better result.

    Eighty percent of success is showing up...
    W. Allen
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Dec 2009
    Posts
    5

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Thank you sir dlorde for taking time to help me, finaly it worked Thanks

    This is the code for how it works:
    Code:
    public void mouseClicked(MouseEvent E)
    {
        int x = E.getX();
        int y = E.getY();
        // After the user click inside the ball click else where to move it;
        if (canMove)
        {
            xBall = E.getX() - dragFromX;
            yBall = E.getY() - dragFromY;
            
            // The ball wont move again till it's clicked first;
            canMove = false;
            
            repaint();
        }
        else
        {
            if (x >= xBall & x <= (xBall + diam) & y >= yBall & y <= (yBall + diam))
            {
                canMove = true;
                dragFromX = x - xBall;
                dragFromY = y - yBall;
            }
        }
    }

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How to reuse mouseClicked() in the same code to do diffrent tasks?

    Glad you managed to fix it

    A man's reach should exceed his grasp, or what's heaven for?
    R. Browning
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center