JavaFX draw pixels with PixelWriter results in gaps - java

I am drawing onto an ImageView in such way:
#FXML
void imgVDrag(MouseEvent event) { //On Mouse Dragged
pixelWriter.setColor((int)event.getX(),(int)event.getY(), colPick.getValue());
}
If i move the mouse too fast it results in gaps between the pixels. Do i have to use another event or is there any other solution?

Related

How do I implement drag and drop on a canvas?

More specifically, how would I go about implementing a drag and drop feature so that the image file dragged on to the canvas would be drawn on the canvas? I've tried using a VBox listener on top of the canvas, but that didn't work. The source code of by program is available here.
In my controllers initialize() function, I have the following code. canvas is passed from the FXML file via the #FXML annotation:
public void initialize() {
GraphicsContext g = canvas.getGraphicsContext2D();
// Setter for brush type
setBrushBrush();
// Get screen dimensions and set the canvas accordingly
Dimension screenSize = getScreenSize();
double screenWidth = screenSize.getWidth();
double screenHeight = screenSize.getHeight();
canvas.setHeight(screenHeight/1.5);
canvas.setWidth(screenWidth/1.5);
canvas.setOnMouseDragged(e -> {
//Drawing code here
});
canvas.setOnDragOver(e -> {
// Need to read data of dragged image
});
canvas.setOnMouseDragReleased(e -> {
// Need to put dragged data on to canvas
});
}
The mouseDragReleased event is the wrong event to listen for here. That event is triggered when the mouse is released during a "full press-drag-release gesture" within the application; not when data is dropped during a "platform-supported drag-and-drop gesture" (see the documentation for MouseEvent for a description of these different dragging modes). So instead of canvas.setOnMouseDragReleased(...), you need:
canvas.setOnDragDropped(e -> {
// ...
});
Assuming the implementations of the handlers are correct, this should enable you to drop an image from a file and draw it on the canvas.

Java, If a draggable object touches other object

I have a code for dragging a label width mouse.
lbl_banner.addMouseListener(new MouseAdapter()
{
#Override
public void mousePressed(MouseEvent e) {
//catching the current values for x,y coordinates on screen
x_pressed = e.getX();
y_pressed = e.getY();
}
});
lbl_banner.addMouseMotionListener(new MouseMotionAdapter(){
#Override
public void mouseDragged(MouseEvent e){
//and when the Jlabel is dragged
setLocation(e.getXOnScreen() - x_pressed, e.getYOnScreen() - y_pressed);
}
});
Now, how do I make a function: While I'm dragging this label around the Screen, If the Label by dragging touches other object (label, button,...) to do something.
if(//labelTouchesSomething){//do something}
While this is not technically a dragging but the dynamic move of a component (dragging is the transfer of contents in between components), you can compute the intersection of the current moving component against other components (this may need some navigation inside your hierarchy). May be this can help you: How do I detect the collison of components?. You can also use the methods contains of Component to determine if some coordinates are inside a component or not.

javafx - when I drag my stage too fast, mouse "anchor" change in the stage and the dragging is not correct

I designed an undecorated stage and I want to move it around the screen clicking in the stage and dragging it.
My controller handles two event: mousePressendHandler and mouseDraggedHandler.
The first event is fired when the user click with the mouse on the stage. In this method I store the coordinates relatives to the stage of the mouse pointer.
The second event is fired when the user moves the mouse without relasing the button. Than I move the stage subtracting the coordinates of the mouse pointer in the screen and setting the result to the stage.
Examples:
#FXML
public void mousePressedHandler(MouseEvent me) {
dragAnchorX = me.getScreenX() - stage.getX();
dragAnchorY = me.getScreenY() - stage.getY();
}
#FXML
public void mouseDraggedHandler(MouseEvent me) {
double stageX = me.getScreenX() - dragAnchorX;
double stageY = me.getScreenY() - dragAnchorY;
stage.setX(stageX);
stage.setY(stageY);
}
I want to avoid the stage to go out of the screen, so I modified the second method:
#FXML
public void mouseDraggedHandler(MouseEvent me) {
double stageX = me.getScreenX() - dragAnchorX;
if (stageX > 0) {
stage.setX(stageX);
}
double stageY = me.getScreenY() - dragAnchorY;
if (stageY > 0) {
stage.setY(stageY);
}
}
But if I move the mouse too fast, the mouse pointer is moved in the scene and so the result is not correct.
where am I wrong?

How to reduce the click area of an Button?

I have a gridview that is filled with buttons that looks like this:
right now if I click anywhere on the button, it will be pressed. But I want to the button to only be pressed when it's clicked in the light gray area, not the dark gray or white area on the top and left sides. I tried using setPadding(int,int,int,int) and setPaddingRelative(int,int,int,int) but neither of them makes any effect. How do I fix this?
Edit: the button is 50x50 and is already extended from the Button Class
Make a custom Button view inherit from Button class and override onDraw method with custom code to do that.
You can override touch methods to detect when button is pressed and get the x and y coords to remove cases when button must not be pressed.
Override onTouchEvent in your custom button:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (isInGrayArea(event.getX(), event.getY())) {
return super.onTouchEvent(event);
}
return false;
}
event.getX() and event.getY() is pixels coordinates, you will need to convert them into dp, I suppose.

JavaFX: how to design a chess table with event handler on each square

How can I draw a chess table in java?
I thought using TilePane would be ok.
I also managed to put squares inside the TilePane.
But when one of the squares is clicked it should change color. (i.e. from black to red or vice versa). In this example I used Circle instead of Rectangle:
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println("Mouse Clicked!"));
}
});
shows a message, but I cannot change any properties of the circle
Assuming that you have declared #FXML Circle circle; in your fxml controller and that your circle has the appropriate fx:id, you can use the following to change the color of the circle. Make sure you have the proper import of javafx.scene.paint.Color.
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (circle.getFill() == Color.RED)
circle.setFill(Color.BLACK);
else
circle.setFill(Color.RED);
System.out.println("Mouse Clicked!");
}
});

Categories

Resources