title: javafx: Draw rectangle to canvas
body: <p>Hello again fellow programmer. </p>
  
  <p>I'm creating a program which draws rectangle to to the canvas and at the back of the canvas i have a imageview</p>
  
  <p>so basically the program i'm creating is a pdfviewer that can draw in the pdf. it uses a imageview for the viewing pdf page and canvas to draw the pdf</p>
  
  <p>to far it's working fine.</p>
  
  <p>i can draw the rectangle using the mouse. i can even zoom in and zoom out the pdf and the rectangle i draw is following its proper location. if the <code>imageview zoom is = 1</code></p>
  
  <p>but when i zoom in first to make the view larger, and then create a rectangle. and then i <code>zoom in</code> or <code>zoom out</code> the location of the rectangle will put to other location. </p>
  
  <p>so the problem is the location of the rectangle drawn is wrong. How can i do this? thank you</p>
  
  <p>i created a sample video and i uploaded on youtube. for clarification of my question</p>
  
  <p><a href="https://youtu.be/TpI08GJRJKE" rel="nofollow noreferrer">https://youtu.be/TpI08GJRJKE</a></p>
  
  <p>here is my code</p>
  
  <pre><code>float zoomValue =1f;
  
  
  protected void ItemClick() {
          try {
              tableView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -&gt; {
                  if (newSelection != null) {
                      MyPdfPages pdf = (MyPdfPages) tableView.getSelectionModel().getSelectedItem();
                      File file = new File(txtLocation.getText());
                      BufferedImage bi = pageToImage(file, pdf.getIntValue(), zoomValue);
                      Image img = SwingFXUtils.toFXImage(bi, null);
                      imageView.setFitWidth(img.getWidth());
                      imageView.setFitHeight(img.getHeight());
                      canvas.setHeight(img.getHeight());
                      canvas.setWidth(img.getWidth());
                      imageView.setImage(img);
                  }
              });
          } catch (Exception ex) {
              JOptionPane.showMessageDialog(null, ex.getStackTrace());
          }
  
  
  
  private BufferedImage pageToImage(File pdfFile, int pageNum, float scale) {
          PDDocument doc = null;
          BufferedImage bi = null;
          try {
              doc = PDDocument.load(pdfFile);
  
              PDFRenderer renderer = new PDFRenderer(doc);
              bi = renderer.renderImage(pageNum, scale, ImageType.RGB);
          } catch (Exception ex) {
              ex.printStackTrace();
          } finally {
              if (doc != null) try {
                  doc.close();
              } catch (Exception ex) {
              }
          }
          return bi;
      }
  
  @FXML protected  void ZoomInPdf(){
          try{
              MyPdfPages pdf = (MyPdfPages) tableView.getSelectionModel().getSelectedItem();
              if (zoomValue != 12.5) {
                  zoomValue = zoomValue + 0.50f;
                  System.out.println(zoomValue);
                  Zoom(txtLocation.getText(), pdf.getIntValue(), zoomValue);
              }
          }catch ( Exception ex){
              Alert alert = new Alert(Alert.AlertType.INFORMATION);
              alert.setContentText(ex.getStackTrace().toString());
              alert.showAndWait();
          }
      }
  
      @FXML protected void ZoomOutPdf(){
          try{
              MyPdfPages pdf = (MyPdfPages) tableView.getSelectionModel().getSelectedItem();
              if (zoomValue != 1) {
                  zoomValue = zoomValue - 0.50f;
                  System.out.println(zoomValue);
                  Zoom(txtLocation.getText(), pdf.getIntValue(), zoomValue);
              }
          }catch (Exception ex){
              Alert alert = new Alert(Alert.AlertType.INFORMATION);
              alert.setContentText(ex.getStackTrace().toString());
              alert.showAndWait();
          }
      }
  
      protected void Zoom(String fileName, Integer page, float zoom) {
          try {
              //MyPdfPages pdf = tableView.getSelectionModel().getSelectedItem();
              File file = new File(fileName);
  
              BufferedImage bi = pageToImage(file, page, zoom);
              Image img = SwingFXUtils.toFXImage(bi, null);
              imageView.setFitWidth(img.getWidth());
              imageView.setFitHeight(img.getHeight());
              canvas.setWidth(img.getWidth());
              canvas.setHeight(img.getHeight());
              imageView.setImage(img);
  
              g = canvas.getGraphicsContext2D();
              g.save();
              g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
              g.setStroke(Color.RED);
              g.strokeRect(pX1 * zoomValue, pY1 * zoomValue, (pX2 - pX1) * zoomValue, (pY2-pY1) * zoomValue);
              g.restore();
  
          } catch (Exception ex) {
              Alert al = new Alert(Alert.AlertType.INFORMATION);
              al.setContentText("Zoom Error: " + ex.getStackTrace().toString());
              al.showAndWait();
          }
      }
  
  
  @FXML
      protected void MousePressed(MouseEvent me) {
  
          isPressed = true;
  
  
          pX1 =  me.getX();
          pY1 = me.getY();
      }
  
      @FXML
      protected void MouseMove(MouseEvent me) {
  
          if (!isPressed) return;
  
          pX2 = me.getX();
          pY2 = me.getY();
  
          try {
  
  
              g = canvas.getGraphicsContext2D();
              g.save();
              g.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
              g.setStroke(Color.RED);
              if (zoomValue ==1){
                  g.strokeRect(pX1 , (pY1 ) , (pX2 - pX1) , (pY2-pY1)  );
              }else{
                  g.strokeRect((pX1 / zoomValue) * zoomValue, (pY1  / zoomValue) * zoomValue , ((pX2 - pX1) / zoomValue) * zoomValue , ((pY2-pY1)/ zoomValue) * zoomValue);
              }
  
              g.restore();
  
          } catch (Exception ex) {
              Alert al = new Alert(Alert.AlertType.INFORMATION);
              al.setContentText(ex.getStackTrace().toString());
              al.showAndWait();
          }
  
      }
  </code></pre>