I am creating a simple programme in javafx.
private void onClick(final Circle circle) {
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
circle.setTranslateX(150.);
}
});
}
in the "public void start" i match th created circle with the method "onClick"
onClick(circle1);
this code move a circle to the right. How can I move it multiple times? I tried to create more methods analogically "onClick1" but it always respond just to the first click. I need to move it to the right with each click again.
Thank you for your time.
What about
circle.setTranslateX(circle.getTranslateX() + 150.0);
Related
I am very new to Java, and I wanted to try to make a thing in BlueJ that requires BlueJ to know when the mouse is clicked, and to be able to determine the mouse's coordinates on the x,y plane.
In my class where I code, I have seen some imported class and things like Scanner and Graphics, so it might be something along those lines, but I am not sure.
I just mainly need
The thing to import (if it is a thing that needs to be imported)
How to make it tell if the mouse is clicked
How to make it be able to tell me the x, y position of the mouse when asked (like, what class method would I have to refer to to find this)
After I have that, I will work with that to try to make my program. Thank you!
EDIT: Upon request, here is my attempt
java.awt.event.MouseAdapter
public class main
{
MouseAdapter test = new MouseAdapter();
}
public void mouseMoved(test e)
{
System.out.println("hey your mouse moved");
}
I am clearly doing something horribly wrong
One way to achieve your goal would be to use java swing. The following code will print out a statement if the mouse is moved inside the created window:
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame jFrame = new JFrame();
jFrame.setSize(720,480);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrame.getContentPane().addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent mouseEvent) {
System.out.println("STUFF");
}
#Override
public void mouseMoved(MouseEvent mouseEvent) {
System.out.println("STUFF");
}
});
});
}
This is not an ideal solution but I hope it helps you to look in the right direciton.
I want to get the position of my mouse relative to the frame when I click a shortcut:
#Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.isShortcutDown()) {
if (keyEvent.getCode() == KeyCode.P) {
//Code Here
}
}
}
When the KeyEvent fires, just get the pointer location using MouseInfo.
#Override
public void handle(KeyEvent keyEvent) {
if (keyEvent.isShortcutDown()) {
if (keyEvent.getCode() == KeyCode.P) {
//Code Here
Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
}
}
If you want to continuously track the mouse location, you should use a MouseMoved listener.
Hey here from five years in the future. Since JavaFX still doesn't have this feature for some reason, my solution was to subtract the X/Y position of the cursor's position on the screen (java.awt.MouseInfo) from the X/Y position of the stage. As an example for Y:
MouseInfo.getPointerInfo().getLocation().getY() - stage.getY()
It's not perfect, you'll have to optimize it a bit, but it's an actual solution (the first guy who answered seems to think the screen equals the stage...)
I have a basic javafx program where a rectangle, simulating an elevator, must move up and down at the push of 'up' and 'down' buttons. I have successfully implemented the code to do this below:
public void handle(ActionEvent event) {
if (event.getSource() == upButton) {
//this should all be put into a 'slideNode' method
TranslateTransition translateTransition1 = new TranslateTransition(Duration.millis(500), theElevator);
translateTransition1.setByX(0);
translateTransition1.setByY(-50);
translateTransition1.setCycleCount(1);
translateTransition1.setAutoReverse(false);
translateTransition1.play();
}
}
The issue I need to solve is what happens when the elevator is partway through this motion and the button is pressed again - the elevator doesn't get the full motion it would have if I waited until it reached its first destination to press the button again!
I understand why this happens, but I'd like to know if there's a way to solve this. I imagine there should be some piece of the API similar to the following, which I can toss at the end of my code:
Pause pause = new Pause(Duration.millis(500));
pause.pause();
Does such a thing exist? How would you solve my problem?
You can disable the button while the TranslateTransition is playing:
public void handle(ActionEvent event) {
if (event.getSource() == upButton) {
//this should all be put into a 'slideNode' method
TranslateTransition translateTransition1 = new TranslateTransition(Duration.millis(500), theElevator);
translateTransition1.setByX(0);
translateTransition1.setByY(-50);
translateTransition1.setCycleCount(1);
translateTransition1.setAutoReverse(false);
translateTransition.statusProperty().addListener((obs, oldStatus, newStatus) ->
button.setDisable(newStatus==Animation.Status.RUNNING));
translateTransition1.play();
}
}
Been trying around and searching but couldn't find any solution, so I finally decided to give up and ask further...
Creating a javafx app, I load tiles in a TilePane.
This tiles are clickable and lead to a details page of their respective content.
On each tile, if they do belong to a certain pack, I do display the pack name, that is also clickable and lead to a page showing that specific pack content.
So that means the container, the tile, that is a Pane is clickable and on top of it I have a Label that is claickable also. What happens is when I do click the Label, it also triggers the Pane onMousePressed()... Here is a part of the tile creation code, the part focused on the onMousePressed(). I tried to make the Pane react by double click and the Label by single, it works, but I want to Pane to open with a single click.
I would be more than thankfull for any ideas how to solve that.
public DownloadTile (Downloadable upload, MainApp mainApp) {
_mainApp = mainApp;
_upload = upload;
_tile = new Pane();
_tile.setPrefHeight(100);
_tile.setPrefWidth(296);
_tile.setStyle("-fx-background-color: #ffffff;");
_tile.setCursor(Cursor.HAND);
}
public void refresh() {
_tile.getChildren().clear();
_tile.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.isPrimaryButtonDown() /*&& event.getClickCount() == 2*/) {
_mainApp.showDownloadDialog(dt, _upload);
}
}
});
if (_upload.getPack() != null) {
Label pack = new Label();
pack.setText(_upload.getPack());
pack.getStyleClass().add("pack-link");
pack.setCursor(Cursor.HAND);
pack.relocate(10, 48);
_tile.getChildren().add(pack);
pack.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.isPrimaryButtonDown()) {
_mainApp.showPackPage(_upload);
}
}
});
}
}
Your label will receive the mouseclick first (since it's on top), so after you have processed the click, you can stop it from being passed down the chain using 'consume':
pane.setOnMouseClicked(
(Event event) -> {
// process your click here
System.out.println("Panel clicked");
pane.requestFocus();
event.consume();
};
I have a legacy swing application that I need to add touch gestures to,specifically pinch to zoom and touch and drag.
I tried the SwingNode of JDK 8 and I can run the swing application there, but the display performance was cut by more than 50% which won't work. SwingTextureRenderer in MT4J has the same issue and that is without even trying to redispatch touch events as mouse events.
I thought about a glass pane approach using a JavaFX layer on top and capturing the touch events and attempting to dispatch them as mouse events to the Swing app underneath.
Does anyone have an alternative approach? The target platform is windows 8.
Bounty coming as soon as Stackoverflow opens it up. I need this one pretty rapidly.
EDIT:
Here is what I tried with SwingNode (the mouse redispatch didn't work). The SwingNode stuff might be a distraction from the best solution so ignore this if you have a better idea for getting touch into swing:
#Override
public void start(Stage stage) {
final SwingNode swingNode = new SwingNode();
createAndSetSwingContent(swingNode);
StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setScene(new Scene(pane, 640, 480));
stage.show();
}
private void createAndSetSwingContent(final SwingNode swingNode) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
UILib.setPlatformLookAndFeel();
// create GraphViewTouch
String datafile = null;
String label = "label";
final JPanel frame = GraphView.demoFrameless(datafile, label);
swingNode.setContent(frame);
swingNode.setOnZoom(new EventHandler<ZoomEvent>() {
#Override public void handle(ZoomEvent event) {
MouseWheelEvent me = new MouseWheelEvent(frame, 1, System.currentTimeMillis(), 0, (int)Math.round(event.getSceneX()), (int)Math.round(event.getSceneY()), (int)Math.round(event.getScreenX()), (int)Math.round(event.getScreenY()), (int)Math.round(event.getZoomFactor()), false, MouseWheelEvent.WHEEL_UNIT_SCROLL, (int)Math.round(event.getZoomFactor()), (int)Math.round(event.getZoomFactor()), event.getZoomFactor());
frame.dispatchEvent(me);
System.out.println("GraphView: Zoom event" +
", inertia: " + event.isInertia() +
", direct: " + event.isDirect());
event.consume();
}
});
}
});
}
You can use JNA to parse the messages from Windows.
Some documentation on the multi touch events from Microsoft:
https://learn.microsoft.com/en-us/windows/desktop/wintouch/wm-touchdown
Some documentation on how to do it in Java and sample code:
https://github.com/fmsbeekmans/jest/wiki/Native-Multitouch-for-AWT-component-(Windows)
hmm... this is a tough one, but there is a chance.
What I would do is make multiple MouseListener classes (relative to the number of mouse events you want to pick up), and than create some sort of system to detect certain adjustments, eg. (zoom)
1st listener:
public void mousePressed(MouseEvent e){
//Set click to true
clk = true;
//set first mouse position
firstPos = window.getMousePosition();
}
public void mouseReleased(MouseEvent e){
//set second mouse position
secondPos = window.getMousePosition();
}
Second Listener
public void mousePressed(MouseEvent e){
//set clicked to true
clk = true;
//set first mouse position (listener 2)
firstPos = window.getMousePosition();
}
public void mouseReleased(MouseEvent e){
//set second mouse position
secondPos = window.getMousePosition();
}
Main handler
if(Listener1.get1stMousePos() < Listener1.get2ndMousePos() && Listener2.get1stMousePos() < Listener2.get2ndMousePos() && Listener1.clk && Listener2.clk){
zoomMethod((Listener1.get1stMousePos - Listener1.get2ndMousePos()) + (Listener1.get1stListener2.get2ndMousePos());
}
And than just do this to add it to the window:
window.addMouseListener(Listener1);
window.addMouseListener(Listener2);
Hope you find a way.
For the pinch-to-zoom:
Try the GestureMagnificationListener by guigarage.com. It provides a method:
public void magnify(GestureMagnificationEvent me){...}