简体   繁体   中英

JavaFx8 VBox center image

I have a JavaFx VBox inside of a ScrollPane:

VBox container = new VBox();
container.setAlignment(Pos.CENTER);

...

scrollPane.setContent(container);
scrollPane.setFitToWidth(true);
scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
scrollPane.setMinWidth(150);
scrollPane.setPannable(true);

the size of this VBox never change, inside i have some labels, one label has an user image like the next image(A)

the image is resized to some Height, but i don't know the size of this image, so if the width of the image is bigger than width of the VBox, this happen(part of the image hidden)(B)

but i don't want this, i want something like the following image:(the sides of the image hidden if the image width is bigger than the VBox width)(C)

http://i.stack.imgur.com/B3DOK.png

How i can do this?

I tried to put a rectangle as clip, in this rectangle i wanna show the center of the image, but the same happens.

imageView.setClip(new Rectangle(centerX - recSize, centerY - recSize, recSize*2, recSize*2));

---------------with the clip----------------

red = original image

blue = part of the image that is visible

http://i.stack.imgur.com/mYbyF.png

Nice(D)

Not nice:(E)(labels not centered correctly because of the image.)

Sorry by the links, i can't put the images directly

Solution

Set a viewport on the image not a clip.

imageView.setViewport(
     new Rectangle2D(500, 320, 420, 300)
);

Sample

Here is a sample. It's not going to exactly match what you are asking for because even with the linked images in your question, I can't quite understand what you are trying to do. But I think it should give you enough background info that you can learn to accomplish what you want.

The sample creates an image view as a graphic in a scroll pane. The image view applies a viewport to the image and scales the viewport with preserved ratio. This allows a scaled portion of the much larger image to be displayed. It's kind of like a thumbnail clip (click on the thumbnail to display the full image).

家

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.*;

// display a captioned image in a viewport.
// click the image to get an expanded view.
public class LabelWithImage extends Application {
    private static final double IMAGE_WIDTH = 150;

    @Override
    public void start(Stage stage) {
        Image image = new Image(IMAGE_LOC);
        ImageView imageView = new ImageView(
                image
        );
        imageView.setViewport(
                new Rectangle2D(500, 320, 420, 300)
        );
        imageView.setFitWidth(IMAGE_WIDTH);
        imageView.setPreserveRatio(true);

        Label labeledImage = createCaptionedImage(
                imageView,
                "Village Home"
        );

        addGlowOnMouseOver(labeledImage);
        labeledImage.setOnMouseClicked(event -> {
            displayFullImage(stage, image);
        });

        VBox vbox = new VBox( // vbox just there to mimic question askers structure.
                labeledImage
        );
        vbox.setPadding(new Insets(10));

        ScrollPane scrollPane = makeScrollable(vbox);

        Scene scene = new Scene(
                scrollPane
        );

        stage.setScene(scene);
        stage.show();

        stage.setMaxWidth(stage.getWidth());
        stage.setMaxHeight(stage.getHeight());
    }

    private void displayFullImage(Stage stage, Image image) {
        Stage displayStage = new Stage();

        displayStage.initStyle(StageStyle.UTILITY);
        displayStage.initModality(Modality.APPLICATION_MODAL);
        displayStage.initOwner(stage);
        displayStage.setScene(
                new Scene(
                        new Group(
                                new ImageView(
                                        image
                                )
                        )
                )
        );
        displayStage.show();
    }

    private void addGlowOnMouseOver(Node node) {
        Glow glow = new Glow();
        DropShadow shadow = new DropShadow(20, Color.GOLD);
        glow.setInput(shadow);

        node.setOnMousePressed(event -> node.setEffect(null));
        node.setOnMouseEntered(event -> node.setEffect(glow));
        node.setOnMouseExited(event -> node.setEffect(null));
    }

    private ScrollPane makeScrollable(Node node) {
        ScrollPane scrollPane = new ScrollPane(node);

        scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scrollPane.setPannable(true);

        return scrollPane;
    }

    private Label createCaptionedImage(ImageView imageView, String caption) {
        Label labeledImage = new Label(caption);

        labeledImage.setFont(Font.font("Athelas", FontPosture.ITALIC, 20));
        labeledImage.setStyle("-fx-background-color: cornsilk");
        labeledImage.setPadding(new Insets(0, 0, 5, 0));
        labeledImage.setGraphic(
                imageView
        );
        labeledImage.setContentDisplay(ContentDisplay.TOP);

        return labeledImage;
    }

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

    private static final String IMAGE_LOC =
            "http://www.imgion.com/images/01/beautiful-village-home.jpg";
            // image courtesy of http://www.imgion.com which provides
            // "free images on large topics to share with your friends and on your blogs."
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM