Click to See Complete Forum and Search --> : [RESOLVED] How to reuse mouseClicked() in the same code to do diffrent tasks?


Laythe
December 14th, 2009, 11:04 PM
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.*;


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

}


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:

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 :D

Laythe
December 14th, 2009, 11:09 PM
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!

keang
December 15th, 2009, 06:35 AM
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.

if ( canMove ) { ... }


Have you commented out/removed the code in the mousePressed() method?

dlorde
December 15th, 2009, 06:12 PM
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

Laythe
December 17th, 2009, 01:55 AM
Please excuse me to reposte the code again:

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

}



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):

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:

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.

dlorde
December 17th, 2009, 06:40 AM
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: // 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

Laythe
December 17th, 2009, 10:10 AM
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.

dlorde
December 17th, 2009, 02:52 PM
No problem, I'm cool. I didn't intend to sound deeply upset - maybe I spend too much time in more 'plainspeaking' (impolite) forums :rolleyes:

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

Laythe
December 17th, 2009, 05:29 PM
Thank you sir dlorde for taking time to help me, finaly it worked Thanks :D

This is the code for how it works:

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;
}
}
}

dlorde
December 17th, 2009, 06:19 PM
Glad you managed to fix it :thumb:

A man's reach should exceed his grasp, or what's heaven for?
R. Browning