Search in sources :

Example 11 with Rectangle2D

use of javafx.geometry.Rectangle2D in project Board-Instrumentation-Framework by intel.

the class ConfigurationReader method CalculateScaling.

private void CalculateScaling() {
    if (null == _Configuration) {
        return;
    }
    if (_Configuration.isAutoScale() && _Configuration.getCreationHeight() > 0 && _Configuration.getCreationWidth() > 0) {
        Rectangle2D visualBounds = _Configuration.getPrimaryScreen().getVisualBounds();
        double appWidth = visualBounds.getWidth();
        double appHeight = visualBounds.getHeight();
        // if app size specified
        if (getConfiguration().getWidth() > 0) {
            appWidth = getConfiguration().getWidth();
        }
        if (getConfiguration().getHeight() > 0) {
            appHeight = getConfiguration().getHeight();
        }
        double createWidth = (double) _Configuration.getCreationWidth();
        double createHeight = (double) _Configuration.getCreationHeight();
        double widthScale, heightScale, appScale;
        Font defFont = Font.getDefault();
        LOGGER.info("System Font info: " + defFont.toString());
        if (appWidth == createWidth && appHeight == createHeight) {
            LOGGER.info("Attempting to automatically scale, however current resolution is the same as specified <CreationSize>");
            return;
        }
        widthScale = appWidth / createWidth;
        heightScale = appHeight / createHeight;
        appScale = widthScale;
        if (// pick the smaller of the 2
        heightScale < widthScale) {
            appScale = heightScale;
        }
        _Configuration.setScaleFactor(appScale);
        String strCurr = " screen resolution: [" + Double.toString(appWidth) + "x" + Double.toString(appHeight) + "]";
        String strCreated = " created screen resolution: [" + Double.toString(createWidth) + "x" + Double.toString(createHeight) + "]";
        LOGGER.info("AutoScale set to " + Double.toString(appScale) + strCurr + strCreated);
    } else {
        _Configuration.setAutoScale(false);
    }
}
Also used : Rectangle2D(javafx.geometry.Rectangle2D) Font(javafx.scene.text.Font)

Example 12 with Rectangle2D

use of javafx.geometry.Rectangle2D in project tokentool by RPTools.

the class ImageUtil method autoCropImage.

/*
	 * Crop image to smallest width/height based on transparency
	 */
private static Image autoCropImage(Image imageSource) {
    ImageView croppedImageView = new ImageView(imageSource);
    PixelReader pixelReader = imageSource.getPixelReader();
    int imageWidth = (int) imageSource.getWidth();
    int imageHeight = (int) imageSource.getHeight();
    int minX = imageWidth, minY = imageHeight, maxX = 0, maxY = 0;
    // Find the first and last pixels that are not transparent to create a bounding viewport
    for (int readY = 0; readY < imageHeight; readY++) {
        for (int readX = 0; readX < imageWidth; readX++) {
            Color pixelColor = pixelReader.getColor(readX, readY);
            if (!pixelColor.equals(Color.TRANSPARENT)) {
                if (readX < minX)
                    minX = readX;
                if (readX > maxX)
                    maxX = readX;
                if (readY < minY)
                    minY = readY;
                if (readY > maxY)
                    maxY = readY;
            }
        }
    }
    if (maxX - minX <= 0 || maxY - minY <= 0)
        return new WritableImage(1, 1);
    // Create a viewport to clip the image using snapshot
    Rectangle2D viewPort = new Rectangle2D(minX, minY, maxX - minX, maxY - minY);
    SnapshotParameters parameter = new SnapshotParameters();
    parameter.setViewport(viewPort);
    parameter.setFill(Color.TRANSPARENT);
    return croppedImageView.snapshot(parameter, null);
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) SnapshotParameters(javafx.scene.SnapshotParameters) Color(javafx.scene.paint.Color) Rectangle2D(javafx.geometry.Rectangle2D) ImageView(javafx.scene.image.ImageView)

Example 13 with Rectangle2D

use of javafx.geometry.Rectangle2D in project tokentool by RPTools.

the class ImageUtil method composePreview.

