下面列出了javafx.scene.effect.BlendMode#SRC_OVER 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Image addTextFx(Image image, String textString,
Font font, Color color, int x, int y, float transparent, int shadow) {
try {
Group group = new Group();
Text text = new Text(x, y, textString);
text.setFill(color);
text.setFont(font);
if (shadow > 0) {
DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(shadow);
dropShadow.setOffsetY(shadow);
text.setEffect(dropShadow);
}
group.getChildren().add(text);
Blend blend = new Blend(BlendMode.SRC_OVER);
blend.setBottomInput(new ImageInput(image));
blend.setOpacity(1.0 - transparent);
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;
}
}
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;
}
}
public void start1(Stage primaryStage) {
try {
BlendMode bm1 = BlendMode.MULTIPLY;
BlendMode bm2 = BlendMode.SRC_OVER;
Node[] node = setEffectOnGroup(bm1, bm2);
//Node[] node = setModeOnGroup(bm1, bm2);
//Node[] node = setEffectOnCircle(bm1, bm2);
//Node[] node = setEffectOnSquare(bm1, bm2);
//Node[] node = setModeOnCircle(bm1, bm2);
//Node[] node = setModeOnSquare(bm1, bm2);
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(40);
grid.setVgap(15);
grid.setPadding(new Insets(20, 20, 20, 20));
int i = 0;
grid.addRow(i++, new Text("Circle top"), new Text("Square top"));
grid.add(node[0], 0, i++, 2, 1);
GridPane.setHalignment(node[0], HPos.CENTER);
grid.addRow(i++, node[1], node[2]);
grid.add(node[3], 0, i++, 2, 1);
GridPane.setHalignment(node[3], HPos.CENTER);
grid.addRow(i++, node[4], node[5]);
Text txt = new Text("Circle opacity - 0.5\nSquare opacity - 1.0");
grid.add(txt, 0, i++, 2, 1);
GridPane.setHalignment(txt, HPos.CENTER);
Scene scene = new Scene(grid, 350, 350);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX blend effect");
primaryStage.onCloseRequestProperty()
.setValue(e -> System.out.println("Bye! See you later!"));
primaryStage.show();
} catch (Exception ex){
ex.printStackTrace();
}
}
public static Image indicateSplitFx(Image image,
List<Integer> rows, List<Integer> cols,
Color lineColor, int lineWidth, boolean showSize) {
try {
if (rows == null || cols == null) {
return image;
}
Group group = new Group();
int width = (int) image.getWidth();
int height = (int) image.getHeight();
int row;
for (int i = 0; i < rows.size(); ++i) {
row = rows.get(i);
if (row <= 0 || row >= height - 1) {
continue;
}
Line rowLine = new Line(0, row, width, row);
rowLine.setStroke(lineColor);
rowLine.setStrokeWidth(lineWidth);
group.getChildren().add(rowLine);
}
int col;
for (int i = 0; i < cols.size(); ++i) {
col = cols.get(i);
if (col <= 0 || col >= width - 1) {
continue;
}
Line colLine = new Line(col, 0, col, height);
colLine.setStroke(lineColor);
colLine.setStrokeWidth(lineWidth);
group.getChildren().add(colLine);
}
if (showSize) {
for (int i = 0; i < rows.size() - 1; ++i) {
int h = rows.get(i + 1) - rows.get(i) + 1;
for (int j = 0; j < cols.size() - 1; ++j) {
int w = cols.get(j + 1) - cols.get(j) + 1;
Text text = new Text();
text.setX(cols.get(j) + w / 3);
text.setY(rows.get(i) + h / 3);
text.setFill(lineColor);
text.setText(w + "x" + h);
text.setFont(new javafx.scene.text.Font(lineWidth * 3.0));
group.getChildren().add(text);
}
}
}
Blend blend = new Blend(BlendMode.SRC_OVER);
blend.setBottomInput(new ImageInput(image));
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;
}
}
public static Image combineImagesColumnFx(List<Image> images,
Color bgColor, int interval, int Margin) {
try {
if (images == null || images.isEmpty()) {
return null;
}
Group group = new Group();
int x = Margin, y = Margin, width = 0, height = 0;
for (int i = 0; i < images.size(); ++i) {
Image image = images.get(i);
ImageView view = new ImageView(image);
view.setPreserveRatio(true);
view.setFitWidth(image.getWidth());
view.setFitHeight(image.getHeight());
view.setX(x);
view.setY(y);
group.getChildren().add(view);
x = Margin;
y += image.getHeight() + interval;
if (image.getWidth() > width) {
width = (int) image.getWidth();
}
}
width += 2 * Margin;
height = y + Margin - interval;
Blend blend = new Blend(BlendMode.SRC_OVER);
blend.setBottomInput(new ColorInput(0, 0, width, height, 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;
}
}
public static Image combineSingleColumnFx(List<Image> images) {
if (images == null || images.isEmpty()) {
return null;
}
try {
Group group = new Group();
double x = 0, y = 0, imageWidth, imageHeight;
double totalWidth = 0, totalHeight = 0;
for (Image theImage : images) {
ImageView view = new ImageView(theImage);
imageWidth = theImage.getWidth();
imageHeight = theImage.getHeight();
view.setPreserveRatio(true);
view.setX(x);
view.setY(y);
view.setFitWidth(imageWidth);
view.setFitHeight(imageHeight);
group.getChildren().add(view);
y += imageHeight;
if (imageWidth > totalWidth) {
totalWidth = imageWidth;
}
}
totalHeight = y;
Blend blend = new Blend(BlendMode.SRC_OVER);
blend.setBottomInput(new ColorInput(0, 0, totalWidth, totalHeight, Color.TRANSPARENT));
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;
}
}
public static Image addMarginsFx2(Image image, Color color, int MarginWidth,
boolean addTop, boolean addBottom, boolean addLeft, boolean addRight) {
try {
if (image == null || MarginWidth <= 0) {
return image;
}
Group group = new Group();
double imageWidth = image.getWidth(), imageHeight = image.getHeight();
double totalWidth = image.getWidth(), totalHeight = image.getHeight();
ImageView view = new ImageView(image);
view.setPreserveRatio(true);
view.setFitWidth(imageWidth);
view.setFitHeight(imageHeight);
if (addLeft) {
view.setX(MarginWidth);
totalWidth += MarginWidth;
} else {
view.setX(0);
}
if (addTop) {
view.setY(MarginWidth);
totalHeight += MarginWidth;
} else {
view.setY(0);
}
if (addBottom) {
totalHeight += MarginWidth;
}
if (addRight) {
totalWidth += MarginWidth;
}
group.getChildren().add(view);
Blend blend = new Blend(BlendMode.SRC_OVER);
blend.setBottomInput(new ColorInput(0, 0, totalWidth, totalHeight, color));
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 image;
}
}