0

For school we need to make the game called memory, but mine isn't really working fine. I already have all the cards and they can also turn around but I don't know how you can javafx compares the colors and turning the cards back if the colors does not mach or let the cards dissapear if the colors do mach. Can you guys please help me? This is a little part of the code:

d = Rectangle {
width: bind 150
height: bind 150
x: bind 500
y: bind 20
arcWidth: 20
arcHeight: 20
fill:  Color.GREEN
stroke: Color.BLACK
strokeWidth: 1.0
onMouseClicked:function(a: MouseEvent)
        {
           if(d.fill == Color.GREEN)
            d.fill = Color1.YELLOW
         else
            d.fill = Color.GREEN
        }
}
 if(Color1.equals("yellow"))
             && (Color2.equals("yellow"))
 {
  d.setVisible(false);
  j.setVisible(false);

 }
NLrocks
  • 1
  • 1
  • Which javafx version are you using? Old 1.X or newer 2.X? Your question might need a little bit of re-tagging. Also hope my answer will help you. – Magnilex Mar 18 '13 at 21:20

1 Answers1

1

Versions 2.X

JavaFX is obiously partly open source (Stack Overflow reference). From that link I found the source code to the equals() method in the Color class for versions 2.X:

/**
 * Indicates whether some other object is "equal to" this one.
 * @param obj the reference object with which to compare.
 * @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
 */
@Override public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj instanceof Color) {
        Color other = (Color) obj;
        return red == other.red
            && green == other.green
            && blue == other.blue
            && opacity == other.opacity;
    } else return false;
}

Obviously, red, green, blue, and opacity needs to be the same.

Versions 1.X

For versions 1.X i just looked at the compiled class file and feel confident enough to say that the implementation is the same as for 2.X (snippet below):

@com.sun.javafx.runtime.annotation.Public
  public boolean equals(java.lang.Object arg0);
  4  invokestatic javafx.lang.Builtins.isSameObject(java.lang.Object, java.lang.Object) : boolean [87]
 15  instanceof javafx.scene.paint.Color [80]
 18  ifeq 121
 22  checkcast javafx.scene.paint.Color [80]
 28  invokevirtual javafx.scene.paint.Color.get$red() : float [45]
 38  invokevirtual javafx.scene.paint.Color.get$red() : float [45]
 50  invokevirtual javafx.scene.paint.Color.get$green() : float [47]
 60  invokevirtual javafx.scene.paint.Color.get$green() : float [47]
 72  invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
 82  invokevirtual javafx.scene.paint.Color.get$blue() : float [48]
 94  invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]
104  invokevirtual javafx.scene.paint.Color.get$opacity() : float [49]

The equals() implementation has not changed from 1.X to 2.X.

Your real problem

If Color1 and Color2 indeed are of type Color, you are comparing them with objects of type String:

if(Color1.equals("yellow")) && (Color2.equals("yellow"))

The comparison will fail here:

if (obj instanceof Color)

Hence, the equals() method will always return false. You should use equals() with another object of type Color.

Community
  • 1
  • 1
Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • The sample code in the question relates to the obsolete JavaFX 1.x branch, which may not work this way (though it likely does). For JavaFX 2+ code, the answer is great. – jewelsea Mar 18 '13 at 19:58
  • 1
    @jewelsea Thanks. Just did a quick check by opening a class file, and indeed, the implementation hasn't changed. :) – Magnilex Mar 18 '13 at 21:18