简体   繁体   English

按下键时JavaFX imageView旋转

[英]Javafx imageView rotation when key pressed

Basically, I want to rotate an ImageView by 90 degrees when the user hits R on the keyboard. 基本上,我想当用户在键盘上按R时将ImageView旋转90度。

@FXML
private Image arrow;
@FXML
private ImageView arrowDirection;


@FXML
public void arrowRotation(KeyEvent keyEvent)
{
    if(keyEvent.getCode() == KeyCode.R)
    arrowDirection.setRotate(90);
}

Your question is a bit unspecific, so I assumed that you want an EventHandler that is added to the pane in which the image view rests, and that you want to set it in the Controller. 您的问题有点不确定,因此我假设您想要将EventHandler添加到图像视图所在的窗格中,并希望在Controller中进行设置。 This is my solution: 这是我的解决方案:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;

public class FXMLDocumentController implements Initializable {

    @FXML
    Pane pane;

    @FXML
    ImageView imgView;

    public void arrowRotation(KeyEvent event){
        if(event.getCode().equals(KeyCode.R))
            imgView.setRotate(90);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Javafx-layout-classes.svg/1000px-Javafx-layout-classes.svg.png");
        imgView.setImage(img);
        pane.sceneProperty().addListener(new ChangeListener<Scene>() {
            @Override
            public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {
                if(newValue != null){
                    pane.requestFocus();
                }
            }
        });
        pane.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent event) {
                arrowRotation(event);
            }
        });

    }    

}

Of course, this needs an FXML that holds an ImageView inside of a Pane, but that can easily be adjusted to fit your needs. 当然,这需要一个FXML,它可以将ImageView保留在窗格中,但是可以很容易地进行调整以满足您的需求。 The function that performs the rotation works pretty much like you thought, but I would compare using equals whenever possible. 执行旋转的功能的工作原理与您想像的差不多,但是我会尽可能地使用equals进行比较。 I added a listener to the sceneProperty of the pane to let the pane request the focus once the whole thing is loaded and the scene is set. 我在窗格的sceneProperty中添加了一个侦听器,以使窗格在装入整个对象并设置了场景后请求焦点。 This means that pressing a key will actually trigger KeyEventHandlers that are registered on the pane. 这意味着按下一个键实际上会触发在窗格中注册的KeyEventHandlers。 Registering the EventHandler is straight forward again. 再次注册EventHandler很简单。 Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM