netbeans - How does javafx compares colors? -
for school need make game called memory, mine isn't working fine. have cards , can turn around don't know how can javafx compares colors , turning cards if colors not mach or let cards dissapear if colors mach. can guys please me? little part of 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); }
versions 2.x
javafx obiously partly open source (stack overflow reference). link found source code equals()
method in color
class versions 2.x:
/** * indicates whether other object "equal to" one. * @param obj reference object compare. * @return {@code true} if object equal {@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
, , opacity
needs same.
versions 1.x
for versions 1.x looked @ compiled class file , feel confident enough implementation same 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 1.x 2.x.
your real problem
if color1
, color2
indeed of type color
, comparing them objects of type string
:
if(color1.equals("yellow")) && (color2.equals("yellow"))
the comparison fail here:
if (obj instanceof color)
hence, equals()
method return false. should use equals()
object of type color
.
Comments
Post a Comment