How to draw a line in JavaFX with the mouse? - javafx

I am working on a paint program in which a button is clicked that toggles the button that draws lines. My code doesn't do what I want it to and instead draws a strange fan shape whenever I try to draw multiple lines. I want it to be able to draw multiple straight lines with the mouse.
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
initialTouch = new Pair<>(event.getX(), event.getY());
}
});
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
gc.strokeLine(initialTouch.getKey(), initialTouch.getValue(), event.getX(), event.getY());
}
});
canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
}
});
}```
That method is called when a button is pushed to draw the line. I am expecting multiple lines, but I stead get a fan shape.

What you do, is:
You create start Point.
And then for each DRAG movement you create a OWN line from the Start location to the new Location. Hence you create a lot of lines that resemble a shape.
What you want is:
Create a start point and than draw to the end Point.
So you should ONLY draw a line to the Graphical Context when the Mouse released.
just Move what you do in "dragged" to the "released" part and it sould work.
When you want "Preview" you should Use a Line Object and add it to the View, and then when release the mouse you should delete the Object it.

Related

JavaFX - Separate Child Bounding Box from Parent

I am having an issue with a graph I have made in JavaFX. I am creating a label and adding it to a point on mouse enter:
Label label = new Label(s);
label.setTranslateY(-20.0);
...
setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
getChildren().setAll(label);
toFront();
}
});
setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
getChildren().clear();
}
});
This ends up with the point and the label sharing a bounding box.
I would like the bounding boxes of the two to be separate. This way, when the cursor exits the point, the label should disappear. Currently, the label will still display as long as it is in the box generated by the label and point combined.
I would appreciate any help on figuring out how to separate the bounding boxes of these two elements.
Thanks!
To solve this, I ended up creating a new empty node which sits at the top middle of the graph. When a point is hovered, the label is added to the newly created node. This allows the user to hover the points without the label getting in the way.

Javafx retrievable drag and drop bug

I am creating a javafx application with a drag and drop feature. I need to be able to drag from one pane and drop to the next pane. After dropping, all dropped nodes need to be accessible. The drag and drop is working smoothly. However, I am having a problem in which not all nodes are accessible after being dropped. I would drag a node and click on it and sometimes it would respond, but sometimes a random node would no longer exhibit capabilities when I clicked on it. There was no noticeable pattern (that I could see) about which nodes stopped working and which always worked. If anyone has any idea as to why this might be a problem or how to fix it, it would be greatly appreciated. I have looked everywhere and found only one similar issue a few years ago but the page with their solution had been removed.
I will briefly explain how I set up my code structure.
The nodes I am dragging extend StackPane and I drag these nodes onto a center Pane from an ImageView in VBox. However, both of these are children of a Main layout that extends BorderPane which I have called MainEntrance.
In my node's class I have onMousePressed and onMouseDragged features as well as onContextMenuRequested features. In the class of the MainEntrance, I have drag features, as well, that handle when an object is first dragged onto the child Pane from the other child VBox.
The following are the relevant classes/methods:
VBox class: (where nodes are dragged from)
ImageView iV= new ImageView(image);
iV.setId("image");
iV.setOnDragDetected(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
dragAndDrop(iV, iV.getId(), event);
}
});
I have drag three ImageViews and these ImageViews are children of a VBox.
MainEntrance:
In this class I have a method that registers my drag events to my pane as such:
PView is my class extension of Pane
public void registerOnDragDropped(PView pView){
pView.setOnDragOver(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
System.out.println("drag over registered");
}
});
pView.setOnDragDropped(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
Dragboard db = event.getDragboard();
final String id = db.getString();
double mousePosY = event.getY();
double mousePosX = event.getX();
deviceCounter += 1;
pView.createBoardView(id, deviceCounter, mousePosX, mousePosY);
event.setDropCompleted(true);
}
});
pView.setOnDragExited(new EventHandler<DragEvent>() {
#Override
public void handle(DragEvent event) {
event.acceptTransferModes(TransferMode.ANY);
//computerView.setEffect(null);
event.consume();
}
});
}
Note: this method is in my MainEntrance class (BorderPane). In this class is when I initialize all other child nodes of MainEntrance.
Node: (draggable nodes extend StackPane)
my node's handlers are in a method that I called makeDraggable()
public void makeDraggable(Node node) {
this.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.getScene().setCursor(Cursor.MOVE);
System.out.println("makeDraggable mouse pressed");
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
node.setTranslateX(node.getTranslateX() + event.getX() - 35);
node.setTranslateY(node.getTranslateY() + event.getY() - 35);
event.consume();
node.toFront();
}
});
}
If any other information is needed please let me know. Thank you in advance!
I have figured out the solution to this problem.
The problem was in the rendering order of an attachment I had placed on the node. So when I drag the node on to the screen, I have another node (a line placed on a Pane) that binds the center of the screen to the node. I did not initially think that this was the problem, so I did not even include that class or snippet. However, after playing around with it I realized that this indeed was the problem. I had to add the line to the top of the rendering order. This can be done in the following way in whatever method allows you to create a new node:
Pane line = new Pane();
//add whatever code binds your line to desired places
getChildren.add(0, line);
//add the other node as child

How to resize WorldWind Panel?

I want to put a WorldWindowGLJPanel into a Pane, and I want to make it resizable, but I can't, even when I call resize or setSize method.
Here's what I'm doing :
wwd = new WorldWindowGLJPanel();
wwd.setPreferredSize(new java.awt.Dimension(300, 300));
wwd.setModel(new BasicModel());
swingNode = new SwingNode();
swingNode.setContent(wwd);
wwdPane = new Pane();
wwdPane.getChildren().add(swingNode);
Then I use this wwdPane to display World Wind.
I want my world wind panel to have the size of the pane which contains it, and I want to make this world wind panel resizable.
I thought about give the size to my world wind panel of my pane with a setSize(PaneDimenson) and then bind the size of my worldwindpanel with my pane , but the setSize function doesn't work.
EDIT : I found an alternative solution by not using a pane, but directly the swingNode, the resize is now automatic. But if you want to use a pane there's still a problem, and you're force to use a group.
The setSize is working, try this code:
scene.widthProperty().addListener(new ChangeListener<Number>() {
#Override public void changed( ObservableValue<? extends Number> o, Number b, Number a ) {
Platform.runLater(new Runnable() {
public void run() {
wwd.setSize((int)(a.intValue()*0.5), wwd.getHeight());
}
});
or with Java8
scene.widthProperty().addListener((o,b,a)->Platform.runLater(()->
wwd.setSize((int)(a.intValue()*0.5), wwd.getHeight())));
But I couldn't make the resize work in my sample code, because the SwingNode somehow mess up the resizing, I think you should try the solution advised here.

JavaFX opposite function of .requestFocus()?

I'm looking for function in JavaFX, where I can "disable or dismiss" the requested focus.
Here is a screenshot of my program: Screenshot
Every cell is filled with an Eventhandler (onMouseEntered and onMouseExited) and in every onMouseEntered function I have to request the focus like this:
label.requestFocus(). I need to do that, because I'm using KeyEvents to change the content of the cell.
It works fine, but there is a bug: When I move out of the scrollPane, there is still the requested focus on the last entered cell.
How can I solve that issue, without requesting focus for everything around the Scrollpane to fix this bug? Is there a function, where I can dismiss the requested focus, so after exiting a cell, it'll dismiss the focus.
Best regards,
My code:
arbeitetLabel.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
arbeitetLabel.requestFocus();
arbeitetLabel.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent keyEvent) {
// Do some keyEvent Stuff
}
});
}
});
arbeitetLabel.setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
// Dismiss the requested Focus here?
}
});
I'm passing the focus to the parent, that's it!
arbeitetLabel.getParent().requestFocus()

How to implement collision detection on nodes which are in StackPane (in JavaFX)?

I am trying to check collision detection on the nodes which are inside StackPane. Below is my code:
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
Scene scene = new Scene(pane,300,300,Color.GREEN);
primaryStage.setScene(scene);
primaryStage.show();
Rectangle rect1 = new Rectangle(50, 50);
rect1.setFill(Color.BLUE);
Rectangle rect2 = new Rectangle(50, 50);
pane.getChildren().add(rect1);
pane.getChildren().add(rect2);
TranslateTransition translateTransitionEnemyCar = new TranslateTransition();
translateTransitionEnemyCar.setDuration(Duration.millis(2000));
translateTransitionEnemyCar.setNode(rect2);
translateTransitionEnemyCar.setFromY(-150);
translateTransitionEnemyCar.setToY(150);
translateTransitionEnemyCar.setAutoReverse(true);
translateTransitionEnemyCar.setCycleCount(Timeline.INDEFINITE);
translateTransitionEnemyCar.play();
checkCollision(pane,rect1,rect2);
}
//Collision Detection
void checkCollision(StackPane pane, final Rectangle rect1, Rectangle rect2){
rect2.boundsInParentProperty().addListener(new ChangeListener<Bounds>() {
#Override
public void changed(ObservableValue<? extends Bounds> arg0,Bounds oldValue, Bounds newValue) {
if(rect1.intersects(newValue)){
System.out.println("Collide ============= Collide");
}
}
});
}
}
Here the collision detection is working if I use AnchorPane. But with StackPane I am not able to achieve it. I guess it is because of the co-ordinate system of the stack pane (Correct me if I am wrong).
So please help me out to achieve collision detection of the above two rectangles. Also please provide some suggestion (if you know) to implement such collision detection on nodes inside StackPane if I want to change the the co-ordinate of the nodes.
In your test condition you compare the local bounds of one rectangle with the bounds in parent of the other rectangle. You want to compare the same bounds types.
So change:
rect1.intersects(newValue)
To:
rect1.getBoundsInParent().intersects(newValue)
To understand bounds types better, you might want to play with this intersection demo application.
Well in My experience, if creating an environment (collision space) a group should be used as opposed to a container..
I think in your situation the stackPane is the culprit.
plus I think your collision check should be more along the lines of this:
public void checkCollisions(){
if(rect1.getBoundsInParent().intersects(rect2.getBoundsInParent)){
Shape intersect = Shape.intersect(rect1, rect2);
if(intersect.getBoundsInLocal().getWidth() != -1){
// Collision handling here
System.out.println("Collision occured");
}
}
}
Or you could do something more along these lines: in a class that extends "Node"
public boolean collided(Node other) {
if(this.getBoundsInParent().intersects(other.getBoundsInParent())){
Shape intersect = Shape.intersect(this.getShape(), other.getShape());
if(intersect.getBoundsInLocal().getWidth() != -1){
System.out.println(this.getId() + " : " + other.getId());
return true;
}
}
return false;
}
public void checkCollisions(){
for(Node n : root.getChildren().filtered(new Predicate(){
#Override
public boolean test(Object t) {
//makes sure object does not collide with itself
return !t.equals(this);
// for shapes
// return t instanceof Shape;
}
}){
if(collided(n)){
// handle code here
}
}
}
well thats my 2 cents...
Another thing i just noticed is you are registering a new changeListener each time your check collision method is called without un-registering it.. that could lead to problems later imo...

Resources