Add FreeHand functionality in JavaFX - javafx

Actually I am making an application which allows user to crop an image an then gives functionality of free-hand drawing and to also to draw different shapes. I have achieved functionality of cropping and drawing shapes like line etc. But I am facing some problems in free-hand drawing.
I have added my image on "HBox" and cropped that image through "Rectangle" in JavaFX class which is added on "Group". And all these are added on "Pane" class. Now for free-hand drawing, I am using "Canvas". Where to add canvas, whether on "HBox" or "Group" or "Pane" class. And Canvas is only initialized on clicking pencil button. I have added single functions for Mouse-Events and applied if-checks for different functionalities in those functions.
basically how to draw pencil on image or how to add lineTo on group.
Can someone please help me in solving my problem??
`
package application;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.Rectangle;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
int dragStatus = 0;
double startingPointX;
double startingPointY;
double currentEndingPointX;
double currentEndingPointY;
BufferedImage bufferedImage;
Robot robot;
ImageView imageView;
Button pencilBtn;
Image image;
Pane rootPane;
HBox pictureRegion;
Scene scene;
Rectangle croppedArea;
Canvas canvas;
GraphicsContext gc;
WritableImage writableImage;
Group group = new Group();
Line line;
LineTo lineTo;
String shape;
Boolean canvasAdded;
#Override
public void start(Stage primaryStage) {
try {
int sleepTime = 120;
Thread.sleep(sleepTime);
pencilBtn = createButton("save.png");
pictureRegion = new HBox();
rootPane = new Pane();
scene = new Scene(rootPane);
robot = new Robot();
java.awt.Rectangle capture = new java.awt.Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
bufferedImage = robot.createScreenCapture(capture);
image = ConvertBufferedToJavaFXImage.convertToFxImage(bufferedImage);
pencilBtn.setOnAction(e -> geometrySelection("pencil"));
croppedArea = new Rectangle();
if(canvas != null) {
canvas.setOnMousePressed(e -> onMousePressed(e));
canvas.setOnMousePressed(e -> onMouseDragged(e));
}
imageView = new ImageView(image);
scene.setOnMouseEntered(e -> onMouseEntered(e, "scene"));
scene.setOnMousePressed(e -> onMousePressed(e));
scene.setOnMouseReleased(e -> onMouseReleased(e));
scene.setOnMouseDragged(e -> onMouseDragged(e));
pictureRegion.getChildren().add(imageView);
rootPane.getChildren().add(pictureRegion);
rootPane.getChildren().add(group);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setMaximized(true);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onMouseEntered(MouseEvent ev, String nodeType) {
if (nodeType == "scene") {
scene.setCursor(Cursor.CROSSHAIR);
} else if (nodeType == "croppedArea") {
croppedArea.setCursor(Cursor.TEXT);
//croppedArea.setOnMousePressed(e -> onMousePressed(e));
} else if (nodeType == "btn") {
pencilBtn.setCursor(Cursor.HAND);
}
}
public void onMousePressed(MouseEvent event) {
if ((startingPointX < event.getX() && event.getX() < currentEndingPointX)
&& (startingPointY < event.getY() && event.getY() < currentEndingPointY)) {
if(shape == "pencil") {
lineTo = new LineTo();
System.out.println("lineTo1");
gc.beginPath();
gc.lineTo(event.getX(), event.getY());
gc.stroke();
}
} else {
shape = "croppedArea";
rootPane.getChildren().remove(canvas);
group.getChildren().clear();
startingPointX = event.getX();
startingPointY = event.getY();
group.getChildren().add(croppedArea);
}
}
public void onMouseDragged(MouseEvent event) {
dragStatus = 1;
if (dragStatus == 1) {
// if((startingPointX < event.getX() && event.getX() < currentEndingPointX)
// && (startingPointY < event.getY() && event.getY() < currentEndingPointY)) {
if(shape == "pencil") {
System.out.println("lineTo1");
gc.lineTo(event.getX(), event.getY());
gc.stroke();
}
else {
currentEndingPointX = event.getX();
currentEndingPointY = event.getY();
rootPane.getChildren().remove(pencilBtn);
croppedArea.setFill(Color.TRANSPARENT);
croppedArea.setStroke(Color.BLACK);
croppedArea.setOnMouseEntered(e -> onMouseEntered(e, "croppedArea"));
adjustRectangleProperties(startingPointX, startingPointY, currentEndingPointX, currentEndingPointY,
croppedArea);
}
}
}
public void geometrySelection(String tempShape) {
if(tempShape == "pencil") {
shape = "pencil";
canvas = new Canvas();
gc = canvas.getGraphicsContext2D();
//gc.setFill(Color.LIGHTGRAY);
gc.setStroke(Color.BLACK);
gc.setLineWidth(5);
canvas.setLayoutX(startingPointX);
canvas.setLayoutY(startingPointY);
canvas.setWidth(croppedArea.getWidth());
canvas.setHeight(croppedArea.getHeight());
System.out.println("canvasAdded");
group.getChildren().add(canvas);
}
}
public void onMouseReleased(MouseEvent event) {
if(dragStatus == 1) {
if(shape == "croppedArea") {
if (croppedArea.getHeight() > 0 && croppedArea.getWidth() > 0) {
pencilBtn.setLayoutX(Math.max(startingPointX, currentEndingPointX) + 5);
pencilBtn.setLayoutY(Math.max(startingPointY, currentEndingPointY) - 120);
rootPane.getChildren().add(pencilBtn);
dragStatus = 0;
}
}
bufferedImage = robot.createScreenCapture(new java.awt.Rectangle((int) startingPointX, (int) startingPointY,
(int) croppedArea.getWidth(), (int) croppedArea.getHeight()));
}
}
void adjustRectangleProperties(Double startingPointX, Double startingPointY, Double currentEndingPointX,
Double currentEndingPointY, Rectangle givenRectangle) {
givenRectangle.setX(startingPointX);
givenRectangle.setY(startingPointY);
givenRectangle.setWidth(currentEndingPointX - startingPointX);
givenRectangle.setHeight(currentEndingPointY - startingPointY);
if (givenRectangle.getWidth() < 0) {
givenRectangle.setWidth(-givenRectangle.getWidth());
givenRectangle.setX(givenRectangle.getX() - givenRectangle.getWidth());
}
if (givenRectangle.getHeight() < 0) {
givenRectangle.setHeight(-givenRectangle.getHeight());
givenRectangle.setY(givenRectangle.getY() - givenRectangle.getHeight());
}
}
public Button createButton(String imageName) throws FileNotFoundException {
FileInputStream iconFile = new FileInputStream(imageName);
Image iconImage = new Image(iconFile);
Button button = new Button();
button.setGraphic(new ImageView(iconImage));
button.setMaxSize(20, 20);
button.setPadding(Insets.EMPTY);
return button;
}
public static void main(String[] args) {
launch(args);
}
}
`

