简体   繁体   中英

JavaFX Scene Builder on Eclipse , ImageView

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).

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 (eg "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 ....
       }
}

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);  
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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