Multiple gauges on one stage - JavaFX - javafx

I am new to JavaFX and trying to make a GUI with multiple gauges (using Medusa), horizontally aligned along with the window. However, when I try to compile, only one gauge is displaying.
StackPane rootPane = new StackPane();
Scene scene = new Scene(rootPane);
Pane pane = new Pane(bigGauge, smallGauge);
pane.setBackground(new Background(new BackgroundFill(Gauge.DARK_COLOR, CornerRadii.EMPTY, Insets.EMPTY)));
bigGauge.relocate(0, 0);
smallGauge.relocate(0, 230);
timer.start();
Pane pane1 = new Pane(bigGauge, smallGauge);
pane.setBackground(new Background(new BackgroundFill(Gauge.DARK_COLOR, CornerRadii.EMPTY, Insets.EMPTY)));
bigGauge.relocate(500, 0);
smallGauge.relocate(500, 230);
timer.start();
rootPane.getChildren().addAll(pane,pane1);
stage.setScene(scene);
stage.show();
Any suggestions would be helpful. Thank you.

Related

Java FX out of the window screen

I wanna it will be okay when the number variables is changed, but when the are increased the button goes out from the window. How to fix it? Also how to put the bar down to the level of "10$", so they will be in the same row?
Before :
After :
Here is my code :
VBox vboxBottom = new VBox();
HBox hboxBottomElements = new HBox(15);
HBox hboxBottomMain = new HBox(0);
Region region = new Region();
region.setPrefWidth(500);
hboxBottomElements.getChildren().addAll(visaLabel, separator2, adLabel, separator3, governRelationStatus, separator4, region, next);
hboxBottomElements.setPadding(new Insets(5));
vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));
vboxBottom.setPadding(new Insets(3));
hboxBottomMain.getChildren().addAll(new Group(moneyBox), vboxBottom);
hboxBottomMain.setPadding(new Insets(3));
layout.setBottom(hboxBottomMain);
By using a Group here
vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));
you're creating a layout structure that resizes hboxBottomElements to it's prefered size independent of the space available.
HBox simply moves elements out the right side of it's bounds, if the space available does not suffice. This means if the Group containing moneyBox grows, the Button is moved out of the HBox...
The following simpler example demonstrates the behavior:
#Override
public void start(Stage primaryStage) {
Button btn = new Button("Do something");
HBox.setHgrow(btn, Priority.NEVER);
btn.setMinWidth(Region.USE_PREF_SIZE);
Region filler = new Region();
filler.setPrefWidth(100);
HBox.setHgrow(filler, Priority.ALWAYS);
Rectangle rect = new Rectangle(200, 50);
HBox hBox = new HBox(rect, filler, btn);
Scene scene = new Scene(hBox);
primaryStage.setScene(scene);
primaryStage.show();
}
This will resize filler to make the HBox fit the window.
Now replace
Scene scene = new Scene(hBox);
with
Scene scene = new Scene(new Group(hBox));
and the Button will be moved out of the window...

JavaFX Transition: Invert button color

I want to change the color of a button to it's invert color and back as a transition.
I was able to use Timeline with ColorAdjust to do this with the brightness, but I didn't found a effect class to transition the color of a button with Timeline.
Hope someone out there is able to help me?
Thanks!
A inversion effect can be achieved by using a Blend effect with BlendMode.DIFFERENCE and a topInput that is black for no inversion and white for completely inverted colors. Example:
#Override
public void start(Stage primaryStage) {
Blend blendEffect = new Blend(BlendMode.DIFFERENCE);
ColorInput input = new ColorInput();
blendEffect.setTopInput(input);
Button btn = new Button("Inversion Animation");
input.widthProperty().bind(btn.widthProperty());
input.heightProperty().bind(btn.heightProperty());
btn.setEffect(blendEffect);
btn.setStyle("-fx-body-color: orange;");
DoubleProperty brightness = new SimpleDoubleProperty(0);
input.paintProperty().bind(Bindings.createObjectBinding(() -> Color.BLACK.interpolate(Color.WHITE, brightness.get()), brightness));
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(brightness, 0d)),
new KeyFrame(Duration.seconds(1), new KeyValue(brightness, 1d))
);
timeline.setOnFinished(evt -> timeline.setRate(-timeline.getRate()));
btn.setOnAction((ActionEvent event) -> {
timeline.play();
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 500, 500);
primaryStage.setScene(scene);
primaryStage.show();
}

Moving Button or Any thing else in javafx?

Button btn=new Button("Click Me");
Button btn2=new Button("Click");
btn2.setOnAction(e->System.exit(0));
btn.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent action){
System.out.println(5);
}
});
btn2.relocate(0, 0);
StackPane root=new StackPane();
root.getChildren().add(btn);
root.getChildren().add(btn2);
Scene sene=new Scene(root,500,265);
primaryStage.setScene(sene);
primaryStage.show();
I want to move button and using above code but I am unable to move my button?
What is the problem in code and is their any other way to do it???
The answer is :
http://docs.oracle.com/javafx/2/animations/jfxpub-animations.htm
JavaFX has ready libraries for animations.
An example (the rectangle will be faded):
final Rectangle rect1 = new Rectangle(10, 10, 100, 100);
rect1.setArcHeight(20);
rect1.setArcWidth(20);
rect1.setFill(Color.RED);
...
FadeTransition ft = new FadeTransition(Duration.millis(3000), rect1);
ft.setFromValue(1.0); ft.setToValue(0.1);
ft.setCycleCount(Timeline.INDEFINITE);
ft.setAutoReverse(true);
ft.play();
In your code you maybe need TranslateTranstition or just use translateX and translateY methods

Closing a scene in javafx

I'm learning javafx and I have a problem.
I'm building an interface with a border pane and a menu bar at the top and when I click on the items I want scenes to be loaded on the center of the border pane. That seems to be working alright. I want to add a button to close the scene but I can't make it work.
See below the code.
stage = primaryStage;
stage.setTitle("My Program");
BorderPane pane = new BorderPane();
MenuBar menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem load = new MenuItem("Load");
MenuItem save = new MenuItem("Save");
....
BorderPane sp = new BorderPane();
sp.setStyle("-fx-background: #FF0000;");
Button btn = new Button("Close");
btn.setPrefSize(200, 20);
btn.setLayoutX(200);
btn.setLayoutY(200);
sp.getChildren().add(btn);
load.setOnAction(e -> pane.setCenter(sp));
btn.setOnAction(e -> ???????????????????);
scene1 = new Scene(sp);
scene = new Scene(pane, 800, 500);
stage.setScene(scene);
stage.show();
I was wondering if it's something that I can do and what code I should put instead of the question marks.
Any help is appreciated. Thanks in advance.
btn.setOnAction(e -> pane.setCenter(null));
to clear the border pane content, or
btn.setOnAction(e -> stage.hide());
to close the entire window

JavaFX BorderPane Maximising Window Issues

My code creates a window and lays it out exactly how I want... initially. However, if I maximise the window, the top and bottom parts of the border pane do not remain in the centre. They drift off to the top left and bottom left corners.
I tried to disable the maximise window option, but again it messes up the look of the page, with the top and bottom parts moving.
Here is my code:
#Override
public void start(Stage startWindow) {
startWindow.setTitle("QuizApp");
BorderPane borderPane = new BorderPane();
borderPane.setTop(addHorizontalBoxWithMessage());
borderPane.setCenter(addImageView());
borderPane.setBottom(addHorizontalBoxWithButton());
Scene scene = new Scene(borderPane, 750, 663);
startWindow.setScene(scene);
scene.getStylesheets().add(StartWindow.class.getResource("application.css").toExternalForm());
// startWindow.resizableProperty().setValue(Boolean.FALSE);
startWindow.show();
}
public HBox addHorizontalBoxWithMessage() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(0, 10, 0, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Text message = new Text("Welcome to the QuizApp!");
message.setId("message");
hBox.getChildren().add(message);
return hBox;
}
public ImageView addImageView() {
Image image = new Image(getClass().getResourceAsStream("quiz.jpg"));
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitWidth(750);
imageView.setFitHeight(563);
return imageView;
}
public HBox addHorizontalBoxWithButton() {
HBox hBox = new HBox();
hBox.setId("hBox");
hBox.setMinWidth(750);
hBox.setMinHeight(50);
hBox.setMaxWidth(750);
hBox.setMaxHeight(50);
hBox.setPadding(new Insets(10, 10, 10, 10));
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button registerButton = new Button("Register");
registerButton.setPrefSize(100, 30);
Button loginButton = new Button("Login");
loginButton.setPrefSize(100, 30);
hBox.getChildren().add(registerButton);
hBox.getChildren().add(loginButton);
return hBox;
}
I only started teaching myself JavaFX last night but can't seem to figure out where I am going wrong, or find a solution to my problem.
Thanks in advance for any advice.
In your code replace : hBox.setMaxWidth(750);
with:
hBox.setMaxWidth(Region.USE_COMPUTED_SIZE);
The problem was that after resizing your hbox, the width was still 750 px long
Another easy option is to use JavaFX Scene Builder to figure out how GUI components works.

Resources