Desktop Java

JavaFX Custom Control – Nest Thermostat Part 3

Hi, after some discussions with my colleagues, I decided today to show that the css approach is not the only one that can be used to create a custom control. Of course it allows to offer some look and feel extension point, but the same approach (working with svand transfering to JavaFX) can be used using code API.

Here is the graphic initialization method where I have replaced the css initialization code (now commented out) by call to the JavaFX objects API.
 
 
 
 

private void initGraphcis() {
	// frame = new Region();
	// frame.getStyleClass().setAll("frame");
	frame = new Circle();
	frame.setFill(FRAME_FILL);

	shadow = new DropShadow();
	shadow.setBlurType(BlurType.ONE_PASS_BOX);
	shadow.setColor(Color.rgb(0, 0, 0, 0.4));
	frame.setEffect(shadow);

	frame1 = new Circle();
	// frame1.getStyleClass().setAll("frame1");
	frame1.setFill(FRAME1_FILL);
	frame1.setStroke(FRAME1_STROKE);
	frame1.setStrokeWidth(2.0);

	frame2 = new Circle();
	// frame2.getStyleClass().setAll("frame2");
	frame2.setFill(FRAME2_FILL);
	frame2.setStroke(FRAME2_STROKE);

	frame3 = new Circle();
	// frame3.getStyleClass().setAll("frame3");
	frame3.setFill(Color.web("#c44f1a"));

	line = new SVGPath();
	// line.getStyleClass().setAll("line");
	line.setContent("M 0.75,1.806272 C 0.75,1.806272 67.422114,-2.659598 118.06708,1.085452 130.59357,2.011752 166.81696,11.039202 185.35089,11.189052 206.02921,11.356252 242.24677,2.052122 255.84883,1.085452 304.58057,-2.377808 372.89963,1.806272 372.89963,1.806272");
	line.setFill(Color.web("#ffffff00"));
	line.setStroke(Color.web("#4d4d4d"));
	line.setStrokeWidth(1.5);

	line1 = new SVGPath();
	// line.getStyleClass().setAll("line1");
	line1.setContent("M 0.75,1.806272 C 0.75,1.806272 67.422114,-2.659598 118.06708,1.085452 130.59357,2.011752 166.81696,11.039202 185.35089,11.189052 206.02921,11.356252 242.24677,2.052122 255.84883,1.085452 304.58057,-2.377808 372.89963,1.806272 372.89963,1.806272");
	line.setFill(Color.web("#ffffff00"));
	line.setStroke(Color.web("#141414"));
	line.setStrokeWidth(1.5);

	lightEffect = new Ellipse();
	lightEffect.setFill(Color.rgb(255, 255, 255, 0.7));
	lightEffect.setEffect(new BoxBlur(90, 90, 5));
	lightEffect.setCache(true);

	getChildren().setAll(frame, frame1, frame2, frame3, line, line1, lightEffect );
    }

And the management of the nodes size is as follow (tha same is to be done for the css approach anyway).

private void resize() {
	size = getWidth() < getHeight() ? getWidth() : getHeight();
	// frame.setPrefSize(size, size);
	frame.setRadius(size / 2.0);
	frame.setTranslateX(size / 2.0);
	frame.setTranslateY(size / 2.0);

	frame1.setRadius(frame1Ratio * size / 2.0);
	frame1.setTranslateX(size / 2.0);
	frame1.setTranslateY(size / 2.0);
	shadow.setOffsetX(size * shadowXOffset);
	shadow.setOffsetY(size * shadowYOffset);
	shadow.setRadius(size * shadowSizeOffset);
	shadow.setSpread(0.099);

	frame2.setRadius(frame2Ratio * size / 2.0);
	frame2.setTranslateX(size / 2.0);
	frame2.setTranslateY(size / 2.0);

	frame3.setRadius(frame3Ratio * size / 2.0);
	frame3.setTranslateX(size / 2.0);
	frame3.setTranslateY(size / 2.0);

	final double scaleRatio = size / initialSize;
	line1.setScaleX(scaleRatio);
	line1.setScaleY(scaleRatio);
	final double lineWidth = line1.getBoundsInLocal().getWidth();
	line1.setTranslateX(size / 2.0 - lineWidth / 2.0);
	line1.setTranslateY(size * 408.72054 / initialSize);

	line.setScaleX(scaleRatio);
	line.setScaleY(scaleRatio);
	line.setTranslateX(size / 2.0 - lineWidth / 2.0);
	line.setTranslateY(size * 410.08419 / initialSize);

	lightEffect.setRotate(lightEffectRotate);
	lightEffect.setTranslateX(lightEffectXRatio * size);
	lightEffect.setTranslateY(lightEffectYRatio * size);
	lightEffect.setRadiusX(lightEffectXRadiusRatio * size);
	lightEffect.setRadiusY(lightEffectYRadiusRatio * size);
    }

Next step will be to mix the two approach to offer both working methods for users. Ones will be able to use the css approach to customize the Nest representation, others will be able to use the good code API approach. It will allow me to see what are the impacts on the code (is it heavy or not to mix both approach) and if I shall make a choice between css and code API before going further to keep my code as simple as possible.
 

Reference: JavaFX Custom Control – Nest Thermostat Part 3 from our JCG partner Laurent Nicolas at the Mr LoNee blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button