How to clip and resize javafx borderpane - javafx

I am just starting with JavaFX. I want to have a BorderPane with controls on top, left, and right, and an image in the center. I want the center pane to resize as you resize the window, but to always be able to see all left, right, and top controls.
With the code below, I can show a button in the left, top, and right. And I can display an image in the center.
But the image expands beyond center bounds and hides the right button.
Oddly, if I set a clipping rectangle on the imageview in the center pane (uncomment lines 67 & 68), it does in fact only draw the clipped region, but the rest of the layout behaves as if it were drawing the whole picture. That is, the UNDRAWN part of the image still obscures the button on the right.
Any help would be much appreciated.
Thanks in advance and apologies if it's simple.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ImageApp extends Application {
private BorderPane root;
private Rectangle clipRect;
private ImageView iv;
private StackPane leftPane;
private StackPane rightPane;
private Button topButton;
private Button leftButton;
private Button rightButton;
#Override
public void start(Stage primaryStage) {
root = new BorderPane();
Scene primaryScene = new Scene(root, 900, 800);
initializePrimaryStage(primaryStage, primaryScene);
initializeFrameContent(root, topButton, leftButton);
initializeContent(root);
primaryStage.show();
}
private void initializeFrameContent(BorderPane root, Button topButton, Button leftButton) {
topButton = new Button("TOP");
leftButton = new Button("LEFT");
rightButton = new Button("RIGHT");
leftPane = new StackPane(leftButton);
leftPane.setAlignment(Pos.TOP_LEFT);
rightPane = new StackPane(rightButton);
rightPane.setAlignment(Pos.TOP_RIGHT);
root.setLeft(leftPane);
root.setTop(topButton);
root.setRight(rightButton);
}
private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
primaryStage.setTitle("Image Clip Test");
primaryStage.setScene(primaryScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.minWidthProperty().setValue(400);
primaryStage.minHeightProperty().setValue(300);
}
public static void main(String[] args) {
launch(args);
}
private void initializeContent(BorderPane root) {
Image image = new Image(
"http://www.ciee.org/study-abroad/images/cities/0020/headers/desktop/big-ben-london-traffic-trafalgar-abroad-studies.jpg"
);
iv = new ImageView(image);
root.setCenter(iv);
//clipRect = new Rectangle(400,200);
//root.getCenter().setClip(clipRect);
}
}

You don't specify what you intend to do. Why would you want to clip the content? The way you describe it all you want is some background that's getting clipped. You can do that with various mechanisms, e. g. css.
Or you could use a proper parent, e. g. a ScrollPane in order to limit the region or e. g. an ImageViewPane in order to stretch to fit:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ImageApp extends Application {
private BorderPane root;
private Rectangle clipRect;
private ImageView iv;
private StackPane leftPane;
private StackPane rightPane;
private Button topButton;
private Button leftButton;
private Button rightButton;
#Override
public void start(Stage primaryStage) {
root = new BorderPane();
Scene primaryScene = new Scene(root, 900, 800);
initializePrimaryStage(primaryStage, primaryScene);
initializeFrameContent(root, topButton, leftButton);
initializeContent(root);
primaryStage.show();
}
private void initializeFrameContent(BorderPane root, Button topButton, Button leftButton) {
topButton = new Button("TOP");
leftButton = new Button("LEFT");
rightButton = new Button("RIGHT");
leftPane = new StackPane(leftButton);
leftPane.setAlignment(Pos.TOP_LEFT);
rightPane = new StackPane(rightButton);
rightPane.setAlignment(Pos.TOP_RIGHT);
root.setLeft(leftPane);
root.setTop(topButton);
root.setRight(rightButton);
}
private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
primaryStage.setTitle("Image Clip Test");
primaryStage.setScene(primaryScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.minWidthProperty().setValue(400);
primaryStage.minHeightProperty().setValue(300);
}
public static void main(String[] args) {
launch(args);
}
private void initializeContent(BorderPane root) {
Image image = new Image(
"http://www.ciee.org/study-abroad/images/cities/0020/headers/desktop/big-ben-london-traffic-trafalgar-abroad-studies.jpg"
);
iv = new ImageView(image);
// ImageViewPane content = new ImageViewPane( iv);
ScrollPane content = new ScrollPane( imageView);
// hide scrollbars
content.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
content.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
content.setPadding(Insets.EMPTY);
root.setCenter(content);
}
// code from here: https://stackoverflow.com/questions/22993550/how-to-resize-an-image-when-resizing-the-window-in-javafx
class ImageViewPane extends Region {
private ObjectProperty<ImageView> imageViewProperty = new SimpleObjectProperty<ImageView>();
public ObjectProperty<ImageView> imageViewProperty() {
return imageViewProperty;
}
public ImageView getImageView() {
return imageViewProperty.get();
}
public void setImageView(ImageView imageView) {
this.imageViewProperty.set(imageView);
}
public ImageViewPane() {
this(new ImageView());
}
#Override
protected void layoutChildren() {
ImageView imageView = imageViewProperty.get();
if (imageView != null) {
imageView.setFitWidth(getWidth());
imageView.setFitHeight(getHeight());
layoutInArea(imageView, 0, 0, getWidth(), getHeight(), 0, HPos.CENTER, VPos.CENTER);
}
super.layoutChildren();
}
public ImageViewPane(ImageView imageView) {
imageViewProperty.addListener(new ChangeListener<ImageView>() {
#Override
public void changed(ObservableValue<? extends ImageView> arg0, ImageView oldIV, ImageView newIV) {
if (oldIV != null) {
getChildren().remove(oldIV);
}
if (newIV != null) {
getChildren().add(newIV);
}
}
});
this.imageViewProperty.set(imageView);
}
}
}

