Java Code Examples for javafx.scene.image.Image#getHeight()

The following examples show how to use javafx.scene.image.Image#getHeight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageUtil.java    From Augendiagnose with GNU General Public License v2.0 7 votes vote down vote up
/**
 * Resize an image to the given size.
 *
 * @param baseImage The original image.
 * @param targetSize The target size.
 * @param allowGrowing flag indicating if the image is allowed to grow.
 * @return the resized image.
 */
public static Image resizeImage(final Image baseImage, final int targetSize, final boolean allowGrowing) {
	if (baseImage == null || baseImage.getWidth() == 0 || baseImage.getHeight() == 0) {
		return baseImage;
	}
	if (baseImage.getWidth() <= targetSize && baseImage.getHeight() <= targetSize && !allowGrowing) {
		return baseImage;
	}
	int targetWidth;
	int targetHeight;
	if (baseImage.getWidth() > baseImage.getHeight()) {
		targetWidth = targetSize;
		targetHeight = (int) (targetSize * baseImage.getHeight() / baseImage.getWidth());
	}
	else {
		targetWidth = (int) (targetSize * baseImage.getWidth() / baseImage.getHeight());
		targetHeight = targetSize;
	}

	Canvas canvas = new Canvas(targetWidth, targetHeight);
	canvas.getGraphicsContext2D().drawImage(baseImage, 0, 0, targetWidth, targetHeight);
	return canvas.snapshot(null, null);
}
 
Example 2
Source File: WriteFxImage.java    From chart-fx with Apache License 2.0 7 votes vote down vote up
/**
 * copy the given Image to a WritableImage
 * 
 * @param image the input image
 * @return clone of image
 */
public static WritableImage clone(Image image) {
    int height = (int) image.getHeight();
    int width = (int) image.getWidth();
    WritableImage writableImage = WritableImageCache.getInstance().getImage(width, height);
    PixelWriter pixelWriter = writableImage.getPixelWriter();
    if (pixelWriter == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }

    final PixelReader pixelReader = image.getPixelReader();
    if (pixelReader == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            Color color = pixelReader.getColor(x, y);
            pixelWriter.setColor(x, y, color);
        }
    }
    return writableImage;
}
 
Example 3
Source File: ImageParticle.java    From tilesfx with Apache License 2.0 6 votes vote down vote up
public ImageParticle(final Image IMAGE, final double WIDTH, final double HEIGHT) {
    image      = IMAGE;
    halfWidth  = IMAGE.getWidth() * 0.5;
    halfHeight = IMAGE.getHeight() * 0.5;

    // Position
    x = RND.nextDouble() * WIDTH;
    y = HEIGHT + halfHeight;

    // Size
    size = (RND.nextDouble() * 1) + 0.5;

    // Velocity
    vX = (RND.nextDouble() * 0.5) - 0.25;
    vY = -(RND.nextDouble() * 3);

    // Opacity
    opacity = 1.0;

    // Image
    image = IMAGE;

    // Life
    life          = (RND.nextDouble() * 20) + 40;
    remainingLife = life;
}
 
Example 4
Source File: ViewText.java    From latexdraw with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Adds transparency to the given image.
 * @param img The image to transform.
 * @return The same image with white replaced by transparent.
 */
private WritableImage toTransparentPNG(final Image img) {
	final PixelReader pixelReader = img.getPixelReader();
	final WritableImage wImage = new WritableImage((int) img.getWidth(), (int) img.getHeight());
	final PixelWriter pixelWriter = wImage.getPixelWriter();

	for(int readY = 0; readY < img.getHeight(); readY++) {
		for(int readX = 0; readX < img.getWidth(); readX++) {
			final javafx.scene.paint.Color color = pixelReader.getColor(readX, readY);
			if (color.equals(javafx.scene.paint.Color.WHITE)) {
				pixelWriter.setColor(readX, readY, new javafx.scene.paint.Color(color.getRed(), color.getGreen(), color.getBlue(), 0)); // new javafx.scene.paint.Color(1, 1, 1, 0));
			} else {
				pixelWriter.setColor(readX, readY, color);
			}
		}
	}

	return wImage;
}
 