public static Image composePreview(StackPane compositeTokenPane, Color bgColor, ImageView portraitImageView, ImageView maskImageView, ImageView overlayImageView, boolean useAsBase, boolean clipImage) {
    // Process layout as maskImage may have changed size if the overlay was changed
    compositeTokenPane.layout();
    SnapshotParameters parameter = new SnapshotParameters();
    Image finalImage = null;
    Group blend;
    if (clipImage) {
        // We need to clip the portrait image first then blend the overlay image over it
        // We will first get a snapshot of the portrait equal to the mask overlay image width/height
        double x, y, width, height;
        x = maskImageView.getParent().getLayoutX();
        y = maskImageView.getParent().getLayoutY();
        width = maskImageView.getFitWidth();
        height = maskImageView.getFitHeight();
        Rectangle2D viewPort = new Rectangle2D(x, y, width, height);
        Rectangle2D maskViewPort = new Rectangle2D(1, 1, width, height);
        WritableImage newImage = new WritableImage((int) width, (int) height);
        WritableImage newMaskImage = new WritableImage((int) width, (int) height);
        ImageView overlayCopyImageView = new ImageView();
        ImageView clippedImageView = new ImageView();
        parameter.setViewport(viewPort);
        parameter.setFill(bgColor);
        portraitImageView.snapshot(parameter, newImage);
        parameter.setViewport(maskViewPort);
        parameter.setFill(Color.TRANSPARENT);
        maskImageView.setVisible(true);
        maskImageView.snapshot(parameter, newMaskImage);
        maskImageView.setVisible(false);
        clippedImageView.setFitWidth(width);
        clippedImageView.setFitHeight(height);
        clippedImageView.setImage(clipImageWithMask(newImage, newMaskImage));
        // Our masked portrait image is now stored in clippedImageView, lets now blend the overlay image over it
        // We'll create a temporary group to hold our temporary ImageViews's and blend them and take a snapshot
        overlayCopyImageView.setImage(overlayImageView.getImage());
        overlayCopyImageView.setFitWidth(overlayImageView.getFitWidth());
        overlayCopyImageView.setFitHeight(overlayImageView.getFitHeight());
        overlayCopyImageView.setOpacity(overlayImageView.getOpacity());
        if (useAsBase) {
            blend = new Group(overlayCopyImageView, clippedImageView);
        } else {
            blend = new Group(clippedImageView, overlayCopyImageView);
        }
        // Last, we'll clean up any excess transparent edges by cropping it
        finalImage = autoCropImage(blend.snapshot(parameter, null));
    } else {
        parameter.setFill(Color.TRANSPARENT);
        finalImage = autoCropImage(compositeTokenPane.snapshot(parameter, null));
    }
    return finalImage;
}
Also used : Group(javafx.scene.Group) WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) Rectangle2D(javafx.geometry.Rectangle2D) ImageView(javafx.scene.image.ImageView) BufferedImage(java.awt.image.BufferedImage) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image)

Example 14 with Rectangle2D

use of javafx.geometry.Rectangle2D in project org.csstudio.display.builder by kasemir.

the class Rubberband method handleStop.

private void handleStop(final MouseEvent event) {
    if (!active)
        return;
    final Point2D in_parent = parent.sceneToLocal(event.getSceneX(), event.getSceneY());
    x1 = in_parent.getX();
    y1 = in_parent.getY();
    parent.getChildren().remove(rect);
    active = false;
    handler.handleSelectedRegion(new Rectangle2D(Math.min(x0, x1), Math.min(y0, y1), Math.abs(x1 - x0), Math.abs(y1 - y0)), event.isControlDown());
    event.consume();
}
Also used : Point2D(javafx.geometry.Point2D) Rectangle2D(javafx.geometry.Rectangle2D)

Example 15 with Rectangle2D

use of javafx.geometry.Rectangle2D in project org.csstudio.display.builder by kasemir.

the class SelectedWidgetUITracker method updateWidgetsFromTracker.

/**
 * Updates widgets to current tracker location and size
 */