Related

JavaFX: transparency of popup window only changes brightness, still displays as solid color and hides underlying stage

I have the main window (mainWindow.fxml) on top of which I want to display a transparent popup window (errorDialog.fxml) with 50% opacity so that the main window's content can still be seen underneath.
However, my attempts at making the background colour of overlay inside errorDialog.fxml transparent only results in the background colour being displayed as a solid 50% grey that hides the main window completely.
I tried to set the transparency both in the style attribute of "overlay" as well as in in the initialize method of controllerErrorDialog.java.
Any help is appreciated!
controllerMainWindow.java
package myPackage;
import [...];
public class controllerMainWindow extends AbstractController
{
#FXML
private Button btnOpenPopup;
#FXML
private BorderPane paneMainWindow;
//---------------------------------------------------------------------------------------------------
public void initialize()
{
}
//---------------------------------------------------------------------------------------------------
#FXML
public void handleButtonAction(ActionEvent event)
{
try {
if (event.getSource().equals(btnOpenPopup)) {
FXMLLoader errorLoader = new FXMLLoader();
errorLoader.setLocation(getClass().getResource("errorDialog.fxml"));
controllerErrorDialog errorController = new controllerErrorDialog();
errorLoader.setController(errorController);
Parent layout;
layout = errorLoader.load();
Scene errorScene = new Scene(layout);
Stage errorStage = new Stage();
errorStage.initStyle(StageStyle.UNDECORATED);
errorStage.setMaximized(true);
errorController.setStage(errorStage);
if(this.main!=null) {
errorStage.initOwner(main.getPrimaryStage());
}
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.setScene(errorScene);
errorStage.showAndWait();
}
}catch (IOException exceptionCockpitSettings) {
System.out.println("Error when switching to cockpitSettings.");
exceptionCockpitSettings.printStackTrace();
return;
}
}
//---------------------------------------------------------------------------------------------------
}
controllerErrorDialog.java
package myPackage;
import [...];
public class controllerErrorDialog extends AbstractController implements Initializable
{
#FXML
private BorderPane overlay;
private Stage stage = null;
//-------------------------------------------------------------------------------------------------------
#Override
public void initialize(URL url, ResourceBundle rb)
{
overlay.setStyle("fx-background-color: transparent");
}
//---------------------------------------------------------------------------------------------------
public void setStage(Stage stage) {
this.stage = stage;
}
//---------------------------------------------------------------------------------------------------
}
errorDialog.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import [...]?>
<BorderPane fx:id="overlay" prefWidth="1920" prefHeight="1080" style="-fx-background-color: rgba(0,0,0,0.5)" xmlns:fx="http://javafx.com/fxml">
<top></top>
<left></left>
<center></center>
<right></right>
<bottom></bottom>
</BorderPane>
You need to make sure:
The scene has a transparent background, with errorScene.setFill(Color.TRANSPARENT);
The stage is transparent, using errorStage.initStyle(StageStyle.TRANSPARENT);
Any content (that you explicitly want to see through) other than the root has fully transparent background
Here's a complete (albeit not very user-friendly) example:
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* JavaFX App
*/
public class App extends Application {
private Random rng = new Random();
#Override
public void start(Stage stage) {
Button createError = new Button("Try something dangerous");
createError.setOnAction(e -> {
try {
throw new Exception("Boom!");
} catch (Exception exc) {
BorderPane root = new BorderPane();
root.setTop(new Label("Error"));
Label stackTrace = new Label();
StringWriter sw = new StringWriter();
exc.printStackTrace(new PrintWriter(sw));
ScrollPane scroller = new ScrollPane(stackTrace);
stackTrace.setText(sw.toString());
root.setCenter(scroller);
Button close = new Button("Close");
HBox buttons = createHBox(close);
root.setBottom(buttons);
Scene errorScene = new Scene(root, 400, 400);
errorScene.setFill(Color.TRANSPARENT);
errorScene.getStylesheets().add(getClass().getResource("transparent.css").toExternalForm());
Stage errorStage = new Stage();
close.setOnAction(evt -> errorStage.close());
errorStage.setScene(errorScene);
errorStage.initStyle(StageStyle.TRANSPARENT);
errorStage.initModality(Modality.APPLICATION_MODAL);
errorStage.initOwner(stage);
errorStage.show();
}
});
HBox buttons = createHBox(createError);
BorderPane root = new BorderPane(createContent());
root.setBottom(buttons);
Scene scene = new Scene(root, 600, 600);
stage.setScene(scene);
stage.show();
}
private HBox createHBox(Node... content) {
HBox buttons = new HBox(content);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(2));
return buttons;
}
private Node createContent() {
Pane pane = new Pane();
for (int i = 0 ; i < 15 ; i++) {
Circle circle = new Circle(
50 + rng.nextDouble() * 500,
50 + rng.nextDouble() * 500,
50 + rng.nextDouble() * 50,
randomColor());
pane.getChildren().add(circle);
}
return pane ;
}
private Color randomColor() {
return Color.color(rng.nextDouble(), rng.nextDouble(), rng.nextDouble(), 0.5 + 0.25 * rng.nextDouble());
}
public static void main(String[] args) {
launch();
}
}
with transparent.css being:
.root {
-fx-background-color: #ffffff7f ;
}
.root HBox, .root .scroll-pane, .root .scroll-pane .viewport {
-fx-background-color: transparent ;
}

