javafx:绘制矩形到画布

时间:2016-11-14 03:55:40

标签: java pdf canvas javafx pdfbox

你好,同事程序员。

我正在创建一个程序,它将矩形绘制到画布上,在画布的后面我有一个imageview

所以基本上我创建的程序是一个可以用pdf绘制的pdfviewer。它使用imageview查看pdf页面和画布来绘制pdf

到目前为止,它的工作正常。

我可以使用鼠标绘制矩形。我甚至可以放大和缩小PDF格式,我绘制的矩形跟随其正确的位置。如果imageview zoom is = 1

但是当我首先放大以使视图变大,然后创建一个矩形。然后我zoom inzoom out矩形的位置将放到其他位置。

所以问题是绘制的矩形的位置是错误的。我怎样才能做到这一点?谢谢

我创建了一个示例视频,我在youtube上上传。澄清我的问题

https://youtu.be/TpI08GJRJKE

这是我的代码

float zoomValue =1f;


protected void ItemClick() {
        try {
            tableView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
                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();
        }

    }

0 个答案:

没有答案