下面列出了怎么用javafx.scene.paint.ImagePattern的API类实例代码及写法,或者点击链接到github查看源代码。
public static void setFillScheme(final GraphicsContext gc, final String defaultStyle, final int dsIndex) {
AssertUtils.gtEqThanZero("setFillScheme dsIndex", dsIndex);
final Map<String, List<String>> map = splitQuery(defaultStyle);
final Color fillColor = StyleParser.getColorPropertyValue(defaultStyle, XYChartCss.FILL_COLOR);
if (fillColor != null) {
final Color rawColor = fillColor;
final Color color = getColorModifier(map, rawColor);
if (color == null) {
return;
}
final ImagePattern hatch = FillPatternStyleHelper.getDefaultHatch(color.brighter(),
dsIndex * hatchShiftByIndexProperty().get());
gc.setFill(hatch);
} else {
final int size = fillStylesProperty().size();
gc.setFill(fillStylesProperty().get(dsIndex % size));
}
}
@Override
protected Node initBottomGui() {
Polygon shape = new Polygon();
shape.getPoints().setAll(chunkPositions.stream().flatMap(v -> Stream.of(v.x(), v.y())).map(d -> (d + 1) * 16.0).collect(Collectors.toList()));
shape.setFill(new ImagePattern(image, 0, 0, 16, 16, false));
getTopGui().hoverProperty().addListener(e -> {
if (getTopGui().isHover())
shape.setOpacity(0.6);
else
shape.setOpacity(0.2);
});
shape.setOpacity(0.2);
shape.setMouseTransparent(true);
shape.setCache(true);
shape.setCacheHint(CacheHint.SCALE);
shape.setViewOrder(1);
return shape;
}
public static ImagePattern getDefaultHatch(final Color color) {
ImagePattern retVal = FillPatternStyleHelper.defaultHatchCache.get(color);
if (retVal == null) {
retVal = FillPatternStyleHelper.getDefaultHatch(color, 0.0);
FillPatternStyleHelper.defaultHatchCache.put(color, retVal);
}
return retVal;
}
private static ImagePattern createDefaultHatch(final Paint color, final double strokeWidth,
final boolean isHorizontal, final double[] pattern) {
final Integer hash = computeHash(color, strokeWidth, isHorizontal, pattern);
return DashPatternStyle.dashHashMap.computeIfAbsent(hash, t -> {
// need to recompute hatch pattern image
final double dashPatternLength = getPatternLength(pattern);
final double width = isHorizontal ? dashPatternLength : strokeWidth;
final double height = isHorizontal ? strokeWidth : dashPatternLength;
final double middle = (int) (strokeWidth / 2.0);
final Pane pane = new Pane();
pane.setPrefSize(width, height);
final Line fw = isHorizontal ? new Line(0, middle, dashPatternLength, middle)
: new Line(middle, 0, middle, dashPatternLength);
fw.setSmooth(false);
fw.setStroke(color);
if (pattern == null) {
fw.getStrokeDashArray().setAll(dashPatternLength);
} else {
fw.getStrokeDashArray().setAll(DoubleStream.of(pattern).boxed().collect(Collectors.toList()));
}
fw.setStrokeWidth(strokeWidth);
pane.getChildren().addAll(fw);
pane.setStyle("-fx-background-color: rgba(0, 0, 0, 0.0)");
final Scene scene = new Scene(pane);
scene.setFill(Color.TRANSPARENT);
final Image hatch = pane.snapshot(null, null);
return new ImagePattern(hatch, width, 0, width, height, false);
});
}
private void setUpUI(){
Image userImage = new Image("/images/chamod.jpg");
Circle circle = new Circle(75);
ImagePattern imagePattern = new ImagePattern(userImage);
circle.setFill(imagePattern);
imgUser.setImage(userImage);
}
private Rectangle buildFigure(int x, int y, int size, String image) {
Rectangle rect = new Rectangle();
rect.setX(x);
rect.setY(y);
rect.setHeight(size);
rect.setWidth(size);
Image img = new Image(this.getClass().getClassLoader().getResource(image).toString());
rect.setFill(new ImagePattern(img));
final Rectangle momento = new Rectangle(x, y);
rect.setOnDragDetected(
event -> {
momento.setX(event.getX());
momento.setY(event.getY());
}
);
rect.setOnMouseDragged(
event -> {
rect.setX(event.getX() - size / 2);
rect.setY(event.getY() - size / 2);
}
);
rect.setOnMouseReleased(
event -> {
if (logic.move(this.extract(momento.getX(), momento.getY()),
this.extract(event.getX(), event.getY()))) {
rect.setX(((int) event.getX() / 40) * 40 + 5);
rect.setY(((int) event.getY() / 40) * 40 + 5);
checkWinner();
} else {
rect.setX(((int) momento.getX() / 40) * 40 + 5);
rect.setY(((int) momento.getY() / 40) * 40 + 5);
}
}
);
return rect;
}
private Rectangle buildFigure(int x, int y, int size, String image) {
Rectangle rect = new Rectangle();
rect.setX(x);
rect.setY(y);
rect.setHeight(size);
rect.setWidth(size);
Image img = new Image(this.getClass().getClassLoader().getResource(image).toString());
rect.setFill(new ImagePattern(img));
return rect;
}
private Rectangle buildFigure(int x, int y, int size, String image) {
Rectangle rect = new Rectangle();
rect.setX(x);
rect.setY(y);
rect.setHeight(size);
rect.setWidth(size);
Image img = new Image(this.getClass().getClassLoader().getResource(image).toString());
rect.setFill(new ImagePattern(img));
final Rectangle momento = new Rectangle(x, y);
rect.setOnDragDetected(
event -> {
momento.setX(event.getX());
momento.setY(event.getY());
}
);
rect.setOnMouseDragged(
event -> {
rect.setX(event.getX() - size / 2);
rect.setY(event.getY() - size / 2);
}
);
rect.setOnMouseReleased(
event -> {
try {
logic.move(
this.findBy(momento.getX(), momento.getY()),
this.findBy(event.getX(), event.getY()));
rect.setX(((int) event.getX() / 40) * 40 + 5);
rect.setY(((int) event.getY() / 40) * 40 + 5);
} catch (Exception e) {
Alert info = new Alert(Alert.AlertType.ERROR);
info.setContentText(e.getClass().getName() + " " + e.getMessage());
info.show();
rect.setX(((int) momento.getX() / 40) * 40 + 5);
rect.setY(((int) momento.getY() / 40) * 40 + 5);
}
}
);
return rect;
}
public ImagePattern apply(final Shape SHAPE) {
double x = SHAPE.getLayoutBounds().getMinX();
double y = SHAPE.getLayoutBounds().getMinY();
double width = SHAPE.getLayoutBounds().getWidth();
double height = SHAPE.getLayoutBounds().getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern getImagePattern(final Rectangle BOUNDS) {
double x = BOUNDS.getX();
double y = BOUNDS.getY();
double width = BOUNDS.getWidth();
double height = BOUNDS.getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern getImagePattern(final CtxBounds BOUNDS) {
double x = BOUNDS.getMinX();
double y = BOUNDS.getMinY();
double width = BOUNDS.getWidth();
double height = BOUNDS.getHeight();
centerX = BOUNDS.getCenterX();
centerY = BOUNDS.getCenterY();
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
@Override
public void setCanvasBackgroundToImage(@Nullable String imgPath) {
if (imgPath == null) {
uiCanvasEditor.setCanvasBackgroundImage(null);
return;
}
uiCanvasEditor.setCanvasBackgroundImage(new ImagePattern(new Image(imgPath)));
}
public void updateImageAsTiled(@NotNull Image img, int tileW, int tileH, boolean modifyBoth) {
ImagePattern pattern = new ImagePattern(img, 0, 0, tileW, tileH, false);
ColorInput colorInput = new ColorInput(0, 0, w, h, pattern);
if (modifyBoth || previewMode) {
imgTrans2.setInput(colorInput);
}
if (modifyBoth || !previewMode) {
imgTrans1.setInput(colorInput);
}
}
public ImagePattern apply(final Shape SHAPE) {
double x = SHAPE.getLayoutBounds().getMinX();
double y = SHAPE.getLayoutBounds().getMinY();
double width = SHAPE.getLayoutBounds().getWidth();
double height = SHAPE.getLayoutBounds().getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern getImagePattern(final Rectangle BOUNDS) {
double x = BOUNDS.getX();
double y = BOUNDS.getY();
double width = BOUNDS.getWidth();
double height = BOUNDS.getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern apply(final Shape SHAPE) {
double x = SHAPE.getLayoutBounds().getMinX();
double y = SHAPE.getLayoutBounds().getMinY();
double width = SHAPE.getLayoutBounds().getWidth();
double height = SHAPE.getLayoutBounds().getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern getImagePattern(final Rectangle BOUNDS) {
double x = BOUNDS.getX();
double y = BOUNDS.getY();
double width = BOUNDS.getWidth();
double height = BOUNDS.getHeight();
centerX = width * 0.5;
centerY = height * 0.5;
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public Image createPattern(CarbonPatterns cp, boolean save) {
ImagePattern pattern;
switch (cp) {
case DARK_CARBON:
pattern = createCarbonPattern();
break;
case LIGHT_CARBON:
pattern = createLightCarbonPattern();
break;
case CARBON_KEVLAR:
pattern = createCarbonKevlarPattern();
break;
default:
pattern = createCarbonPattern();
break;
}
Rectangle rectangle = new Rectangle(width, height);
if (pattern != null) {
rectangle.setFill(pattern);
}
rectangle.setStrokeWidth(0);
imgPattern = rectangle.snapshot(new SnapshotParameters(), null);
if (save) {
saveImage();
}
return imgPattern;
}
public SpecklesView(Viewport viewport, long ecosystemSize) {
this.viewport = viewport;
createBackgroundTextures();
background = new Rectangle(0, 0, ecosystemSize, ecosystemSize);
background.setFill(new ImagePattern(backgroundTexture, 0, 0, viewport.getSizeSC().x, viewport.getSizeSC().y, false));
infraredBackground = new Rectangle(0, 0, ecosystemSize, ecosystemSize);
infraredBackground
.setFill(new ImagePattern(infraredBackgroundTexture, 0, 0, viewport.getSizeSC().x, viewport.getSizeSC().y, false));
}
public ImagePattern apply(final Shape SHAPE) {
double x = SHAPE.getLayoutBounds().getMinX();
double y = SHAPE.getLayoutBounds().getMinY();
double width = SHAPE.getLayoutBounds().getWidth();
double height = SHAPE.getLayoutBounds().getHeight();
center = new Point2D(width * 0.5, height * 0.5);
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
public ImagePattern apply(final Shape SHAPE) {
double x = SHAPE.getLayoutBounds().getMinX();
double y = SHAPE.getLayoutBounds().getMinY();
double width = SHAPE.getLayoutBounds().getWidth();
double height = SHAPE.getLayoutBounds().getHeight();
return new ImagePattern(getImage(width, height), x, y, width, height, false);
}
private Paint getHatchingsFillingPaint(final FillingStyle style) {
final Bounds bounds = border.getBoundsInParent();
if(bounds.getWidth() <= 0d || bounds.getHeight() <= 0d) {
return null;
}
final Group hatchings = new Group();
final double hAngle = model.getHatchingsAngle();
hatchings.getChildren().add(new Rectangle(bounds.getWidth(), bounds.getHeight(), style.isFilled() ? model.getFillingCol().toJFX() : null));
final double angle = hAngle > 0d ? hAngle - Math.PI / 2d : hAngle + Math.PI / 2d;
switch(style) {
case VLINES, VLINES_PLAIN -> computeHatchings(hatchings, hAngle, bounds.getWidth(), bounds.getHeight());
case HLINES, HLINES_PLAIN -> computeHatchings(hatchings, angle, bounds.getWidth(), bounds.getHeight());
case CLINES, CLINES_PLAIN -> {
computeHatchings(hatchings, hAngle, bounds.getWidth(), bounds.getHeight());
computeHatchings(hatchings, angle, bounds.getWidth(), bounds.getHeight());
}
}
final WritableImage image = new WritableImage((int) bounds.getWidth(), (int) bounds.getHeight());
hatchings.snapshot(new SnapshotParameters(), image);
return new ImagePattern(image, 0, 0, 1, 1, true);
}
public static ImagePattern getDefaultHatch(final Paint color, final double xOffset) {
return new ImagePattern(FillPatternStyleHelper.createDefaultHatch(color, 1.0), xOffset, xOffset,
FillPatternStyleHelper.HATCH_WINDOW_SIZE, FillPatternStyleHelper.HATCH_WINDOW_SIZE, false);
}
public static ImagePattern getHatch(final FillPattern fillPattern, final Paint color) {
return FillPatternStyleHelper.getHatch(fillPattern, color, 1.0);
}
public static ImagePattern getHatch(final FillPattern fillPattern, final Paint color, final double width) {
final Image hatch = FillPatternStyleHelper.createHatch(fillPattern, color, width);
return new ImagePattern(hatch, 0, 0, FillPatternStyleHelper.HATCH_WINDOW_SIZE, FillPatternStyleHelper.HATCH_WINDOW_SIZE,
false);
}
/** Update background, using background color and grid information from model */
private void updateBackground()
{
final WidgetColor background = model.propBackgroundColor().getValue();
// Setting the "-fx-background:" of the root node propagates
// to all child nodes in the scene graph.
//
// if (isEditMode())
// model_root.setStyle("-fx-background: linear-gradient(from 0px 0px to 10px 10px, reflect, #D2A2A2 48%, #D2A2A2 2%, #D2D2A2 48% #D2D2A2 2%)");
// else
// model_root.setStyle("-fx-background: " + JFXUtil.webRGB(background));
//
// In edit mode, this results in error messages because the linear-gradient doesn't "work" for all nodes:
//
// javafx.scene.CssStyleHelper (calculateValue)
// Caught java.lang.ClassCastException: javafx.scene.paint.LinearGradient cannot be cast to javafx.scene.paint.Color
// while converting value for
// '-fx-background-color' from rule '*.text-input' in stylesheet ..jfxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
// '-fx-effect' from rule '*.scroll-bar:vertical>*.increment-button>*.increment-arrow' in StyleSheet ... jfxrt.jar!/com/sun/javafx/scene/control/skin/modena/modena.bss
// '-fx-effect' from rule '*.scroll-bar:vertical>*.decrement-button>*.decrement-arrow' in stylesheet ... modena.bss
// '-fx-effect' from rule '*.scroll-bar:horizontal>*.increment-button>*.increment-arrow' in stylesheet ... modena.bss
//
// In the runtime, the background color style is applied to for example the TextEntryRepresentation,
// overriding its jfx_node.setBackground(..) setting.
// Setting just the scroll body background to a plain color or grid image provides basic color control.
// In edit mode, the horiz_bound, vert_bound lines and grid provide sufficient
// visual indication of the display size.
final Color backgroundColor = new Color(background.getRed(), background.getGreen(), background.getBlue());
final boolean gridVisible = isEditMode() ? model.propGridVisible().getValue() : false;
final int gridStepX = model.propGridStepX().getValue(),
gridStepY = model.propGridStepY().getValue();
final WidgetColor grid_rgb = model.propGridColor().getValue();
final Color gridColor = new Color(grid_rgb.getRed(), grid_rgb.getGreen(), grid_rgb.getBlue());
final BufferedImage image = new BufferedImage(gridStepX, gridStepY, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = image.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setBackground(backgroundColor);
g2d.clearRect(0, 0, gridStepX, gridStepY);
if (gridVisible)
{
g2d.setColor(gridColor);
g2d.setStroke(new BasicStroke(GRID_LINE_WIDTH));
g2d.drawLine(0, 0, gridStepX, 0);
g2d.drawLine(0, 0, 0, gridStepY);
}
final WritableImage wimage = new WritableImage(gridStepX, gridStepY);
SwingFXUtils.toFXImage(image, wimage);
final ImagePattern pattern = new ImagePattern(wimage, 0, 0, gridStepX, gridStepY, false);
widget_parent.setBackground(new Background(new BackgroundFill(pattern, CornerRadii.EMPTY, Insets.EMPTY)));
}
public ImagePattern getImagePattern(final Bounds BOUNDS) {
return getImagePattern(new Rectangle(BOUNDS.getMinX(), BOUNDS.getMinY(), BOUNDS.getWidth(), BOUNDS.getHeight()));
}
public ImagePattern getImagePattern(final Bounds BOUNDS) {
return getImagePattern(new Rectangle(BOUNDS.getMinX(), BOUNDS.getMinY(), BOUNDS.getWidth(), BOUNDS.getHeight()));
}
@Override
@Nullable
public ImagePattern getCanvasBackgroundImage() {
return uiCanvasEditor.getBackgroundImage();
}
/** @return the background image of the canvas, or null if not set */
@Nullable ImagePattern getCanvasBackgroundImage();