Related

How to get Key Press working on two file Javafx project

I've a "driver" file and a source file for the project I'm working on right now. In my driver file I add an EventHandle to me stage.
stage.addEventHandler(KeyEvent.KEY_TYPED, shape.getKeyHandler());
Now in my source file, Im try to define the function "getKeyHandler" in my source file. I've created a new EventHandler inside of the function. And went about creating a an event handler that takes in whatever key you press (R, G or B) and changes the circle/square on screen to its corresponding color. (G = Green, etc.)
The problem I'm running into is I'm not sure how to go about setting up a EventHandler that takes (KeyEvent.KEY_CLICKED). My two other EventHandlers were never called in the driver file, so I was able to this.addEventHandler to start off, now I'm in a different boat. Could someone push me in the correct direction.
Source File:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.input.*;
import javafx.geometry.Point2D;
public class Multishape extends Group {
//Declaring variables here gives them greater scope
private Rectangle rectangle;
private Circle circle;
private Point2D prev;
public Multishape (double x, double y, double len){
rectangle = new Rectangle((len*2), (len*2), Color.RED);
rectangle.setX(x-len);
rectangle.setY(y-len);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(1);
circle = new Circle(len, Color.BLUE);
circle.setCenterX(x);
circle.setCenterY(y);
circle.setStroke(Color.BLACK);
circle.setStrokeWidth(1);
this.getChildren().add(rectangle);
this.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent me) ->
{
if(me.getButton() == MouseButton.PRIMARY)
{
if(this.getChildren().get(0).equals(rectangle))
{
this.getChildren().set(0, circle);
}
else
{
this.getChildren().set(0, rectangle);
}
}
});
prev = new Point2D(320, 240);
this.addEventHandler(MouseEvent.MOUSE_DRAGGED, (MouseEvent me) ->
{
if(me.getButton() == MouseButton.SECONDARY)
{
if(this.getChildren().get(0).equals(rectangle))
{
rectangle.setX(prev.getX()-len);
rectangle.setY(prev.getY()-len);
circle.setCenterX(prev.getX());
circle.setCenterY(prev.getY());
}
else
{
circle.setCenterX(prev.getX());
circle.setCenterY(prev.getY());
rectangle.setX(prev.getX()-len);
rectangle.setY(prev.getY()-len);
}
}
prev = new Point2D(me.getX(), me.getY());
});
}
public EventHandler<KeyEvent> getKeyHandler(){
EventHandler<KeyEvent> keyPressHandler = new
EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event){
if(event.getCode() == KeyCode.R){
circle.setFill(Color.RED);
rectangle.setFill(Color.RED);
}
if(event.getCode() == KeyCode.G){
circle.setFill(Color.GREEN);
rectangle.setFill(Color.GREEN);
}
if(event.getCode() == KeyCode.B){
circle.setFill(Color.BLUE);
rectangle.setFill(Color.BLUE);
}
}
};
return keyPressHandler;
}
}
Driver File:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.input.KeyEvent;
import javafx.event.EventHandler;
public class MultishapeDriver extends Application
{
public static void main(String[] args)
{
launch(args);
}
public void start(Stage stage)
{
stage.setTitle("Multishape lab");
Group root = new Group();
Multishape shape = new Multishape(320, 240, 40);
root.getChildren().add(shape);
stage.addEventHandler(KeyEvent.KEY_TYPED,
shape.getKeyHandler());
stage.setScene(new Scene(root, 640, 480));
stage.show();
}
}
The KeyEvent.code property is undefined for KEY_TYPED events. You either need to use the character property
Color color = null;
switch(event.getCharacter().toLowerCase()) {
case "r":
color = Color.RED;
break;
case "g":
color = Color.LIME; // or green, but lime is #00ff00
break;
case "b":
color = Color.BLUE;
break;
default:
return;
}
circle.setFill(color);
rectangle.setFill(color);
or use a different event type, e.g. KEY_PRESSED
stage.addEventHandler(KeyEvent.KEY_PRESSED, shape.getKeyHandler());

JavaFX AnimationTimer renders

My goal is to completely sync each animation frame to the monitor device vsync at perfectly 60fps. I googled online, and people suggested that i could use JavaFX AnimationTimer. I wrote a simple ball JavaFX AnimationTimer, but i could sometimes see a very noticeable jittery in the animation. Could you guys please help me?
I have attached the code here:
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Main extends Application
implements EventHandler <KeyEvent>
{
final int WIDTH = 600;
final int HEIGHT = 400;
double ballRadius = 40;
double ballX = 100;
double ballY = 200;
double xSpeed = 4;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Basic JavaFX demo");
Group root = new Group();
Scene scene = new Scene(root, WIDTH, HEIGHT);
// Bouncing Ball
Circle circle = new Circle();
circle.setCenterX(ballX);
circle.setCenterY(ballY);
circle.setRadius(ballRadius);
circle.setFill(Color.BLUE);
root.getChildren().add(circle);
// need to attach KeyEvent caller to a Node of some sort.
// How about an invisible Box?
final Box keyboardNode = new Box();
keyboardNode.setFocusTraversable(true);
keyboardNode.requestFocus();
keyboardNode.setOnKeyPressed(this);
root.getChildren().add(keyboardNode);
stage.setScene(scene);
stage.show();
AnimationTimer animator = new AnimationTimer(){
#Override
public void handle(long arg0) {
long startTime = System.currentTimeMillis();
// UPDATE
ballX += xSpeed;
if (ballX + ballRadius >= WIDTH)
{
ballX = WIDTH - ballRadius;
xSpeed *= -1;
} else if (ballX - ballRadius < 0) {
ballX = 0 + ballRadius;
xSpeed *= -1;
}
// RENDER
circle.setCenterX(ballX);
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
System.out.println(elapsedTime);
}
};
animator.start();
}
#Override
public void handle(KeyEvent arg0) {
if (arg0.getCode() == KeyCode.SPACE )
{
xSpeed *= -1;
}
}
}

