Results 1 to 2 of 2
  1. #1
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default Problem with click to move ball algorithm

    Hello EveryOne :)

    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;

    This is my code:
    Java 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){}
    
    }
    Java 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());
        }
    }
    I tried this but its not working (the ball just wont move with this code) and i dont know why:
    Java 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();
            }
    }
    you may notice sometimes that the ball will follow the mouse click after it's moved and sometimes it's not, i think it's doing that cos when you Release the mouse after the ball is moved the computer consider that you made one click on the ball (one click = press + release), but if you keept the press after the ball is moved then drag the mouse outside the ball with keeping the mouse pressed the ball wont move till it's clicked again.

  2. #2
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    I found the answer anyway,
    This is how you can do it for the sake of sharing:
    Java 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();
    
            // 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;
                }
            }
        }
    
        public void mousePressed(MouseEvent E) {}
    
        public void mouseReleased(MouseEvent E){}
    
        public void mouseExited(MouseEvent E){}
    
        public void mouseMoved(MouseEvent E){}
    
        public void mouseEntered(MouseEvent E){}
    
    }

Similar Threads

  1. How to move a ball across the applet window?
    By getkiran in forum Java Applets
    Replies: 3
    Last Post: 03-05-2009, 09:45 AM
  2. How do I make My ball to move randomly?
    By whdbstjr90 in forum New To Java
    Replies: 4
    Last Post: 12-31-2007, 06:32 PM
  3. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10:08 PM
  4. Replies: 2
    Last Post: 12-01-2007, 08:48 AM
  5. Algorithm problem
    By Marcus in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 02:37 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
  •