Results 1 to 2 of 2
- 12-17-2009, 08:02 PM #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); // 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){} }I tried this but its not working (the ball just wont move with this code) and i dont know why: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()); } }
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.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(); } }
- 12-19-2009, 12:00 AM #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); // 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
-
How to move a ball across the applet window?
By getkiran in forum Java AppletsReplies: 3Last Post: 03-05-2009, 08:45 AM -
How do I make My ball to move randomly?
By whdbstjr90 in forum New To JavaReplies: 4Last Post: 12-31-2007, 05:32 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM -
Need help making ball move and bounce off of sides of JPanel
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-01-2007, 07:48 AM -
Algorithm problem
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks