2222import javafx .beans .property .SimpleDoubleProperty ;
2323import javafx .beans .property .SimpleObjectProperty ;
2424import javafx .beans .value .ChangeListener ;
25- import javafx .beans .value .ObservableValue ;
2625import javafx .geometry .Bounds ;
2726import javafx .scene .image .Image ;
28- import javafx .scene .layout .Region ;
27+ import javafx .scene .layout .* ;
2928import javafx .scene .paint .Color ;
3029import javafx .scene .paint .ImagePattern ;
3130import javafx .scene .paint .Paint ;
4140 *
4241 */
4342@ DefaultProperty ("image" )
43+ @ SuppressWarnings ("unused" )
4444public class GNAvatarView extends Region {
4545
46- private double prefWidth = 200D ;
47- private double prefHeight = 200D ;
46+ private final Circle circle = new Circle () ;
47+ private final Rectangle rectangle = new Rectangle () ;
4848
49- private Circle circle = new Circle ();
50- private Rectangle rectangle = new Rectangle ();
49+ private final ObjectProperty <Image > image = new SimpleObjectProperty <>(this , "image" );
50+ private final ObjectProperty <ImagePattern > imagePattern = new SimpleObjectProperty <>(this , "imagePattern" );
51+ private final ObjectProperty <AvatarType > type = new SimpleObjectProperty <>(this , "avatarType" );
52+ private final ObjectProperty <Paint > stroke = new SimpleObjectProperty <>(this , "stroke" );
53+ private final DoubleProperty strokeWidth = new SimpleDoubleProperty (this , "strokeWidth" );
5154
52- private ObjectProperty <Image > image = new SimpleObjectProperty <>(this , "image" );
53- private ObjectProperty <ImagePattern > imagePattern = new SimpleObjectProperty <>(this , "imagePattern" );
54- private ObjectProperty <AvatarType > type = new SimpleObjectProperty <>(this , "avatarType" );
55- private ObjectProperty <Paint > stroke = new SimpleObjectProperty <>(this , "stroke" );
56- private DoubleProperty strokeWidth = new SimpleDoubleProperty (this , "strokeWidth" );
55+ private String nameImage ;
56+ private String path ;
5757
58- private ChangeListener <Bounds > circleListener = (observable , oldValue , newValue ) -> {
58+ private final ChangeListener <Bounds > circleListener = (observable , oldValue , newValue ) -> {
5959 double width = newValue .getWidth () / 2 ;
6060 double height = newValue .getHeight () / 2 ;
61- double size = width < height ? width : height ;
61+ double size = Math . min ( width , height ) ;
6262
6363 circle .setRadius (size - (getStrokeWidth () / 2 ));
6464 circle .setCenterY (height );
6565 circle .setCenterX (width );
6666 };
6767
68- private ChangeListener <Bounds > rectListener = (observable , oldValue , newValue ) -> {
68+ private final ChangeListener <Bounds > rectListener = (observable , oldValue , newValue ) -> {
6969 double width = newValue .getWidth () ;
7070 double height = newValue .getHeight ();
7171
@@ -83,31 +83,47 @@ public class GNAvatarView extends Region {
8383 rectangle .setY ( (newValue .getHeight () / 2 ) - (rectangle .getHeight () / 2 ));
8484 };
8585
86+
8687 public GNAvatarView () {
8788 this (null );
8889 }
8990
90- public GNAvatarView (Image image ){
91+ public GNAvatarView (String path ){
92+ this (path , Region .USE_COMPUTED_SIZE , Region .USE_COMPUTED_SIZE );
93+ }
94+
95+ public GNAvatarView (double width , double height ){
96+ this (null , width , height );
97+ }
98+
99+ public GNAvatarView (String path , double width , double height ) {
100+
101+ this .getStyleClass ().add ("gn-avatar-view" );
91102
92103 registerListeners ();
93104 registerBinds ();
94105
95- setType (AvatarType .CIRCLE );
96- setPrefSize (prefWidth , prefHeight );
106+ setType (AvatarType .RECT );
107+ setPrefSize (width , height );
97108
98109 setStrokeWidth (2 );
99110 setStroke (Color .WHITE );
100111
101- if (image != null ) this .imagePattern .set (new ImagePattern (image ));
112+ // this.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.DASHED, CornerRadii.EMPTY, BorderWidths.DEFAULT)));
113+
114+ if (path != null ) this .imagePattern .set (new ImagePattern (new Image (path )));
102115 }
103116
104117 private void registerBinds (){
105118 circle .fillProperty ().bind (imagePattern );
106119 circle .strokeProperty ().bind (stroke );
107120 circle .strokeWidthProperty ().bind (strokeWidth );
121+
108122 rectangle .fillProperty ().bind (imagePattern );
109123 rectangle .strokeProperty ().bind (stroke );
124+ double prefHeight = 200D ;
110125 rectangle .setHeight (prefHeight );
126+ double prefWidth = 200D ;
111127 rectangle .setWidth (prefWidth );
112128 rectangle .strokeWidthProperty ().bind (strokeWidth );
113129 rectangle .setArcHeight (20D );
0 commit comments