Take the 2-minute tour ×

I am creating a program that displays several bases and the amount of troops each base has. There are two types of bases, friendly and enemy bases. Each Base extends GCompound and consists of a GRect and a GLabel(to display the number of troops). Two arrays are used to keep track of the bases, one for friendly, one for enemy.

I want the user to be able to press the mouse down on one friendly base and release on a different friendly base, causing the troop amount to be transferred from the first base to the second one.

My problem currently is I am only able to detect the base the user presses the mouse down on, and not the base that the mouse is released on. I am using the method getElementAt from the ACM library to return the GObject that a mouse action takes place on.

Code for the mouse press:

public void mousePressed(MouseEvent e){
    int mouseX = e.getX();
    int mouseY = e.getY();
    for(int i = 0; i < PlayerBaseArray.length; i++){
        if(getClickedObject(mouseX, mouseY) == PlayerBaseArray[i]){  //Checks to see if the clicked base is in the Array of friendly bases.
            pressedIndex = i;
            pressedBaseTroopCount = PlayerBaseArray[pressedIndex].getTroopCount();

Code for the mouse release:

public void mouseReleased(MouseEvent m){
    int mouseX = m.getX();
    int mouseY = m.getY();
        if(getClickedObject(mouseX, mouseY) == PlayerBaseArray[i]){ // This is always false for some reason.

Method to see what object is clicked:

    private GObject getClickedObject(int x, int y){
    GObject clicked = getElementAt(x, y);
    if(clicked == null) return null;
    else return clicked;

For some reason the if statement in mouseReleased() is never true, even though it works properly in mousePressed(). Any idea on why the if statement in mouseReleased() does not work?

I've tried researching the problem to no avail, and instead of wasting another night on it I thought I would ask here. Thanks!

share|improve this question
can you get "getClickedObject(mouseX, mouseY)" in mouse release event ? I doubt that. Check whether its null or not . –  Gaurav Shah Jun 15 '11 at 1:53
step1 is to debug and figure out if it's false because reference equality is not true or because getElementAt is returning null –  Affe Jun 15 '11 at 1:59
The getClickedObject(mouseX, mouseY) does work in a mouse release event. I will check if it is returning null, and report back. –  LazerShow Jun 15 '11 at 2:01
The getClickedObject(mouseX, mouseY) is not null and appears to be returning the correct GObject. I guess this means that the reference equality is not true, but I still do not understand why it would work for the mousePressed event and not the mouseRelased event. –  LazerShow Jun 15 '11 at 2:08
Ah! I think I got it! I was drawing a GLine from where the first base was clicked it followed the mouse around. Turns out the getClickedObject was returning the GLine instead of the base that was clicked on. Thanks to Affe for suggesting I dig into the debugger more, I'm still learning! –  LazerShow Jun 15 '11 at 2:27
add comment

1 Answer

up vote 0 down vote accepted

You shouldn't use the == operator to compare Objects. You should use the .equals() method:

if (getClickedObject(mouseX, mouseY).equals(PlayerBaseArray[i]))

Put simply, you can only use == only when comparing java primitives (eg int, long etc).

For all "normal" objects, always use objecta.equals(objectb).

This article discusses this issue in more detail.

Attention anal retentives:

Yes, you are right, under some circumstances you can safely use == between objects such as String contants, Integers between -128 and 127, etc, etc. But this guy needed a clear answer and not to be confused. Please resist the temptation to comment about this.

share|improve this answer
But getClickedObject probably returns an object from the array so his comparison is correct. –  MK. Jun 15 '11 at 4:15
@MK Hmmmm... OK. I'll reword to tone down the answer. Cheers. –  Bohemian Jun 15 '11 at 4:19
Thanks for the info, I was not aware that ".equals()" was the preferred way of comparing objects that are not primitives. While I got the question working using "==" I will go back and use ".equals()" –  LazerShow Jun 15 '11 at 4:50
add comment

Your Answer


