JavaFX Transition - Darken button on hover -


i'm beginner in javafx. i'm trying create own button subclass have on animations mouse enter , mouse exit. animation i'm trying achieve simple "darken" or "dim" transition darken color of button background when user hovers on button , , animate normal state when mouse exits button.

first thought can achieve filltransition, need specific darker color of button, depends on button color. i'm trying fade in , fade out low-opacity black rectangle on top of button, rectangle doesn't seem appear @ all.

here's code of button:

public class flatbutton extends button {      private rectangle dimrectangle;     private duration dimduration = duration.millis(250);     private color dimcolor = new color(0,0,0,0.11);      public flatbutton(string text) {         super(text);          getstyleclass().addall("flat-button-style");         createeffect();     }      private void createeffect()      {         dimrectangle = new rectangle(this.getwidth(), this.getheight(), dimcolor);         dimrectangle.setopacity(1.0);         dimrectangle.setx(this.get);           fadetransition entertransition = new fadetransition(dimduration, this);         entertransition.setinterpolator(interpolator.ease_out);         entertransition.setfromvalue(0.0);         entertransition.settovalue(1.0);          fadetransition exittransition = new fadetransition(dimduration, this);         exittransition.setinterpolator(interpolator.ease_out);         exittransition.setfromvalue(1.0);         exittransition.settovalue(0.0);           this.setonmouseentered(new eventhandler<mouseevent>(){              public void handle(mouseevent mouseevent){                 entertransition.play();             }         });          this.setonmouseexited(new eventhandler<mouseevent>(){              public void handle(mouseevent mouseevent){                 exittransition.play();             }         });     }  } 

edit: part in code "new fadetransition(dimduration, this);" should "new fadetransition(dimduration, dimrectangle);". it's testing.

edit2: figured "dimrectangle = new rectangle(this.getwidth(), this.getheight(), dimcolor);" not working , havent found way yet how make rectangle fill button dimensions.

you use coloradjust effect , change it's brightness property using timeline.

public class buttonfadedemo extends application {      @override     public void start(stage primarystage) {          try {              pane root = new pane();              button button = new button("click me!");              coloradjust coloradjust = new coloradjust();             coloradjust.setbrightness(0.0);              button.seteffect(coloradjust);              button.setonmouseentered(e -> {                  timeline fadeintimeline = new timeline(                         new keyframe(duration.seconds(0),                                  new keyvalue(coloradjust.brightnessproperty(), coloradjust.brightnessproperty().getvalue(), interpolator.linear)),                                  new keyframe(duration.seconds(1), new keyvalue(coloradjust.brightnessproperty(), -1, interpolator.linear)                                 ));                 fadeintimeline.setcyclecount(1);                 fadeintimeline.setautoreverse(false);                 fadeintimeline.play();              });              button.setonmouseexited(e -> {                  timeline fadeouttimeline = new timeline(                         new keyframe(duration.seconds(0),                                  new keyvalue(coloradjust.brightnessproperty(), coloradjust.brightnessproperty().getvalue(), interpolator.linear)),                                  new keyframe(duration.seconds(1), new keyvalue(coloradjust.brightnessproperty(), 0, interpolator.linear)                                 ));                 fadeouttimeline.setcyclecount(1);                 fadeouttimeline.setautoreverse(false);                 fadeouttimeline.play();              });              root.getchildren().addall(button);              scene scene = new scene(root, 800, 400);             primarystage.setscene(scene);             primarystage.show();          } catch (exception e) {             e.printstacktrace();         }     }       public static void main(string[] args) {         launch(args);     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -