Change colors using one button javafx - button

How should I use my Buttons(red and blue).
Got four buttons on my pane. Two of them to draw the shapes circle and square on to the pane, and two to color them with either red or blue.
I want the color Buttons to only paint the last shape Button(Circle1 or Square) I clicked on.
Any suggestions?
public void start(Stage primaryStage) {
HBox hBox = new HBox();
Button circle = new Button("Circle");
Button square = new Button("Square");
Button red = new Button("Red");
Button blue = new Button("Blue");
hBox.getChildren().addAll(circle, square, red, blue);
Rectangle Square = new Rectangle();
square.setOnAction(e -> {
Square.setX(200);
Square.setY(200);
Square.setHeight(75);
Square.setWidth(75);
});
Circle Circle1 = new Circle();
red.setOnAction(e -> {
Square.setFill(Color.RED);
Circle1.setFill(Color.RED);
});
blue.setOnAction(e -> {
Square.setFill(Color.BLUE);
Circle1.setFill(Color.BLUE);
});
circle.setOnAction(e -> {
Circle1.setCenterX(100);
Circle1.setCenterY(100);
Circle1.setRadius(45);
});
BorderPane borderPane = new BorderPane();
borderPane.getChildren().addAll(Square, Circle1, hBox);
Scene scene = new Scene(borderPane, 600, 500);
primaryStage.setTitle("ShowBorderPane");
primaryStage.setScene(scene);
primaryStage.show();
}
}

Related

Click outside of pane yet inside of Scene in Javafx

This is for homework but I like to do extra than what's assigned. The goal is if we click in the pane it will show the coordinates. Got it but I don't like how I have to click inside the pane is there a way to make it where if I click in the scene it will display in the pane? (here is what I got).
#Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Text text = new Text(50, 50, "Hello");
pane.getChildren().addAll(text);
text.setOnMouseClicked(e -> {
text.setText(e.getX() + ", " + e.getY() );
});
Scene scene = new Scene(pane, 300, 200);
primaryStage.setTitle("MouseClicker");
primaryStage.setScene(scene);
primaryStage.show();

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...

How to set an image in a circle

How could I set an image in a circle. Is there a better way to set an image with a circled frame? (particularly the image frame on windows 10 login screen)
Circle cir2 = new Circle(250,200,80);
cir2.setStroke(Color.SEAGREEN);
cir2.setFill(Color.SNOW);
cir2.setEffect(new DropShadow(+25d, 0d, +2d, Color.DARKSEAGREEN));
ImagePattern is what you are looking for.
It fills a Shape with an Image, so your code might look like this
cir2.setFill(new ImagePattern(Image));
test code
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
root.setPadding(new Insets(10));
Scene scene = new Scene(root,400,400);
Label l = new Label("SHAPE IMAGE OF MY SISTER");
l.setFont(Font.font(Font.getFontNames().get(23), FontWeight.EXTRA_BOLD, 14));
l.setAlignment(Pos.CENTER);
l.setPrefWidth(Double.MAX_VALUE);
root.setTop(l);
///////////////important code starts from here
Circle cir2 = new Circle(250,250,120);
cir2.setStroke(Color.SEAGREEN);
Image im = new Image("https://juicylinksmag.files.wordpress.com/2016/02/juliet-ibrahim.jpg",false);
cir2.setFill(new ImagePattern(im));
cir2.setEffect(new DropShadow(+25d, 0d, +2d, Color.DARKSEAGREEN));
//////////////important code ends here
root.setCenter(cir2);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

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();
}

Javafx - How to set drag range for components added in a pane

i have tried a below sample, in which left area of border Pane will have list of components and center of the border pane will act as a canvas area and here i have added a rectangle on run time as children to a Pane which is set to Center portion of BorderPane. But when drag the rectangle it moving outof the area allocated for the center, so how could i make this drag around only inside the Center Pane.
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("BPM");
BorderPane border = new BorderPane();
Pane canvas = new Pane();
canvas.setStyle("-fx-background-color: #F0F0F0;");
border.setLeft(compList());
border.setCenter(canvas);
//
Anchor start = new Anchor(null, "Start", Color.PALEGREEN, new SimpleDoubleProperty(170), new SimpleDoubleProperty(170));
final Rect rect=new Rect(100, 70,new SimpleDoubleProperty(10), new SimpleDoubleProperty(100));
rect.setX(100);
rect.setY(100);
canvas.getChildren().add(rect);
canvas.getChildren().add(start);
Scene scene = new Scene(border, 800, 600);
stage.setScene(scene);
stage.show();
}
Actually, by default, the Pane class does not ensure that all its children are clipping hence there are possibility that the children might go out of the boundary of the Pane. To ensure that all children (in your case, the rectangle) are dragged within specify boundary, you have to manually check the boundary as you dragging the children. Below are example of my implementation:
#Override
public void start(Stage stage){
stage.setTitle("BPM");
BorderPane mainPanel = new BorderPane();
VBox nameList = new VBox();
nameList.getChildren().add(new Label("Data"));
nameList.setPrefWidth(150);
Pane canvas = new Pane();
canvas.setStyle("-fx-background-color: #ffe3c3;");
canvas.setPrefSize(400,300);
Circle anchor = new Circle(10);
double rectWidth = 50, rectHeight = 50;
Rectangle rect = new Rectangle(50,50);
rect.setX(100);
rect.setY(100);
canvas.getChildren().addAll(rect, anchor);
// set the clip boundary
Rectangle bound = new Rectangle(400,300);
canvas.setClip(bound);
rect.setOnMouseDragged(event -> {
Point2D currentPointer = new Point2D(event.getX(), event.getY());
if(bound.getBoundsInLocal().contains(currentPointer)){
if(currentPointer.getX() > 0 &&
(currentPointer.getX() + rectWidth) < bound.getWidth()){
rect.setX(currentPointer.getX());
}
if(currentPointer.getY() > 0 &&
(currentPointer.getY() + rectHeight) < bound.getHeight()){
rect.setY(currentPointer.getY());
}
}
});
mainPanel.setLeft(nameList);
mainPanel.setCenter(canvas);
Scene scene = new Scene(mainPanel, 800, 600);
stage.setScene(scene);
stage.show();
}

Resources