JavaFX - StackPane with Pane as EventDispatcher

In the event dispatcher pane occurs an mouse pressed event.
The pane one should show the context menu of it's combobox when a event occurs.
That works fine if the event is only dipatched to pane one.
When the event is dipatched to pane one and pane two the context menu of pane one doesn't show up.
I suppose it has something to do with the event tail and event consuming.
Until now i doesn't had a look at the EventDispatcher Class of the JDK itself.
Here is what i got so far:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author Robert
*/
public class EventDispatcherExample extends Application {
private Group root;
private StackPane cStackPane;
private Pane cPaneEventDispatcher;
private Pane cPaneOne;
private ComboBox cComboBox;
private Pane cPaneTwo;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
root = new Group();
cStackPane = new StackPane();
cStackPane.setPrefHeight(200.0);
cStackPane.setPrefWidth(200.0);
cPaneEventDispatcher = new Pane();
cPaneEventDispatcher.setPrefHeight(200.0);
cPaneEventDispatcher.setPrefWidth(200.0);
cPaneEventDispatcher.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane ED.");
cPaneOne.fireEvent(event);
cPaneTwo.fireEvent(event);
}
});
cPaneOne = new Pane();
cPaneOne.setPrefHeight(200.0);
cPaneOne.setPrefWidth(200.0);
cPaneOne.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane One.");
cComboBox.show();
}
});
ObservableList<String> observableList = FXCollections.observableArrayList();
observableList.add("1");
observableList.add("2");
observableList.add("3");
cComboBox = new ComboBox();
cComboBox.setLayoutX(50.0);
cComboBox.setLayoutY(50.0);
cComboBox.setPrefHeight(30.0);
cComboBox.setPrefWidth(100.0);
cComboBox.setItems(observableList);
cPaneTwo = new Pane();
cPaneTwo.setPrefHeight(200.0);
cPaneTwo.setPrefWidth(200.0);
cPaneTwo.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane Two.");
//Something will happen because of selected item in Combo Box of pane one...
}
});
cPaneOne.getChildren().add(cComboBox);
// add the nodes in reverse order
cStackPane.getChildren().add(cPaneTwo);
cStackPane.getChildren().add(cPaneOne);
cStackPane.getChildren().add(cPaneEventDispatcher);
root.getChildren().add(cStackPane);
Scene scene = new Scene(root, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Any ideas how to handle this?
After some ideas I got a solution that at least works:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* #author Robert
*/
public class EventDispatcherExample extends Application {
private Group root;
private StackPane cStackPane;
private Pane cPaneEventDispatcher;
private Pane cPaneOne;
private ComboBox cComboBox;
private boolean cComboBoxClicked = false;
private Pane cPaneTwo;
public static void main(String[] args) {
launch(args);
}
public boolean isComboBoxClicked() {
if (cComboBoxClicked == true) {
cComboBox.show();
} else {
cComboBox.hide();
}
return cComboBoxClicked;
}
#Override
public void start(Stage primaryStage) throws Exception {
root = new Group();
cStackPane = new StackPane();
cStackPane.setPrefHeight(200.0);
cStackPane.setPrefWidth(200.0);
cPaneEventDispatcher = new Pane();
cPaneEventDispatcher.setPrefHeight(200.0);
cPaneEventDispatcher.setPrefWidth(200.0);
cPaneEventDispatcher.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane ED.");
cPaneOne.fireEvent(event);
cPaneTwo.fireEvent(event);
}
});
cPaneOne = new Pane();
cPaneOne.setPrefHeight(200.0);
cPaneOne.setPrefWidth(200.0);
cPaneOne.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane One.");
Rectangle rect = new Rectangle(cComboBox.getLayoutX(), cComboBox.getLayoutY(),
cComboBox.getPrefWidth(), cComboBox.getPrefHeight());
cComboBoxClicked = rect.contains(event.getX(), event.getY());
}
});
ObservableList<String> observableList = FXCollections.observableArrayList();
observableList.add("1");
observableList.add("2");
observableList.add("3");
cComboBox = new ComboBox();
cComboBox.setLayoutX(50.0);
cComboBox.setLayoutY(50.0);
cComboBox.setPrefHeight(30.0);
cComboBox.setPrefWidth(100.0);
cComboBox.setItems(observableList);
cComboBox.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
// if cComboBoxSelectedIndex == 1 do Color on pane two
// if cComboBoxSelectedIndex == 2 do Size on pane two
// if cComboBoxSelectedIndex == 3 do ...
//System.out.println("newValue " + newValue);
}
});
cPaneTwo = new Pane();
cPaneTwo.setPrefHeight(200.0);
cPaneTwo.setPrefWidth(200.0);
cPaneTwo.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//System.out.println("Mouse pressed in Pane Two.");
boolean cComboBoxClicked = isComboBoxClicked();
if (cComboBoxClicked){
//System.out.println("Skip code internally managed by pane two.");
return;
}
// Internal code of pane two
//...
}
});
cPaneOne.getChildren().add(cComboBox);
// add the nodes in reverse order
cStackPane.getChildren().add(cPaneTwo);
cStackPane.getChildren().add(cPaneOne);
cStackPane.getChildren().add(cPaneEventDispatcher);
root.getChildren().add(cStackPane);
Scene scene = new Scene(root, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Perhaps a better approach comes up during the time of someone else.
Am looking forward...

I am confused by this javafx ActionEvent code

According to this code, button("enlarge") will trigger an action which is calling the method enlarge(), which will do: circle.setRadius(circle.getRadius() + 2);
which is just merely changing the radius value. what I don't understand is that how merely changing the radius will somehow make the program redraw the entire circle.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class ControlCircle extends Application {
private CirclePane1 circlePane = new CirclePane1();
#Override
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
hBox.getChildren().add(btEnlarge);
hBox.getChildren().add(btShrink);
btEnlarge.setOnAction(new EnlargeHandler());
btShrink.setOnAction(new ShrinkHandler());
BorderPane borderPane = new BorderPane();
borderPane.setCenter(circlePane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene(borderPane, 200, 150);
primaryStage.setTitle("ControlCircle");
primaryStage.setScene(scene);
primaryStage.show();
}
class EnlargeHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent e) {
circlePane.enlarge();
}
}
class ShrinkHandler implements EventHandler<ActionEvent>{
#Override
public void handle(ActionEvent e) {
circlePane.shrink();
}
}
}
class CirclePane1 extends StackPane{
private Circle circle = new Circle(50);
public CirclePane1() {
getChildren().add(circle);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
}
public void enlarge() {
circle.setRadius(circle.getRadius() + 2);
}
public void shrink() {
circle.setRadius(circle.getRadius() > 2 ? circle.getRadius() - 2
: circle.getRadius());
}
}
Looking at the source code of Circle you can see how the radius property is defined:
private final DoubleProperty radius = new DoublePropertyBase() {
#Override
public void invalidated() {
NodeHelper.markDirty(Circle.this, DirtyBits.NODE_GEOMETRY);
NodeHelper.geomChanged(Circle.this);
}
#Override
public Object getBean() {
return Circle.this;
}
#Override
public String getName() {
return "radius";
}
};
When you change the value of the radius property its invalidated() method is called. And this is where the "magic" happens, it calls these two methods:
NodeHelper.markDirty(Circle.this, DirtyBits.NODE_GEOMETRY);
NodeHelper.geomChanged(Circle.this);
Now, I don't know what these methods do specifically - nor do I have the time/desire to study it - but they tell the JavaFX runtime that the Circle needs to be redrawn. This means that the next time a rendering pulse occurs, which may be triggered by these methods, the Circle will be drawn with its new radius.
This is all very optimized and will cause a redraw only when applicable (such as only when part of ascene-graph).

