下面列出了怎么用javafx.scene.paint.Paint的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public ObjectProperty<Paint> iconColorProperty() {
if (iconColor == null) {
iconColor = new StyleableObjectProperty<Paint>(Color.BLACK) {
@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.ICON_COLOR;
}
@Override
public Object getBean() {
return SVGIcon.this;
}
@Override
public String getName() {
return "iconColor";
}
};
iconColor.addListener(iconColorChangeListener);
}
return iconColor;
}
@Override
public ObjectProperty<Paint> iconColorProperty() {
if (iconColor == null) {
iconColor = new StyleableObjectProperty<>(Color.BLACK) {
@Override
public CssMetaData getCssMetaData() {
return StyleableProperties.ICON_COLOR;
}
@Override
public Object getBean() {
return FontIcon.this;
}
@Override
public String getName() {
return "iconColor";
}
};
iconColor.addListener((v, o, n) -> FontIcon.this.setFill(n));
}
return iconColor;
}
/**
* Creates a rectangular {@link Image} to visualize the given {@link Paint}.
*
* @param width
* The width of the resulting {@link Image}.
* @param height
* The height of the resulting {@link Image}.
* @param paint
* The {@link Paint} to use for filling the {@link Image}.
* @return The resulting (filled) {@link Image}.
*/
public static ImageData getPaintImageData(int width, int height, Paint paint) {
// use JavaFX canvas to render a rectangle with the given paint
Canvas canvas = new Canvas(width, height);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
graphicsContext.setFill(paint);
graphicsContext.fillRect(0, 0, width, height);
graphicsContext.setStroke(Color.BLACK);
graphicsContext.strokeRect(0, 0, width, height);
// handle transparent color separately (we want to differentiate it from
// transparent fill)
if (paint instanceof Color && ((Color) paint).getOpacity() == 0) {
// draw a red line from bottom-left to top-right to indicate a
// transparent fill color
graphicsContext.setStroke(Color.RED);
graphicsContext.strokeLine(0, height - 1, width, 1);
}
WritableImage snapshot = canvas.snapshot(new SnapshotParameters(), null);
return SWTFXUtils.fromFXImage(snapshot, null);
}
private void drawLabel(StateTransitionEdge pEdge, GraphicsContext pGraphics)
{
adjustLabelFont(pEdge);
Rectangle2D labelBounds = getLabelBounds(pEdge);
double x = labelBounds.getMinX();
double y = labelBounds.getMinY();
Paint oldFill = pGraphics.getFill();
Font oldFont = pGraphics.getFont();
pGraphics.translate(x, y);
pGraphics.setFill(Color.BLACK);
pGraphics.setFont(aFont);
pGraphics.setTextAlign(TextAlignment.CENTER);
pGraphics.fillText(pEdge.getMiddleLabel(), labelBounds.getWidth()/2, 0);
pGraphics.setFill(oldFill);
pGraphics.setFont(oldFont);
pGraphics.translate(-x, -y);
}
public IconToggleButton(String text, Font font, Paint paint, Image normalImage, Image hoverImage, Image selectedImage) {
this(text);
this.normalImage = normalImage;
this.hoverImage = hoverImage;
this.selectedImage = selectedImage;
if (null != font) {
this.setFont(font);
}
if (null != paint) {
this.setTextFill(paint);
}
initEvent();
updateImage();
VBox redVBox=new VBox();
redVBox.setAlignment(Pos.TOP_RIGHT);
redVBox.getChildren().add(redImageView);
stackPane.getChildren().add(imageView);
stackPane.getChildren().add(redVBox);
this.getStyleClass().add(DEFAULT_STYLE_CLASS);
}
public void addColorControl(final String title,
final ObjectProperty<Paint> paintProperty) {
final ColorPicker colorPicker = ColorPickerBuilder.create()
.value((Color) paintProperty.get()).build();
paintProperty.bind(colorPicker.valueProperty());
final VBox box = new VBox();
final Text titleText = new Text(title);
titleText.textProperty().bind(new StringBinding() {
{
super.bind(colorPicker.valueProperty());
}
@Override
protected String computeValue() {
return title + " : " + colorPicker.getValue().toString();
}
});
box.getChildren().addAll(titleText, colorPicker);
getChildren().add(box);
}
public final ObjectProperty<Paint> titleTextColorProperty() {
if (null == titleTextColor) {
titleTextColor = new StyleableObjectProperty<Paint>(DEFAULT_TITLE_TEXT_COLOR) {
@Override public CssMetaData getCssMetaData() { return StyleableProperties.TITLE_TEXT_COLOR; }
@Override public Object getBean() { return this; }
@Override public String getName() { return "titleTextColor"; }
};
}
return titleTextColor;
}
public final ObjectProperty<Paint> sectionFill2Property() {
if (null == sectionFill2) {
sectionFill2 = new StyleableObjectProperty<Paint>(DEFAULT_SECTION_FILL_2) {
@Override public CssMetaData getCssMetaData() { return StyleableProperties.SECTION_FILL_2; }
@Override public Object getBean() { return this; }
@Override public String getName() { return "sectionFill2"; }
};
}
return sectionFill2;
}
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);
});
}
public void drawContour(GraphicsContext gc) {
gc.save();
gc.setStroke(getStroke());
gc.setLineWidth(getStrokeWidth());
gc.setFill(getFill());
if (map == null) {
drawHexagon(gc, Direction.values());
gc.restore();
}
final List<Direction> list = new ArrayList<>();
for (final Direction direction : Direction.values()) {
final Hexagon neighbour = getNeighbour(direction);
if (neighbour == null) {
continue;
}
final Paint stroke = neighbour.getStroke();
// if (stroke != null && !stroke.equals(this.getStroke())) {
// list.add(direction);
// }
// TODO: find work-around for colour smoothing operation on image scaling
if (stroke != null && !myColourCompare(stroke, getStroke(), 0.2)) {
list.add(direction);
}
}
drawHexagon(gc, list.toArray(new Direction[list.size()]));
gc.restore();
}
public void setChartBackground(final Paint PAINT) {
if (null == chartBackground) {
_chartBackground = PAINT;
redraw();
} else {
chartBackground.set(PAINT);
}
}
/**
* Defines the Paint object that will be used to fill the foreground of the clock.
* This could be used to visualize glass effects etc. and is only rarely used.
* @param PAINT
*/
public void setForegroundPaint(final Paint PAINT) {
if (null == foregroundPaint) {
_foregroundPaint = PAINT;
fireUpdateEvent(REDRAW_EVENT);
} else {
foregroundPaint.set(PAINT);
}
}
public ObjectProperty<Paint> borderPaintProperty() {
if (null == borderPaint) {
borderPaint = new ObjectPropertyBase<Paint>(_borderPaint) {
@Override protected void invalidated() { fireUpdateEvent(REDRAW_EVENT); }
@Override public Object getBean() { return Clock.this; }
@Override public String getName() { return "borderPaint"; }
};
_borderPaint = null;
}
return borderPaint;
}
private void addGraphBackground(Paint paint) {
double margin = 5;
GraphPart graphPart = (GraphPart) getContentViewer().getRootPart()
.getContentPartChildren().get(0);
Group group = graphPart.getVisual();
Bounds bounds = group.getLayoutBounds();
group.setEffect(new Blend(BlendMode.SRC_OVER,
new ColorInput(bounds.getMinX() - margin,
bounds.getMinY() - margin,
bounds.getWidth() + 2 * margin,
bounds.getHeight() + 2 * margin, paint),
null));
}
public ObjectProperty<Paint> majorVGridLinePaintProperty() {
if (null == majorVGridLinePaint) {
majorVGridLinePaint = new ObjectPropertyBase<Paint>(_majorVGridLinePaint) {
@Override protected void invalidated() { drawGrid(); }
@Override public Object getBean() { return Grid.this; }
@Override public String getName() { return "majorVGridLinePaint"; }
};
_majorVGridLinePaint = null;
}
return majorVGridLinePaint;
}
public TilesFXSeries(final Series<X, Y> SERIES, final Paint COLOR) {
series = SERIES;
stroke = COLOR;
fill = COLOR;
if (null != COLOR) {
symbolBackground = new Background(new BackgroundFill(COLOR, new CornerRadii(5), Insets.EMPTY), new BackgroundFill(Color.WHITE, new CornerRadii(5), new Insets(2)));
legendSymbolFill = COLOR;
}
}
/**
* Java 8: javafx.scene.text.Text.impl_selectionFillProperty()
* Java 9+: javafx.scene.text.Text.selectionFillProperty()
*/
@SuppressWarnings("unchecked")
static ObjectProperty<Paint> Text_selectionFillProperty(Text text) {
try {
if (mText_selectionFillProperty == null) {
mText_selectionFillProperty = Text.class.getMethod(
isJava9orLater ? "selectionFillProperty" : "impl_selectionFillProperty");
}
return (ObjectProperty<Paint>) mText_selectionFillProperty.invoke(text);
} catch(NoSuchMethodException | SecurityException | IllegalAccessException |
IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
throw new Error(e);
}
}
IndeterminateSpinner(TxConfidenceIndicator control, StaticProgressIndicatorSkin s,
boolean spinEnabled, Paint fillOverride) {
this.control = control;
this.skin = s;
this.fillOverride = fillOverride;
setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
getStyleClass().setAll("spinner");
pathsG = new IndicatorPaths(this);
getChildren().add(pathsG);
rebuild();
}
/**
* changes opacity, if paint is a color
*
* @param paint
* @param opacity
* @return paint
*/
public static Paint changeOpacity(Paint paint, double opacity) {
if (paint instanceof Color) {
Color color = (Color) paint;
return new Color(color.getRed(), color.getGreen(), color.getBlue(), opacity);
} else
return paint;
}
@Override public StyleableProperty<Paint> getStyleableProperty(SimpleLineChart chart) {
return (StyleableProperty) chart.sectionFill3Property();
}
public final void setSectionFill8(Paint value) {
sectionFill8Property().set(value);
}
public final void setSectionFill2(Paint value) {
sectionFill2Property().set(value);
}
public final void setThumbColor(Paint value) {
thumbColorProperty().set(value);
}
public final Paint getMarkerFill4() {
return null == markerFill4 ? DEFAULT_MARKER_FILL_4 : markerFill4.get();
}
@Test
@DisabledOnMac
public void checkCSSFXCanChangeTheLabelFontColor(FxRobot robot) throws Exception {
// The CSS used by the UI
URI basicCSS = BasicUI.class.getResource("basic.css").toURI();
String basicCSSUrl = basicCSS.toURL().toExternalForm();
// Resources containing the color changes we want to apply to the CSS
URI changedBasicCSSBlue = BasicUI.class.getResource("basic-cssfx-blue.css").toURI();
URI changedBasicCSSRed = BasicUI.class.getResource("basic-cssfx-red.css").toURI();
// The file we will tell CSSFX to map the CSS to
Path mappedSourceFile = Files.createTempFile("tmp", ".css");
mappedSourceFile.toFile().deleteOnExit();
// Let's start with the normal content first
Files.copy(Paths.get(changedBasicCSSBlue), mappedSourceFile, StandardCopyOption.REPLACE_EXISTING);
// start CSSFX
Runnable stopper = CSSFX.onlyFor(basicUI.getRootNode())
.noDefaultConverters()
.addConverter((uri) -> {
if (basicCSSUrl.equals(uri)) {
return mappedSourceFile;
}
return null;
}).start();
try {
// We need to let CSSFX some time to detect the file change
Thread.sleep(1000);
// Let's check that initial launch has used the mapped
Label cssfxLabel = robot.lookup("#cssfx").queryAs(Label.class);
Paint textColor = cssfxLabel.getTextFill();
assertThat("retrieved color is not one of expected", getExpectedTextColor(), hasItem(textColor));
assertThat(textColor, is(Color.BLUE));
// Copy the modified version in to the "source" file
try {
Files.copy(Paths.get(changedBasicCSSRed), mappedSourceFile, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
// We TestMemoryLeaksneed to let CSSFX some time to detect the file change
Thread.sleep(1000);
textColor = cssfxLabel.getTextFill();
assertThat("retrieved color is not one of expected", getExpectedTextColor(), hasItem(textColor));
assertThat(textColor, is(Color.RED));
} finally {
stopper.run();
}
}
public ObjectProperty<Paint> backgroundMouseOnFillProperty() {
return backgroundMouseOnFill;
}
public ObjectProperty<Paint> strokeFillProperty() {
return strokeFill;
}
public final Paint getLedColor() {
return null == ledColor ? DEFAULT_LED_COLOR : ledColor.get();
}
public Paint getFill() {
return fill;
}
public ObjectProperty<Paint> tickLabelFillProperty() {
return tickLabelFill;
}