title: Javafx Image Crop according to hovering mouse position
body: <p>I am developing a tracing program by placing dots to anatomical points on Xray. My problem is, I'm trying to zoom into my X-ray image (<code>Imageview</code>) while my mouse hovering onto it and displaying in another <code>Imageview</code>. I want my mouse to create a virtual rectangle around it and stays at the center while moving and transfers that live image to other small <code>Imageview</code>, I've done this so far but there are some glitches. My rectangle does not follow my mouse exactly as it should, and the glitch accelerates when I move into corners, What can you suggest? I'm sharing my error and my codes to clarify all those things.</p>
  <p>Maybe I should create a rectangle shape and place it around mouse position and crop the image live, fit into the <code>zoomedImageview</code>, and display all the time, but I am stuck in this.</p>
  <p>The main problem is, mouse position and center of <code>zoomedImageview</code>'s center position has to coincide all the time.</p>
  <p><a href="https://i.stack.imgur.com/2Swn2m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Swn2m.png" alt="enter image description here" /></a></p>
  <p>Xray Imageview Fitwidth-height is 800x800
  zoomedImageview  Fitwidth-height is 250x250</p>
  <pre><code>public class DrawingController {
  
          @FXML 
      ImageView xrayImage;
          
           @FXML
      ImageView zoomedImage;
  
     public void initialize()
      {
  
     xrayImage.setOnMouseMoved(e -&gt; {    //HOVERING ON XRAY
     
  PixelReader reader = XrayImageview.getImage().getPixelReader();   
  WritableImage newImage = new WritableImage(reader,
  
  (int) e.getX(),  //x and y is picked according to hovering mouse position
     (int) e.getY(),
     (int) 250,     // 250x250 rectangle is requested.
     (int) 250);
  
  zoomedImage.setImage(newImage);  // Displaying zoomed image
  
          });
  }}
  </code></pre>
  <p>And My FXML file is:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import javafx.scene.image.Image?&gt;
  &lt;?import javafx.scene.image.ImageView?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  
  &lt;AnchorPane prefHeight=&quot;913.0&quot; prefWidth=&quot;1238.0&quot; xmlns=&quot;http://javafx.com/javafx/16&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;main.java.controllers.DrawingController&quot;&gt;
     &lt;children&gt;
        &lt;ImageView fitHeight=&quot;800.0&quot; fitWidth=&quot;800.0&quot; layoutX=&quot;418.0&quot; layoutY=&quot;33.0&quot; pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot; fx:id=&quot;xrayImage&quot;&gt;
           &lt;image&gt;
              &lt;Image url=&quot;@threatCare.png&quot; /&gt;
           &lt;/image&gt;
        &lt;/ImageView&gt;
        &lt;ImageView fx:id=&quot;zoomedImage&quot; fitHeight=&quot;250.0&quot; fitWidth=&quot;250.0&quot; layoutX=&quot;108.0&quot; layoutY=&quot;433.0&quot; pickOnBounds=&quot;true&quot; preserveRatio=&quot;true&quot; /&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  <p>And the Main class :</p>
  <pre><code>package main.java;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.layout.AnchorPane;
  import javafx.stage.Stage;
  import main.java.database.DatabaseCreator;
  
  import javax.swing.text.html.ImageView;
  import java.awt.*;
  
  public class Runner extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception{
       
          Parent root = FXMLLoader.load(getClass().getResource(&quot;../resources/DrawingPanel.fxml&quot;));
  
          primaryStage.setTitle(&quot;TreatCare Software Systems&quot;);
           Scene scene = new Scene(root,1200,950);
          primaryStage.setScene(scene);
          primaryStage.setMinWidth(1200);
          primaryStage.setMinHeight(950);
          primaryStage.setMaximized(true);
          primaryStage.setOnCloseRequest( e -&gt; SystemClose());
          primaryStage.show();
      }
  
      private void SystemClose(){
          System.exit(0);         
      }
  
      public static void main(String[] args){
          launch(args);
      }
  }
  </code></pre>