Drawing user input on Image JavaFX

Suppose you have an app that displays user graphic (some kind of image) then you want to allow the user to draw some lines on this image. I have the following questions regarding such situation:
How would you accomplish that?
How would you get pixel coordinates for the image from the user drag events?
How would you update the image in real time?
I will give you an example of the exact opposite [erasing the Image on JavaFX]
which I suppose will be enough as a starter point for you:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class EraseImageonCanvas extends Application {
private Pane root = new Pane();
private void setCanvas(Canvas canvas, Image img) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(img, 0, 0,canvas.getWidth(), canvas.getHeight());
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Erasing the Image");
Rectangle rect = new Rectangle(400, 400);
drawBackground(rect);
root.getChildren().add(rect);
final Canvas canvas = new Canvas(200, 200);
canvas.setTranslateX(100);
canvas.setTranslateY(100);
//For local images use
//image = new Image(getClass().getResource(#Path#).openStream());
final Image image = new Image(
"http://kyllo.com.br/wp-content/uploads/2013/12/Faroeste-Cabloco.jpg"
);
setCanvas(canvas,image);
final GraphicsContext gc = canvas.getGraphicsContext2D();
// Clear away portions as the user drags the mouse
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
gc.clearRect(e.getX() - 2, e.getY() - 2, 5, 5);
}
});
// Reset the Canvas when the user double-clicks
canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.getClickCount() >1) {
setCanvas(canvas, image);
}
}
});
// Add the Canvas to the Scene, and show the Stage
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
//Draws the background with a RadialGradient
private void drawBackground(Rectangle rect) {
rect.setFill(new LinearGradient(0, 0, 1, 1, true,
CycleMethod.REFLECT,
new Stop(0, Color.RED),
new Stop(1, Color.YELLOW)));
}
public static void main(String[] args) {
launch(args);
}
}
Download it on gist
this Canvas tutorial by Oracle shows exactly what you want to accomplish in the "Interacting with the User" section.
It shows how you can add an EventHandler to the Canvas to handle MouseEvent such as MouseEvent.MOUSE_DRAGGED. The GraphicsContext is then used to get the x and y coordinates and draw on the canvas.
In order to use the Canvas outside the main Application class, you'd declare the Canvas in your .fxml file as such:
<BorderPane fx:controller="controllers.MyController"
xmlns:fx="http://javafx.com/fxml">
<Canvas fx:id="drawArea" height="..." width="..."/>
</BorderPane>
Then, on your MyController class:
public class MyController implements Initializable {
#FXML
private Canvas drawArea;
private GraphicsContext gc;
#Override
public void initialize(URL location, ResourceBundle resources) {
gc = drawArea.getGraphicsContext2D();
// Java 8 syntax, beware!
drawArea.setOnMouseDragged(event -> gc.fillRect(event.getX(), event.getY(), 5, 5));
}
}