Circle movement upon rectangle Collision

I'm working on understanding collision detection and movement and I am having issues getting the movement down correctly. my goal is to allow a player to move around rectangles but not through them. my problem is that once the player reaches the bounds of the rectangle they are unable to backup. any help would be greatly appreciated.
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import static javafx.scene.input.KeyCode.DOWN;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
public class CircleMovement extends Application {
//create the panes to handle the game
BorderPane mainPane = new BorderPane();
GridPane infoPane = new GridPane();
Pane gameField = new Pane();
Scene scene;
//create circle/movement properties
double increment = 5.0;
double radius = 10;
double x = radius, y = radius;
//create Rectangle properties
double Rwidth = 80;
double Rheight = 20;
//create player and objects
Circle player = new Circle(x, y, radius);
Rectangle r1 = new Rectangle(0, 100, Rwidth, Rheight);//Rectangle(int x, int y, int width, int height)
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
setPanes();
gameField.getChildren().addAll(r1, player);
moveCircleOnKeyPress(scene, player);//pass to player movement method
//set the stage
stage.setScene(scene);
stage.show();
}
public void moveCircleOnKeyPress(Scene scene, Circle player) {//player movement
scene.setOnKeyPressed((KeyEvent event) -> {
switch (event.getCode()) {
case UP:
player.setCenterY(player.getCenterY() - increment);//move player
if (player.getBoundsInLocal().intersects(r1.getBoundsInLocal())) {
player.centerYProperty().setValue(r1.getY());
}
break;
case RIGHT:
player.setCenterX(player.getCenterX() + increment);
if (player.getBoundsInLocal().intersects(r1.getBoundsInLocal())) {
player.centerYProperty().setValue(r1.getY());
}
break;
case DOWN:
player.setCenterY(player.getCenterY() + increment);
if (player.getBoundsInLocal().intersects(r1.getBoundsInLocal())) {
player.centerYProperty().setValue(r1.getY());
}
break;
case LEFT:
player.setCenterX(player.getCenterX() - increment);
if (player.getBoundsInLocal().intersects(r1.getBoundsInLocal())) {
player.centerYProperty().setValue(r1.getY());
}
break;
}
});
}
public void setPanes() {
infoPane.setMaxSize(200, 200);
mainPane.setMaxSize(800, 800);
mainPane.setCenter(gameField);//place the main game inside the center
mainPane.setRight(infoPane);
mainPane.setPadding(new Insets(10, 10, 10, 10));
mainPane.setStyle("-fx-border-color: red");
gameField.setStyle("-fx-background-color: white");
gameField.setStyle("-fx-border-color: black");
gameField.setMaxSize(600, 600);
scene = new Scene(mainPane, 800, 800);
player.setFill(Color.RED);
}
}
Since you are using Shape, it's a good idea to use Shape.intersect(player, r1).getBoundsInLocal().getWidth() != -1 to detect intersection. Also, instead of using player.centerYProperty().setValue(...);, just use player.setCenterY(...). You had a couple more problems, but the most important one is the idea of what to do once an intersection is detected. Once an intersection is detected you should go back to the distance just before the detection(only if you are moving in small enough steps).
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import static javafx.scene.input.KeyCode.DOWN;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class CircleMovement extends Application
{
//create the panes to handle the game
BorderPane mainPane = new BorderPane();
GridPane infoPane = new GridPane();
Pane gameField = new Pane();
Scene scene;
//create circle/movement properties
double increment = 5.0;
double radius = 10;
double x = radius, y = radius;
//create Rectangle properties
double Rwidth = 80;
double Rheight = 20;
//create player and objects
Circle player = new Circle(x, y, radius);
Rectangle r1 = new Rectangle(0, 100, Rwidth, Rheight);//Rectangle(int x, int y, int width, int height)
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage stage) throws Exception
{
setPanes();
gameField.getChildren().addAll(r1, player);
moveCircleOnKeyPress(scene, player);//pass to player movement method
//set the stage
stage.setScene(scene);
stage.show();
}
public void moveCircleOnKeyPress(Scene scene, Circle player)
{//player movement
scene.setOnKeyPressed((KeyEvent event) ->
{
switch (event.getCode())
{
case UP:
player.setCenterY(player.getCenterY() - increment);//move player
//if (player.getBoundsInLocal().intersects(r1.getBoundsInLocal()))
if (Shape.intersect(player, r1).getBoundsInLocal().getWidth() != -1)
{
player.setCenterY(player.getCenterY() + increment);
}
break;
case RIGHT:
player.setCenterX(player.getCenterX() + increment);
if (Shape.intersect(player, r1).getBoundsInLocal().getWidth() != -1)
{
player.setCenterX(player.getCenterX() - increment);
}
break;
case DOWN:
player.setCenterY(player.getCenterY() + increment);
if (Shape.intersect(player, r1).getBoundsInLocal().getWidth() != -1)
{
player.setCenterY(player.getCenterY() - increment);
}
break;
case LEFT:
player.setCenterX(player.getCenterX() - increment);
if (Shape.intersect(player, r1).getBoundsInLocal().getWidth() != -1)
{
player.setCenterX(player.getCenterX() + increment);
}
break;
}
});
}
public void setPanes()
{
infoPane.setMaxSize(200, 200);
mainPane.setMaxSize(800, 800);
mainPane.setCenter(gameField);//place the main game inside the center
mainPane.setRight(infoPane);
mainPane.setPadding(new Insets(10, 10, 10, 10));
mainPane.setStyle("-fx-border-color: red");
gameField.setStyle("-fx-background-color: white");
gameField.setStyle("-fx-border-color: black");
gameField.setMaxSize(600, 600);
scene = new Scene(mainPane, 800, 800);
player.setFill(Color.RED);
}
}

