# Thread: Problem with click to move ball algorithm

1. ## 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);

}

// 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. 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);

}

// 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){}

}```

#### Posting Permissions

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