Drawing rounded rectangle while dragging fails

I am trying to draw a simple rectangle with rounded corners in javafx while dragging the mouse.
First issue: I can't make the upper-left corner rounded.
Second issue: I want my application to be able to draw nice and full rounded rectangles not like the second one from my picture.
Please, how can I fix these?
Thanks in advance for your help.
Here is my code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage){
stage.setTitle("Test");
root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
paintings = new Group();
stage.setScene(scene);
canvas = new Rectangle(0, 0, stage.getScene().getWidth(), stage.getScene().getHeight());
canvas.setFill(Color.WHITE);
addHandlers();
root.setCenter(canvas);
root.getChildren().add(paintings);
stage.show();
}
private void paintRectangle(){
roundedRect = new Rectangle(pressedX,
pressedY,
draggedX - pressedX,
draggedY - pressedY);
roundedRect.setFill(Color.RED);
roundedRect.setArcHeight(40);
roundedRect.setArcWidth(40);
paintings.getChildren().add(roundedRect);
}
private void addHandlers(){
canvas.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
pressedX = me.getX();
pressedY = me.getY();
}
});
canvas.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
/* If I make the drawing in here, I get round corners, but it doesn't get drawn while dragging*/
//paintRectangle();
}
});
canvas.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
draggedX = me.getX();
draggedY = me.getY();
paintRectangle();
}
});
}
double pressedX, pressedY;
double draggedX, draggedY;
Rectangle canvas;
Group paintings;
Rectangle roundedRect;
BorderPane root;
}
And here is my picture:
I could not examine your code but here is an alternative approach. You may merge this with your code:
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Daer extends Application {
BorderPane pane;
Rectangle rect;
SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
SimpleDoubleProperty rectX = new SimpleDoubleProperty();
SimpleDoubleProperty rectY = new SimpleDoubleProperty();
#Override
public void start(Stage stage) {
pane = new BorderPane();
Scene scene = new Scene(pane, 800, 600);
stage.setScene(scene);
scene.setOnMouseDragged(mouseHandler);
scene.setOnMousePressed(mouseHandler);
scene.setOnMouseReleased(mouseHandler);
rect = getNewRectangle();
rect.widthProperty().bind(rectX.subtract(rectinitX));
rect.heightProperty().bind(rectY.subtract(rectinitY));
pane.getChildren().add(rect);
stage.show();
}
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
rectX.set(mouseEvent.getX());
rectY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
// Hide the rectangle
rectX.set(0);
rectY.set(0);
}
}
};
private Rectangle getNewRectangle() {
Rectangle r = new Rectangle();
r.setFill(Color.web("blue", 0.1));
r.setStroke(Color.BLUE);
r.setArcHeight(40);
r.setArcWidth(40);
return r;
}
public static void main(String[] args) {
launch(args);
}
}

Resources