So Im still learning Java. Now Im learning JavaFX.
所以我还在学习Java。现在我正在学习JavaFX。
I have a picture of a tree. And I want to try two different method. the first method I use was using unary operator to turn the image colour to grey.
我有一张树的照片。我想尝试两种不同的方法。我使用的第一种方法是使用一元运算符将图像颜色变为灰色。
Now I want to try a second method using the ColourTransformer
interface that I made to get a 10 pixel wide gray frame replacing the pixels on the border of an image.
现在我想尝试使用ColourTransformer接口的第二种方法来获得一个10像素宽的灰色框架来替换图像边框上的像素。
This is what I have done. for the second method, im not quite sure how to specify the pixel. Any suggestions?
这就是我所做的。对于第二种方法,我不太清楚如何指定像素。有什么建议?
this is what I have done
这就是我所做的
public class ColourFilter extends Application {
//Using Unary Operator to transform image to grayscale - Method 1
public static Image transform(Image in, UnaryOperator<Color> f) {
int width = (int) in.getWidth();
int height = (int) in.getHeight();
WritableImage out = new WritableImage(
width, height);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
out.getPixelWriter().setColor(x, y,
f.apply(in.getPixelReader().getColor(x, y)));
return out;
}
public static <T> UnaryOperator<T> compose(UnaryOperator<T> op1, UnaryOperator<T> op2) {
return t -> op2.apply(op1.apply(t));
}
//Using ColourTransformer interface to get 10 pixel wide gray frame replacing the pixels on the border of an image - Method 2
public static Image transform(Image in, ColourTransformer f) {
int width = (int) in.getWidth();
int height = (int) in.getHeight();
WritableImage out = new WritableImage(
width, height);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
out.getPixelWriter().setColor(x, y, f.apply(x, y, in.getPixelReader().getColor(x, y)));
return out;
}
@FunctionalInterface
interface ColourTransformer {
Color apply(int x, int y, Color colorAtXY);
}
public void start(Stage stage) {
Image image = new Image("amazing-trees.jpg");
Image image2 = transform(image, Color::brighter);
Image image3 = transform(image2, Color::grayscale);
// alternative to two previous image transforms -- composition
//Image image3 = transform(image, compose(Color::brighter, Color::grayscale));
stage.setScene(new Scene(new VBox(
new ImageView(image),
// new ImageView(image2),
new ImageView(image3))));
stage.show();
}
}
1 个解决方案
#1
1
As the compile error message says, you are trying to call
正如编译错误消息所示,您正在尝试调用
f.apply(Color);
where f
is a ColourTransformer
: however you defined the apply
method in ColorTransformer
with three parameters: apply(int, int, Color)
.
其中f是ColourTransformer:但是你在ColorTransformer中使用三个参数定义了apply方法:apply(int,int,Color)。
You need to replace
你需要更换
f.apply(in.getPixelReader().getColor(x, y))
with
f.apply(x, y, in.getPixelReader().getColor(x, y))
i.e.
public static Image transform(Image in, ColourTransformer f) {
int width = (int) in.getWidth();
int height = (int) in.getHeight();
WritableImage out = new WritableImage(
width, height);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
out.getPixelWriter().setColor(x, y, f.apply(x, y, in.getPixelReader().getColor(x, y)));
return out;
}
You can get the 10-pixel gray border by doing:
你可以通过以下方式获得10像素的灰色边框:
Image image = new Image("amazing-trees.jpg");
int width = (int) image.getWidth();
int height = (int) image.getHeight();
Image framedImage = transform(image, (x, y, color) -> {
if (x < 10 || y < 10 || width - x < 10 || height - y < 10) {
return Color.GRAY ;
} else return color ;
});
#1
1
As the compile error message says, you are trying to call
正如编译错误消息所示,您正在尝试调用
f.apply(Color);
where f
is a ColourTransformer
: however you defined the apply
method in ColorTransformer
with three parameters: apply(int, int, Color)
.
其中f是ColourTransformer:但是你在ColorTransformer中使用三个参数定义了apply方法:apply(int,int,Color)。
You need to replace
你需要更换
f.apply(in.getPixelReader().getColor(x, y))
with
f.apply(x, y, in.getPixelReader().getColor(x, y))
i.e.
public static Image transform(Image in, ColourTransformer f) {
int width = (int) in.getWidth();
int height = (int) in.getHeight();
WritableImage out = new WritableImage(
width, height);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
out.getPixelWriter().setColor(x, y, f.apply(x, y, in.getPixelReader().getColor(x, y)));
return out;
}
You can get the 10-pixel gray border by doing:
你可以通过以下方式获得10像素的灰色边框:
Image image = new Image("amazing-trees.jpg");
int width = (int) image.getWidth();
int height = (int) image.getHeight();
Image framedImage = transform(image, (x, y, color) -> {
if (x < 10 || y < 10 || width - x < 10 || height - y < 10) {
return Color.GRAY ;
} else return color ;
});