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; } }What i need to do is: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()); } }
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:
Thanks in advanceCode: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(); } }![]()





Reply With Quote