Highlighting rectangle when more than half overlaps

I have a JavaFX application with a pane that contains rectangles. These rectangles can be moved by dragging the mouse.
When I drag a rectangle over another rectangle, I would like the second (background) rectangle to be highlighted. This works, see code below
private boolean moveInProgress;
private Point2D prevPos;
public void onMousePressed(MouseEvent event) {
setMouseTransparent(true);
Point2D point = new Point2D(event.getSceneX(), event.getSceneY());
if (!moveInProgress) {
moveInProgress = true;
prevPos = point;
LOG.debug("Mouse move started on location " + prevPos);
}
event.consume();
}
public void onMouseDragged(MouseEvent event) {
if (moveInProgress) {
Point2D point = new Point2D(event.getSceneX(), event.getSceneY());
this.toFront();
double[] translationVector = new double[2];
translationVector[0] = point.getX() - prevPos.getX();
translationVector[1] = point.getY() - prevPos.getY();
setTranslateX(getTranslateX() + translationVector[0]);
setTranslateY(getTranslateY() + translationVector[1]);
prevPos = point;
}
event.consume();
}
public void onMouseReleased(MouseEvent event) {
setMouseTransparent(false);
if (moveInProgress) {
moveInProgress = false;
}
event.consume();
}
public void onDragDetected(MouseEvent event) {
startFullDrag();
event.consume();
}
public void onMouseDragEntered(MouseDragEvent event) {
getStyleClass().add("drag-target");
event.consume();
}
public void onMouseDragExited(MouseDragEvent event) {
if (getStyleClass().contains("drag-target")) {
getStyleClass().remove("drag-target");
}
event.consume();
}
I would like to highlight the underlying rectangle when more than half of my dragging rectangle overlaps. In this picture, I would like to highlight the red rectangle, since the grey rectangle overlaps more than half of it.
The problem is that the MouseDragEntered and MouseDragExited events are fired based on my mouse position. When my mouse position is for example the black dot in the picture, my mouse events will only be fired when my mouse enters the red rectangle.
Can anyone give me some pointers how to highlight the red rectangle when during a drag action of the grey rectangle, more than half of it overlaps?
One approach is to have each rectangle observe the bounds of the rectangle that is being dragged. Then it's reasonably easy to do a computation using Shape.intersect (or by other means) to see if the rectangle is 50% covered by the rectangle being dragged. The tricky part here is adding the listeners to the rectangle being dragged and removing them again when the rectangle stops being dragged.
Here's a quick example. I think I have things set up a little differently from the way you have them set up, but you should be able to adapt this to your use case easily enough.
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.css.PseudoClass;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class DraggingHighlightRectangles extends Application {
private final Random rng = new Random();
private final ObjectProperty<Rectangle> draggingRectangle = new SimpleObjectProperty<>();
#Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
pane.setMinSize(600, 600);
Button newRectButton = new Button("New Rectangle");
newRectButton.setOnAction(e -> pane.getChildren().add(createRectangle()));
BorderPane.setAlignment(newRectButton, Pos.CENTER);
BorderPane.setMargin(newRectButton, new Insets(5));
BorderPane root = new BorderPane(pane);
root.setBottom(newRectButton);
Scene scene = new Scene(root);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private Rectangle createRectangle() {
Rectangle rect = new Rectangle(rng.nextInt(400)+100, rng.nextInt(500)+50, 100, 50);
rect.setFill(randomColor());
rect.getStyleClass().add("rect");
ChangeListener<Bounds> boundsListener = (obs, oldBounds, newBounds) -> {
double myArea = rect.getWidth() * rect.getHeight() ;
Shape intersection = Shape.intersect(draggingRectangle.get(), rect);
Bounds intersectionBounds = intersection.getBoundsInLocal();
double intersectionArea = intersectionBounds.getWidth() * intersectionBounds.getHeight() ;
rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("highlight"), intersectionArea >= 0.5 * myArea);
};
draggingRectangle.addListener((obs, oldRect, newRect) -> {
if (oldRect != null) {
oldRect.boundsInLocalProperty().removeListener(boundsListener);
}
if (newRect != null && newRect != rect) {
newRect.boundsInLocalProperty().addListener(boundsListener);
}
rect.pseudoClassStateChanged(PseudoClass.getPseudoClass("highlight"), false);
});
class MouseLocation { double x, y ; }
MouseLocation mouseLocation = new MouseLocation();
rect.setOnMousePressed(e -> {
draggingRectangle.set(rect);
rect.toFront();
mouseLocation.x = e.getX() ;
mouseLocation.y = e.getY() ;
});
rect.setOnMouseDragged(e -> {
rect.setX(rect.getX() + e.getX() - mouseLocation.x);
rect.setY(rect.getY() + e.getY() - mouseLocation.y);
mouseLocation.x = e.getX() ;
mouseLocation.y = e.getY() ;
});
rect.setOnMouseReleased(e -> draggingRectangle.set(null));
return rect ;
}
private Color randomColor() {
return Color.rgb(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256));
}
public static void main(String[] args) {
launch(args);
}
}
My stylesheet, style.css, just contains
.rect:highlight {
-fx-fill: yellow ;
}

