Reputation: 56
I want to add an event in an ImageView
that I create with JavaFx Scene Builder.
When I click in the ImageView
I can do something (I'm working with OpenCV I want it so that when I click I can catch X and Y).
Upvotes: 1
Views: 662
Reputation: 2164
What do you want to do? if you just want to add an event when you click on the imageview: -->
in the "code" section in the SceneBuilder fill in a fx:id (e.g. "myImageView")
in your Controller just add this:
@FXML
private ImageView myImageView;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
myImageView.setOnMouseClicked(event -> {
//Whatever you want to do ....
}
}
Upvotes: 1
Reputation: 367
Try This Demo...
public class RotateImage extends JPanel{
private static final long serialVersionUID = 1L;
// Declare an Image object for us to use.
Image image;
// Create a constructor method
public RotateImage(){
super();
// Load an image to play with.
image = Toolkit.getDefaultToolkit().getImage("**Set Your Image Path Here**");
}
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g; // Create a Java2D version of g.
g2d.translate(220, 90); // Translate the center of our coordinates.
g2d.rotate(1); // Rotate the image by 1 radian.
g2d.drawImage(image, 200, 200, 200, 200, this);
}
public static void main(String arg[]){
JFrame frame = new JFrame("RotateImage");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
RotateImage panel = new RotateImage();
frame.setContentPane(panel);
frame.setVisible(true);
}
}
Upvotes: 0