Example 5
Source File: WriteFxImageTests.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
private static void assertImageEqual(final Image original, final Image recovered, final boolean... assertRGBA) {
    final int w = (int) original.getWidth();
    final int h = (int) original.getHeight();
    assertEquals(w, recovered.getWidth());
    assertEquals(h, recovered.getHeight());
    if (assertRGBA.length == 0 || assertRGBA[0]) {
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                assertEquals(original.getPixelReader().getArgb(x, y), recovered.getPixelReader().getArgb(x, y));
            }
        }
    } else {
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                assertEquals((original.getPixelReader().getArgb(x, y) << 8) >> 8, (recovered.getPixelReader().getArgb(x, y) << 8) >> 8);
            }
        }
    }
}
 
Example 6
Source File: CheckPane.java    From pattypan with MIT License 6 votes vote down vote up
/**
 * Gets thumbnail of file
 *
 * @param file
 * @return
 */
private ImageView getScaledThumbnail(File file) {
  Image img = new Image(file.toURI().toString());
  ImageView view = new ImageView(img);

  int originalWidth = (int) img.getWidth();
  int originalHeight = (int) img.getHeight();
  int boundWidth = 400;
  int boundHeight = 400;
  int width = originalWidth;
  int height = originalHeight;

  if (originalWidth > boundWidth) {
    width = boundWidth;
    height = (width * originalHeight) / originalWidth;
  }
  if (height > boundHeight) {
    height = boundHeight;
    width = (height * originalWidth) / originalHeight;
  }

  view.setFitHeight(height);
  view.setFitWidth(width);
  return view;
}
 
Example 7
Source File: SpriteView.java    From quantumjava with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public SpriteView(Image spriteSheet, Main.Location loc) {
    imageView = new ImageView(spriteSheet);
    this.location.set(loc);
    setTranslateX(loc.getX() * Main.CELL_SIZE);
    setTranslateY(loc.getY() * Main.CELL_SIZE);
    ChangeListener<Object> updateImage = (ov, o, o2) -> imageView.setViewport(
        new Rectangle2D(frame.get() * spriteWidth,
            direction.get().getOffset() * spriteHeight,
            spriteWidth, spriteHeight));
    direction.addListener(updateImage);
    frame.addListener(updateImage);
    spriteWidth = (int) (spriteSheet.getWidth() / 3);
    spriteHeight = (int) (spriteSheet.getHeight() / 4);
    direction.set(Main.Direction.RIGHT);
    getChildren().add(imageView);
    arrivalHandler = e -> {
        MapObject object = Main.map[location.get().getX()][location.get().getY()];
        if (object != null) {
            object.visit(this);
        }
    };
}
 
Example 8
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image cutMarginsByWidth(Image image, int MarginWidth,
        boolean cutTop, boolean cutBottom, boolean cutLeft, boolean cutRight) {

    try {
        int imageWidth = (int) image.getWidth();
        int imageHeight = (int) image.getHeight();

        int top = 0, bottom = imageHeight - 1, left = 0, right = imageWidth - 1;
        if (cutTop) {
            top = MarginWidth;
        }
        if (cutBottom) {
            bottom -= MarginWidth;
        }
        if (cutLeft) {
            left = MarginWidth;
        }
        if (cutRight) {
            right -= MarginWidth;
        }
        return cropOutsideFx(image, left, top, right, bottom);
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }

}
 
Example 9
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public RectangleRed(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Rectangle;
    this.colorScopeType = ColorScopeType.Red;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
    } else {
        rectangle = new DoubleRectangle();
    }
}
 