Transparent Stage should not minimized when clicked inside in Javafx

I am learning to create Screen Recording application in JavaFx. I want user to resize the rectangle to decide the screen capture area. I have made stage and scene Transparent by primaryStage.initStyle(StageStyle.TRANSPARENT); and scene.setFill(null); .
I am able to resize the rectangular section but the problem is When I click inside the stage, It gets minimized as it is transparent. How to solve this issue ?
I have seen this application screencast-o-matics and following the same. Please guide me on this.
Edit::
Code:
import java.awt.Toolkit;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class ScreenCaptureDemo extends Application {
Rectangle rectangle ;
double x0,y0;
public static void main(String[] args) {
launch(ScreenCaptureDemo.class);
}
#Override
public void start(final Stage primaryStage) throws Exception {
BorderPane borderPane = new BorderPane();
GridPane gridPane = new GridPane();
HBox box = new HBox();
Button button1 = new Button("button2");
Button button2 = new Button("Button3");
Button button = new Button("button");
box.getChildren().add(button);
box.getChildren().add(button1);
box.getChildren().add(button2);
rectangle = new Rectangle(500.0, 500.0);
rectangle.setStrokeWidth(2);
rectangle.setArcHeight(15.0);
rectangle.setArcWidth(15.0);
rectangle.setFill(Color.TRANSPARENT);
rectangle.setStroke(Color.RED);
rectangle.setStrokeWidth(5);
rectangle.getStrokeDashArray().addAll(3.0,13.0,3.0,7.0);
gridPane.add(rectangle, 0, 0);
gridPane.add(box, 0, 1);
borderPane.setCenter(gridPane);
Scene scene = new Scene(borderPane,Toolkit.getDefaultToolkit().getScreenSize().getWidth()-100,Toolkit.getDefaultToolkit().getScreenSize().getHeight()-100);
scene.setOnMouseDragged(mouseHandler);
scene.setOnMousePressed(mouseHandler);
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.TRANSPARENT);
scene.setFill(null);
rectangle.setMouseTransparent(true);
rectangle.setPickOnBounds(true);
primaryStage.show();
}
void setScaleRect(double sX, double sY){
rectangle.setHeight(sY);
rectangle.setWidth(sX);
}
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
double heightLowerLimit = rectangle.getHeight()-500;
double heightUpperLimit = rectangle.getHeight()+500;
double widthLowerLimit = rectangle.getWidth()-500;
double widthUpperLimit = rectangle.getWidth()+500;
if ((mouseEvent.getY() >heightLowerLimit && mouseEvent.getY() < heightUpperLimit) &&
(mouseEvent.getX() >widthLowerLimit && mouseEvent.getX() < widthUpperLimit)
) {
double scaleX = mouseEvent.getX();
double scaleY = mouseEvent.getY();
setScaleRect(scaleX, scaleY);
} else if ((mouseEvent.getY() >heightLowerLimit && mouseEvent.getY() < heightUpperLimit)
&& (mouseEvent.getX() <widthLowerLimit && mouseEvent.getX() > widthUpperLimit)) {
double scaleY = mouseEvent.getY();
double scaleX=rectangle.getWidth();
setScaleRect(scaleX, scaleY);
} else if (mouseEvent.getY() != rectangle.getHeight()
&& mouseEvent.getX() == rectangle.getWidth()) {
double scaleX = mouseEvent.getX();
double scaleY=rectangle.getHeight();
setScaleRect(scaleX, scaleY);
}
}
}
};
}
Thank you in advance
Fill the rectangle as
rectangle.setFill(Color.web("blue", 0.1));
// or more transparent
rectangle.setFill(Color.web("gray", 0.01));

Resources