1

我正在尝试使用jfxtras-labs 2.2中的放大镜来查看图像的更多细节。同时,我想在这张图片上画一个矩形。问题是,放大镜会阻止所有其他事件。

这是我的示例代码:

package magnifiertest;

import java.io.File;

import javax.imageio.ImageIO;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import jfxtras.labs.scene.control.Magnifier;

public class TestMagnifier extends Application {
Group groupForRectangles;
ImageView pageImageView;
private boolean new_rectangle_is_being_drawn;
private Rectangle new_rectangle;
private double starting_point_x;
private double starting_point_y;

public static void main(String[] args) {
    launch();

}

@Override
public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();

    groupForRectangles = new Group();

    groupForRectangles = new Group();
    pageImageView = new ImageView();
    pageImageView.setPreserveRatio(true);
    pageImageView.setFitHeight(800);
    pageImageView.setFitWidth(600);

    pageImageView.setImage(SwingFXUtils.toFXImage(ImageIO.read(new File("/sample/Penguins.jpg")), null));
    groupForRectangles.setOnMouseDragged(event -> {
        setOnMouseDragged(event);
    });
    groupForRectangles.setOnMousePressed(event -> {
        setOnMousePressed(event);
    });
    groupForRectangles.setOnMouseReleased(event -> {
        setOnMouseReleased(event);
    });
    //If you outcomment the following lines, the rectangle drawing works
    Magnifier m = new Magnifier(pageImageView);
    groupForRectangles.getChildren().add(pageImageView);
    groupForRectangles.getChildren().add(m);
    root.getChildren().add(groupForRectangles);
    primaryStage.setScene(new Scene(root, 800, 600));
    primaryStage.show();
}

public void setOnMousePressed(MouseEvent event) {
    System.out.println("mouse pressed");

    if (new_rectangle_is_being_drawn == false) {

        starting_point_x = event.getX();
        starting_point_y = event.getY();
         System.out.println(starting_point_x + " ; " + starting_point_y);
        groupForRectangles.getChildren().remove(new_rectangle);
        new_rectangle = new Rectangle();


        new_rectangle.setFill(Color.TRANSPARENT); 
        new_rectangle.setStroke(Color.BLACK);
        groupForRectangles.getChildren().add(new_rectangle);

        new_rectangle_is_being_drawn = true;
    }
}

public void setOnMouseDragged(MouseEvent event) {
    if (new_rectangle_is_being_drawn == true) {
        double current_ending_point_x = event.getX();// - sub.getLayoutX();
        double current_ending_point_y = event.getY();// - sub.getLayoutY();

        adjust_rectangle_properties(starting_point_x, starting_point_y, current_ending_point_x,
                current_ending_point_y, new_rectangle);
    }
}

public void setOnMouseReleased(MouseEvent event) {
    if (new_rectangle_is_being_drawn == true) {
        new_rectangle.setFill(Color.TRANSPARENT);

        new_rectangle_is_being_drawn = false;
    }

}

void adjust_rectangle_properties(double starting_point_x, double starting_point_y, double ending_point_x,
        double ending_point_y, Rectangle given_rectangle) {

    given_rectangle.setX(starting_point_x);
    given_rectangle.setY(starting_point_y);
    given_rectangle.setWidth((ending_point_x - starting_point_x));
    given_rectangle.setHeight((ending_point_y - starting_point_y));

    if (given_rectangle.getWidth() < 0) {
        given_rectangle.setWidth(-given_rectangle.getWidth());
        given_rectangle.setX(given_rectangle.getX() - given_rectangle.getWidth());
    }

    if (given_rectangle.getHeight() < 0) {
        given_rectangle.setHeight(-given_rectangle.getHeight());
        given_rectangle.setY(given_rectangle.getY() - 

    given_rectangle.getHeight());
        }
    }

}

谢谢。

4

0 回答 0