Example 10
Source File: WriteFxImage.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes a JavaFx image as an RGB png image. If you pass in a ByteBuffer to
 * use, please make sure that it has enough capacity to fit the encoded image or
 * handle errors (IndexOutOfBoundsException) accordingly. If you want to be on
 * the safe side, use {@link #getCompressedSizeBound(int, int, boolean)
 * getCompressedSizeBound(width, height, alpha)} to get an upper bound for the
 * output size.
 *
 * @param image            The input image to be encoded
 * @param byteBuffer       optional byte buffer to store the output in, pass
 *                         null to return a new one.
 * @param alpha            whether to include alpha information in the image
 * @param compressionLevel {@link Deflater#BEST_COMPRESSION} (9) to
 *                         {@link Deflater#BEST_SPEED} (0)
 * @param metaInfo         an optional map which will be filled with debugging
 *                         information like compression efficiency
 * @return a byte buffer with the encoded image
 * @see "https://tools.ietf.org/html/rfc2083"
 */
public static ByteBuffer encodeAlt(final Image image, final ByteBuffer byteBuffer, final boolean alpha, final int compressionLevel, final Map<String, Object> metaInfo) {
    if (image == null) {
        throw new IllegalArgumentException(IMAGE_MUST_NOT_BE_NULL);
    }
    // get meta info
    // get necessary helper classes
    final PixelReader pr = image.getPixelReader();
    if (pr == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    final int w = (int) image.getWidth();
    final int h = (int) image.getHeight();

    // allocate output buffer if necessary. conservative allocation, assume upper
    // bound for deflate algorithm
    final ByteBuffer outputByteBuffer = byteBuffer == null ? ByteBuffer.allocate(getCompressedSizeBound(w, h, alpha)) : byteBuffer;
    final CRC32 crc = new CRC32();
    final Deflater compressor = new Deflater(compressionLevel);
    // actual PNG encoding
    writeImageHeader(w, h, alpha, outputByteBuffer, crc);
    writeImageData(pr, w, h, alpha, compressor, outputByteBuffer, crc);
    writeImageFooter(outputByteBuffer, crc);
    // prepare to return outputBuffer
    outputByteBuffer.flip();
    // DEGBUG output
    if (metaInfo != null) {
        final int bytesPerPixel = alpha ? 4 : 3;
        metaInfo.put("bufferSize", outputByteBuffer.capacity());
        metaInfo.put("outputSizeBound", getCompressedSizeBound(w, h, alpha));
        metaInfo.put("compression", (double) compressor.getBytesWritten() / (w * h * bytesPerPixel));
        metaInfo.put("width", w);
        metaInfo.put("height", h);
        metaInfo.put("colorMode", alpha ? "rgba" : "rgb");
        metaInfo.put("compressionLevel", compressionLevel);
        metaInfo.put("outputSize", outputByteBuffer.limit() - outputByteBuffer.position());
    }
    return outputByteBuffer;
}
 
Example 11
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image addArcFx(Image image, int arc, Color bgColor) {
        try {
            if (image == null || arc <= 0) {
                return null;
            }
            Group group = new Group();
            double imageWidth = image.getWidth(), imageHeight = image.getHeight();
            Scene scene = new Scene(group);

            ImageView view = new ImageView(image);
            view.setPreserveRatio(true);
            view.setFitWidth(imageWidth);
            view.setFitHeight(imageHeight);

            Rectangle clip = new Rectangle(imageWidth, imageHeight);
            clip.setArcWidth(arc);
            clip.setArcHeight(arc);
            view.setClip(clip);

            group.getChildren().add(view);

            Blend blend = new Blend(BlendMode.SRC_OVER);
            blend.setBottomInput(new ColorInput(0, 0, imageWidth, imageHeight, bgColor));
            group.setEffect(blend);

            SnapshotParameters parameters = new SnapshotParameters();
            parameters.setFill(Color.TRANSPARENT);
            WritableImage newImage = group.snapshot(parameters, null);
            return newImage;

        } catch (Exception e) {
//            logger.error(e.toString());
            return null;
        }

    }
 
Example 12
Source File: FindUserItem.java    From oim-fx with MIT License 5 votes vote down vote up
private void setGrayImage(Image image) {
	PixelReader pixelReader = image.getPixelReader();
	grayImage = new WritableImage((int) image.getWidth(), (int) image.getHeight());
	PixelWriter pixelWriter = grayImage.getPixelWriter();

	for (int y = 0; y < image.getHeight(); y++) {
		for (int x = 0; x < image.getWidth(); x++) {
			Color color = pixelReader.getColor(x, y);
			color = color.grayscale();
			pixelWriter.setColor(x, y, color);
		}
	}
}
 
Example 13
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public Rectangle(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Rectangle;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
    } else {
        rectangle = new DoubleRectangle();
    }
}
 
Example 14
Source File: Screenshot.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/** Get a AWT BufferedImage from JavaFX Image
 *  @param jfx {@link Image}
 *  @return BufferedImage
 */
public static BufferedImage bufferFromImage(final Image jfx)
{
    final BufferedImage img = new BufferedImage((int)jfx.getWidth(),
            (int)jfx.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    SwingFXUtils.fromFXImage(jfx, img);

    return img;
}
 
Example 15
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public CircleSaturation(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Circle;
    this.colorScopeType = ColorScopeType.Saturation;
    if (image != null) {
        circle = new DoubleCircle(image.getWidth() / 2, image.getHeight() / 2,
                image.getHeight() / 4);
    } else {
        circle = new DoubleCircle();
    }
}
 
Example 16
Source File: NinePatch.java    From oim-fx with MIT License 5 votes vote down vote up
public NinePatch(Image origin, int top, int right, int bottom, int left) {
    if (left + right > origin.getWidth()) {
        throw new IllegalArgumentException("left add right must less than origin width");
    }
    if (top + bottom > origin.getHeight()) {
        throw new IllegalArgumentException("top add bottom must less than origin height");
    }
    this.origin = origin;
    this.top = top;
    this.right = right;
    this.bottom = bottom;
    this.left = left;
}
 
Example 17
Source File: WidgetTreeCell.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void updateItem(final WidgetOrTab item, final boolean empty)
{
    super.updateItem(item, empty);
    if (empty || item == null)
    {
        setText(null);
        setGraphic(null);
    }
    else if (item.isWidget())
    {
        final Widget widget = item.getWidget();
        final String type = widget.getType();
        setText(widget.getName());
        final Image icon = WidgetIcons.getIcon(type);
        if (icon != null)
        {
            if (widget instanceof VisibleWidget)
            {
                final boolean visible = ((VisibleWidget)widget).propVisible().getValue();
                if (!visible)
                {
                    final double w = icon.getWidth();
                    final double h = icon.getHeight();
                    final Line l1 = new Line(0, 0, w, h);
                    final Line l2 = new Line(0, h, w, 0);
                    l1.setStroke(Color.RED);
                    l2.setStroke(Color.RED);
                    l1.setStrokeWidth(2);
                    l2.setStrokeWidth(2);
                    final StackPane pane = new StackPane(new ImageView(icon), l1, l2);
                    setGraphic(pane);
                }
                else
                    setGraphic(new ImageView(icon));
            }
            else
                setGraphic(new ImageView(icon));
        }
        else
            setGraphic(null);
    }
    else
    {
        setText(item.getTab().name().getValue());
        if (tab_icon != null)
            setGraphic(new ImageView(tab_icon));
        else
            setGraphic(null);
    }
}
 
Example 18
Source File: CImageDisplay.java    From Open-Lowcode with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Node getNode(
		PageActionManager actionmanager,
		CPageData inputdata,
		Window parentwindow,
		TabPane[] parenttabpanes,
		CollapsibleNode nodetocollapsewhenactiontriggered) {
	inputdata.addInlineActionDataRef(this.inlineactiondataref);
	this.actionmanager = actionmanager;
	SFile thumbnail = getThumbnail(inputdata, datareference);
	if (!thumbnail.isEmpty()) {
		ByteArrayInputStream imagedata = new ByteArrayInputStream(thumbnail.getContent());
		Image image = new Image(imagedata);
		double imagewidth = image.getWidth();
		double imageheight = image.getHeight();
		thumbnailview = new ImageView(image);
		thumbnailview.setFitWidth(imagewidth);
		thumbnailview.setFitHeight(imageheight);
		thumbnailview.setOnMousePressed(actionmanager.getMouseHandler());
		actionmanager.registerInlineAction(thumbnailview, fullimageaction);
		BorderPane border = new BorderPane();
		border.setBorder(new Border(
				new BorderStroke(Color.web("#ddeeff"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY,
						new BorderWidths(3)),
				new BorderStroke(Color.web("#bbccdd"), BorderStrokeStyle.SOLID, CornerRadii.EMPTY,
						new BorderWidths(1))));
		border.setCenter(thumbnailview);
		border.setMaxHeight(imageheight + 6);
		border.setMaxWidth(imagewidth + 6);
		DropShadow ds = new DropShadow();
		ds.setRadius(3.0);
		ds.setOffsetX(1.5);
		ds.setOffsetY(1.5);
		ds.setColor(Color.color(0.2, 0.2, 0.2));
		border.setEffect(ds);
		if (this.islabel) {
			FlowPane thispane = new FlowPane();
			Label thislabel = new Label(label);
			thislabel.setFont(
					Font.font(thislabel.getFont().getName(), FontPosture.ITALIC, thislabel.getFont().getSize()));
			thislabel.setMinWidth(120);
			thislabel.setWrapText(true);
			thislabel.setMaxWidth(120);
			thispane.setRowValignment(VPos.TOP);
			thispane.getChildren().add(thislabel);
			thispane.getChildren().add(border);
			return thispane;
		} else {
			return border;
		}

	}

	return null;
}
 
Example 19
Source File: Sprite.java    From Introduction-to-JavaFX-for-Game-Development with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setImage(Image i)
{
    image = i;
    width = i.getWidth();
    height = i.getHeight();
}
 
Example 20
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image addMarginsFx(Image image, Color color, int MarginWidth,
            boolean addTop, boolean addBottom, boolean addLeft, boolean addRight) {
        try {
            if (image == null || MarginWidth <= 0) {
                return image;
            }
            int width = (int) image.getWidth();
            int height = (int) image.getHeight();
            int totalWidth = width, totalHegiht = height;
            int x1 = 0, y1 = 0, x2 = width, y2 = height;
            if (addLeft) {
                totalWidth += MarginWidth;
                x1 = MarginWidth;
                x2 = width + MarginWidth;
            }
            if (addRight) {
                totalWidth += MarginWidth;
            }
            if (addTop) {
                totalHegiht += MarginWidth;
                y1 = MarginWidth;
                y2 = height + MarginWidth;
            }
            if (addBottom) {
                totalHegiht += MarginWidth;
            }
//            logger.debug(width + "  " + totalWidth);

            PixelReader pixelReader = image.getPixelReader();
            WritableImage newImage = new WritableImage(totalWidth, totalHegiht);
            PixelWriter pixelWriter = newImage.getPixelWriter();
            pixelWriter.setPixels(x1, y1, width, height, pixelReader, 0, 0);

//            logger.debug(x1 + "  " + y1);
//            logger.debug(totalWidth + "  " + totalHegiht);
            if (addLeft) {
                for (int x = 0; x < MarginWidth; x++) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addRight) {
                for (int x = totalWidth - 1; x > totalWidth - MarginWidth - 1; x--) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addTop) {
                for (int y = 0; y < MarginWidth; y++) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addBottom) {
                for (int y = totalHegiht - 1; y > totalHegiht - MarginWidth - 1; y--) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }

            return newImage;

        } catch (Exception e) {
            logger.error(e.toString());
            return image;
        }

    }