private void updateWidgetsFromTracker(final Rectangle2D original, final Rectangle2D current) {
    if (updating)
        return;
    updating = true;
    try {
        group_handler.hide();
        final List<Rectangle2D> orig_position = widgets.stream().map(GeometryTools::getBounds).collect(Collectors.toList());
        // If there was only one widget, the tracker bounds represent
        // the desired widget location and size.
        // But tracker bounds can apply to one or more widgets, so need to
        // determine the change in tracker bounds, apply those to each widget.
        final double dx = current.getMinX() - original.getMinX();
        final double dy = current.getMinY() - original.getMinY();
        final double dw = current.getWidth() - original.getWidth();
        final double dh = current.getHeight() - original.getHeight();
        final int N = orig_position.size();
        // Use compound action if there's more than one widget
        final CompoundUndoableAction compound = N > 1 ? new CompoundUndoableAction(Messages.UpdateWidgetLocation) : null;
        for (int i = 0; i < N; ++i) {
            final Widget widget = widgets.get(i);
            final Rectangle2D orig = orig_position.get(i);
            final ChildrenProperty orig_parent_children = ChildrenProperty.getParentsChildren(widget);
            ChildrenProperty parent_children = group_handler.getActiveParentChildren();
            if (parent_children == null)
                parent_children = widget.getDisplayModel().runtimeChildren();
            if (orig_parent_children == parent_children) {
                // Slightly faster since parent stays the same
                if (!widget.propX().isUsingWidgetClass())
                    widget.propX().setValue((int) (orig.getMinX() + dx));
                if (!widget.propY().isUsingWidgetClass())
                    widget.propY().setValue((int) (orig.getMinY() + dy));
            } else {
                // Update to new parent
                if (widget.getDisplayModel().isClassModel()) {
                    logger.log(Level.WARNING, "Widget hierarchy is not permitted for class model");
                    return;
                }
                final Point2D old_offset = GeometryTools.getDisplayOffset(widget);
                orig_parent_children.removeChild(widget);
                parent_children.addChild(widget);
                final Point2D new_offset = GeometryTools.getDisplayOffset(widget);
                logger.log(Level.FINE, "{0} moves from {1} ({2}) to {3} ({4})", new Object[] { widget, orig_parent_children.getWidget(), old_offset, parent_children.getWidget(), new_offset });
                // Account for old and new display offset
                if (!widget.propX().isUsingWidgetClass())
                    widget.propX().setValue((int) (orig.getMinX() + dx + old_offset.getX() - new_offset.getX()));
                if (!widget.propY().isUsingWidgetClass())
                    widget.propY().setValue((int) (orig.getMinY() + dy + old_offset.getY() - new_offset.getY()));
            }
            if (!widget.propWidth().isUsingWidgetClass())
                widget.propWidth().setValue((int) Math.max(1, orig.getWidth() + dw));
            if (!widget.propHeight().isUsingWidgetClass())
                widget.propHeight().setValue((int) Math.max(1, orig.getHeight() + dh));
            final UndoableAction step = new UpdateWidgetLocationAction(widget, orig_parent_children, parent_children, (int) orig.getMinX(), (int) orig.getMinY(), (int) orig.getWidth(), (int) orig.getHeight());
            if (compound == null)
                undo.add(step);
            else
                compound.add(step);
        }
        if (compound != null)
            undo.add(compound);
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Failed to move/resize widgets", ex);
    } finally {
        updating = false;
        updateTrackerFromWidgets();
    }
}
Also used : ChildrenProperty(org.csstudio.display.builder.model.ChildrenProperty) Point2D(javafx.geometry.Point2D) CompoundUndoableAction(org.csstudio.display.builder.util.undo.CompoundUndoableAction) Rectangle2D(javafx.geometry.Rectangle2D) ActionButtonWidget(org.csstudio.display.builder.model.widgets.ActionButtonWidget) GroupWidget(org.csstudio.display.builder.model.widgets.GroupWidget) Widget(org.csstudio.display.builder.model.Widget) UndoableAction(org.csstudio.display.builder.util.undo.UndoableAction) CompoundUndoableAction(org.csstudio.display.builder.util.undo.CompoundUndoableAction) UpdateWidgetLocationAction(org.csstudio.display.builder.editor.undo.UpdateWidgetLocationAction)

Aggregations

Rectangle2D (javafx.geometry.Rectangle2D)76 Image (javafx.scene.image.Image)14 Scene (javafx.scene.Scene)10 Screen (javafx.stage.Screen)10 Stage (javafx.stage.Stage)7 Point2D (javafx.geometry.Point2D)6 IOException (java.io.IOException)5 ImageView (javafx.scene.image.ImageView)5 BufferedImage (java.awt.image.BufferedImage)4 File (java.io.File)4 List (java.util.List)4 Color (javafx.scene.paint.Color)4 Optional (java.util.Optional)3 Level (java.util.logging.Level)3 Application (javafx.application.Application)3 Platform (javafx.application.Platform)3 Menu (javafx.scene.control.Menu)3 MenuBar (javafx.scene.control.MenuBar)3 MenuItem (javafx.scene.control.MenuItem)3 Pane (javafx.scene.layout.Pane)3