【问题标题】:Change shade of color of each shape改变每个形状的颜色深浅
【发布时间】:2019-03-11 19:47:15
【问题描述】:

我有以下图像,我需要将每个形状的颜色更改为绿色,但每个形状都可以有不同的绿色阴影。我不确定如何做到这一点。我知道如何通过更改像素颜色而不是图像的一部分来处理整个图像。

我对 javafx 很陌生,所以请记住这一点。

【问题讨论】:

  • 我不清楚“不同形状的绿色”这个词。我也有点不清楚这个问题。你能解释清楚你实际上想要做什么吗?是在写图像吗?如果可能,请发布您迄今为止尝试过的代码。这样我们就可以快速了解您正在尝试的内容。
  • 我想 OP 想让每个盒子都有不同的绿色 阴影(类型、变体等)。他/她知道如何为整个图像重新着色,但不知道如何将着色限制在某个区域(例如泛洪填充算法)
  • 这是正确的,马特,为错字道歉。我希望每个矩形都有不同的绿色阴影。

标签: java javafx colors pixel-shader


【解决方案1】:

实现这一目标的最简单方法是自己生成图像并单独添加每个矩形。在这种情况下,您可以独立设置每个矩形的颜色:

public class MyApp extends Application {
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 430, 230, Color.WHITE);

        List<Rectangle> rectangles = new ArrayList<>();

        int width = 100;
        int height = 50;
        int pad = 6;

        // first row
        rectangles.add(createRect(pad, pad, width, height, Color.GREEN));
        rectangles.add(createRect(pad + (width + pad), pad, width, height, Color.GREEN.brighter()));
        rectangles.add(createRect(pad + (width + pad) * 2, pad, width, height, Color.GREEN.darker()));
        rectangles.add(createRect(pad + (width + pad) * 3, pad, width, height, Color.GREEN));

        // second row
        rectangles.add(createRect(pad + (width + pad) * 0.5, pad + (height + pad), width, height, Color.GREEN.brighter()));
        rectangles.add(createRect(pad + (width + pad) * 1.5, pad + (height + pad), width, height, Color.GREEN));
        rectangles.add(createRect(pad + (width + pad) * 2.5, pad + (height + pad), width, height, Color.GREEN.darker()));

        // third row
        rectangles.add(createRect(pad + (width + pad), pad + (height + pad) * 2, width, height, Color.GREEN.darker()));
        rectangles.add(createRect(pad + (width + pad) * 2, pad + (height + pad) * 2, width, height, Color.GREEN));

        // last row
        rectangles.add(createRect(pad + (width + pad) * 1.5, pad + (height + pad) * 3, width, height, Color.GREEN.brighter()));

        root.getChildren().addAll(rectangles);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private Rectangle createRect(double x, double y, double width, double height, Color color) {
        Rectangle rectangle = new Rectangle(x, y, width, height);
        rectangle.setStroke(Color.BLACK);
        rectangle.setFill(color);
        return rectangle;
    }
}

结果如下:

在我的示例中,我只使用了三种不同的绿色阴影,但您可以自行设置每个矩形的颜色并创建渐变或任何您喜欢的颜色。只需将Color.GREEN 替换为new Color(0.0f, 0.7f, 0.0f, 1f) 并根据需要调整参数。

希望对您有所帮助。

编辑:

如果你想使用生成的Node作为图片,可以使用Node.snapshot方法创建图片:

WritableImage snapshot = root.snapshot(new SnapshotParameters(), null);

如果你想用图像替换原来的矩形,你可以使用以下:

root.getChildren().clear();
ImageView imageView = new ImageView(snapshot);
imageView.setX(pad);
imageView.setY(pad);
root.getChildren().add(imageView);

要将图像另存为文件,您可以使用:

ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "PNG", new File("/path/to/image.png"));

【讨论】:

  • 非常感谢您,但是有没有办法对 image.png 进行处理?还是会使事情复杂化?
  • 将根节点导出为图像很容易。我将那部分添加到我的答案中。如果您想使用图像作为输入,那么事情会变得更加复杂。在这种情况下,您必须使用图像处理方法。
  • 我明白了,非常感谢您的帮助,非常感谢。
猜你喜欢
  • 2019-12-24
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多