01/10/2018, 14:16
Clip View trong JavaFX
Chào các bạn,
Mình chia sẻ 1 cách để có view theo các hình dạng khác nhau trong javafx. Thực ra đó là 1 câu hỏi mình hỏi anh @JOEY ở 1 topic không mấy liên quan. Mình share ở đây để có newbies nào cần thì dễ tìm kiếm.
Nó khác là đơn giản:
package zoom;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
*
* @author O.J. Sousa Rodrigues (refactored by thanhpv)
*/
public class Zoom extends Application {
private ImageView imageView = new ImageView();
private Pane zoomPane = new Pane();
private StackPane container = new StackPane();
private VBox root = new VBox();
final DoubleProperty zoomProperty = new SimpleDoubleProperty(200);
@Override
public void start(Stage stage) throws Exception {
zoomProperty.addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
imageView.setFitWidth(zoomProperty.get() * 6);
imageView.setFitHeight(zoomProperty.get() * 6);
}
});
zoomPane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
@Override
public void handle(ScrollEvent event) {
if (event.getDeltaY() > 0) {
zoomProperty.set(zoomProperty.get() * 1.1);
} else if (event.getDeltaY() < 0) {
zoomProperty.set(zoomProperty.get() / 1.1);
}
}
});
imageView.setImage(new Image("https://daynhauhoc.s3-ap-southeast-1.amazonaws.com/original/3X/f/7/f717dee3224f89a8af4ead790312f117ca72e99b.jpg"));
imageView.preserveRatioProperty().set(true);
// create an HBox button
Button noClip = new Button("No Clip");
noClip.setOnAction(e -> {
container.setClip(null);
});
Button polygon = new Button("Polygon Clip");
polygon.setOnAction(e -> {
Polygon pol = new Polygon(135, 48, 275, 360, 415, 480, 365, 270, 550, 180, 335, 180, 275, 0, 215, 180, 5, 180, 185, 270);
container.setClip(pol);
});
Button rectangle = new Button("Rectangle Clip");
rectangle.setOnAction(e -> {
Rectangle recClip = new Rectangle(50, 50, 600, 400);
container.setClip(recClip);
});
Button circle = new Button("Circle Clip");
circle.setOnAction(e -> {
Circle circClip = new Circle(350, 350, 300);
container.setClip(circClip);
});
HBox buttons = new HBox(10, noClip, polygon, rectangle, circle);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(new Insets(10));
zoomPane.getChildren().add(imageView);
container.getChildren().add(zoomPane);
root.getChildren().addAll(buttons, container);
stage.setScene(new Scene(root, 400, 300));
stage.show();
}
}
Nó có thể tạo các view đa dạng thông qua shape trong javafx
Hy vọng giúp đc ai đó chung cảnh ngộ:grinning:
Bài liên quan