Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有一个“问题”;填写「;javafx中任意形状的函数?_Java_Css_Javafx_Paint_Javafx 8 - Fatal编程技术网

是否有一个“问题”;填写「;javafx中任意形状的函数?

是否有一个“问题”;填写「;javafx中任意形状的函数?,java,css,javafx,paint,javafx-8,Java,Css,Javafx,Paint,Javafx 8,我需要知道使用JavaFX可以以何种方式为下面的图像(PNG)着色。此图像当前包含在JavaFX的ImageView中: 我想把区域1涂成蓝色,第二个涂成红色,最后两个涂成紫色。如何在JavaFX中实现这一点?难道没有Windows Paint中的某种功能吗?(你知道,在边界之间用颜色填充特定区域的绘画桶)。建议的方法 你可以使用一个算法 示例代码 import javafx.application.Application; import javafx.geometry.Insets; im

我需要知道使用JavaFX可以以何种方式为下面的图像(PNG)着色。此图像当前包含在JavaFX的ImageView中:


我想把区域1涂成蓝色,第二个涂成红色,最后两个涂成紫色。如何在JavaFX中实现这一点?难道没有Windows Paint中的某种功能吗?(你知道,在边界之间用颜色填充特定区域的绘画桶)。

建议的方法

你可以使用一个算法

示例代码

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Stack;

public class UnleashTheKraken extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) {
        Image original = new Image(
            "http://s12.postimg.org/wofhjvy2h/image_2.jpg"
        );

        WritableImage updateable = new WritableImage(
                original.getPixelReader(),
                (int) original.getWidth(),
                (int) original.getHeight()
        );

        Kraken kraken = new Kraken(updateable, Color.WHITE);
        kraken.unleash(new Point2D(40,   40), Color.BLUE);
        kraken.unleash(new Point2D(40,  100), Color.RED);
        kraken.unleash(new Point2D(100, 100), Color.GREEN);
        kraken.unleash(new Point2D(120,  40), Color.YELLOW);

        ImageView originalView = new ImageView(original);
        ImageView filledView   = new ImageView(updateable);

        HBox layout = new HBox(10, originalView, filledView);
        layout.setPadding(new Insets(10));
        stage.setScene(new Scene(layout));
        stage.show();
    }

    class Kraken {
        private final WritableImage image;
        private final Color colorToFill;

        // tolerance for color matching (on a scale of 0 to 1);
        private final double E = 0.3;

        public Kraken(WritableImage image, Color colorToFill) {
            this.image = image;
            this.colorToFill = colorToFill;
        }

        public void unleash(Point2D start, Color color) {
            PixelReader reader = image.getPixelReader();
            PixelWriter writer = image.getPixelWriter();

            Stack<Point2D> stack = new Stack<>();
            stack.push(start);

            while (!stack.isEmpty()) {
                Point2D point = stack.pop();
                int x = (int) point.getX();
                int y = (int) point.getY();
                if (filled(reader, x, y)) {
                    continue;
                }

                writer.setColor(x, y, color);

                push(stack, x - 1, y - 1);
                push(stack, x - 1, y    );
                push(stack, x - 1, y + 1);
                push(stack, x    , y + 1);
                push(stack, x + 1, y + 1);
                push(stack, x + 1, y    );
                push(stack, x + 1, y - 1);
                push(stack, x,     y - 1);
            }
        }

        private void push(Stack<Point2D> stack, int x, int y) {
            if (x < 0 || x > image.getWidth() ||
                y < 0 || y > image.getHeight()) {
                return;
            }

            stack.push(new Point2D(x, y));
        }

        private boolean filled(PixelReader reader, int x, int y) {
            Color color = reader.getColor(x, y);

            return !withinTolerance(color, colorToFill, E);
        }

        private boolean withinTolerance(Color a, Color b, double epsilon) {
            return
                    withinTolerance(a.getRed(),   b.getRed(),   epsilon) &&
                    withinTolerance(a.getGreen(), b.getGreen(), epsilon) &&
                    withinTolerance(a.getBlue(),  b.getBlue(),  epsilon);
        }

        private boolean withinTolerance(double a, double b, double epsilon) {
            return Math.abs(a - b) < epsilon;
        }
    }
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.geometry.Point2D;
导入javafx.scene.scene;
导入javafx.scene.image.*;
导入javafx.scene.layout.HBox;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入java.util.Stack;
公开类Kraken扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共作废开始(最后阶段){
原始图像=新图像(
"http://s12.postimg.org/wofhjvy2h/image_2.jpg"
);
WritableImage updateable=新的WritableImage(
original.getPixelReader(),
(int)original.getWidth(),
(int)original.getHeight()
);
Kraken Kraken=新的Kraken(可更新,颜色为白色);
海怪。释放(新点2D(40,40),颜色。蓝色);
海怪。释放(新点2D(40100),颜色。红色);
海怪。释放(新点2D(100100),颜色。绿色);
海怪。释放(新点2D(120,40),颜色。黄色);
ImageView originalView=新的ImageView(原始);
ImageView Filled视图=新的ImageView(可更新);
HBox布局=新HBox(10,原始视图,填充视图);
布局。设置填充(新插图(10));
舞台场景(新场景(布局));
stage.show();
}
海怪类{
私有最终可写映像;
私人最终颜色彩色填充;
//颜色匹配公差(以0到1为刻度);
私人最终双E=0.3;
公共海怪(可写图像,彩色填充){
这个图像=图像;
this.colorToFill=colorToFill;
}
公共无效释放(Point2D开始,彩色){
PixelReader=image.getPixelReader();
PixelWriter=image.getPixelWriter();
堆栈=新堆栈();
堆栈推送(启动);
而(!stack.isEmpty()){
Point2D point=stack.pop();
int x=(int)point.getX();
int y=(int)point.getY();
if(已填写(读卡器,x,y)){
继续;
}
writer.setColor(x,y,color);
推(堆栈,x-1,y-1);
推(堆栈,x-1,y);
推(堆栈,x-1,y+1);
推送(堆栈,x,y+1);
推送(堆栈,x+1,y+1);
推(堆栈,x+1,y);
推送(堆栈,x+1,y-1);
推(堆栈,x,y-1);
}
}
私有无效推送(堆栈,整数x,整数y){
if(x<0 | | x>image.getWidth()||
y<0 | | y>image.getHeight(){
返回;
}
stack.push(新点2d(x,y));
}
专用布尔填充(像素读取器,int x,int y){
Color=reader.getColor(x,y);
返回!带有不容忍(颜色、颜色填充、E);
}
私有布尔值(颜色a、颜色b、双ε){
返回
不容忍(a.getRed(),b.getRed(),epsilon)&&
不容忍(a.getGreen(),b.getGreen(),epsilon)&&
不耐受(a.getBlue(),b.getBlue(),epsilon);
}
私有布尔值(双a、双b、双ε){
返回Math.abs(a-b)
其他问题的答案


但是图像不是逐像素着色吗

是的,这就是重点,你需要对像素进行着色。位图显示的计算机图形学中的一切最终都归结为着色像素

这是一种有效的着色方法吗

在您提供的示例图像上,它是即时的(据我所知)。就空间而言,它会占用一些内存,但所有这些算法都会使用内存。我提供的示例代码不是可以设计的最有效的泛洪填充着色算法(时间或空间)。我链接的维基百科页面有其他更有效(也更复杂)的算法,如果你需要的话可以应用

替代方法


如果每个区域都有剪切模具形状,则可以堆叠模具并对其应用效果(例如在:)。颜色调整(可能)是硬件加速效果。不过,这种替代方法不是通用方法,因为它要求您了解模具形状。

建议的方法

Shape circle = new Circle(x,y,r);
Shape rect = new Rectangle(x,y,w,h);
Shape region1 = Shape.subtract(circle, rect);// to "cut" the rect away from a circle.
// You'll need to do this twice for each piece.
region1 = Shape.subtract(region1,anotherRect); 
region1.setFill(Color.BLUE);
// Then simply add your shape to a node and set it's translation.
你可以使用一个算法

示例代码

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.image.*;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Stack;

public class UnleashTheKraken extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage stage) {
        Image original = new Image(
            "http://s12.postimg.org/wofhjvy2h/image_2.jpg"
        );

        WritableImage updateable = new WritableImage(
                original.getPixelReader(),
                (int) original.getWidth(),
                (int) original.getHeight()
        );

        Kraken kraken = new Kraken(updateable, Color.WHITE);
        kraken.unleash(new Point2D(40,   40), Color.BLUE);
        kraken.unleash(new Point2D(40,  100), Color.RED);
        kraken.unleash(new Point2D(100, 100), Color.GREEN);
        kraken.unleash(new Point2D(120,  40), Color.YELLOW);

        ImageView originalView = new ImageView(original);
        ImageView filledView   = new ImageView(updateable);

        HBox layout = new HBox(10, originalView, filledView);
        layout.setPadding(new Insets(10));
        stage.setScene(new Scene(layout));
        stage.show();
    }

    class Kraken {
        private final WritableImage image;
        private final Color colorToFill;

        // tolerance for color matching (on a scale of 0 to 1);
        private final double E = 0.3;

        public Kraken(WritableImage image, Color colorToFill) {
            this.image = image;
            this.colorToFill = colorToFill;
        }

        public void unleash(Point2D start, Color color) {
            PixelReader reader = image.getPixelReader();
            PixelWriter writer = image.getPixelWriter();

            Stack<Point2D> stack = new Stack<>();
            stack.push(start);

            while (!stack.isEmpty()) {
                Point2D point = stack.pop();
                int x = (int) point.getX();
                int y = (int) point.getY();
                if (filled(reader, x, y)) {
                    continue;
                }

                writer.setColor(x, y, color);

                push(stack, x - 1, y - 1);
                push(stack, x - 1, y    );
                push(stack, x - 1, y + 1);
                push(stack, x    , y + 1);
                push(stack, x + 1, y + 1);
                push(stack, x + 1, y    );
                push(stack, x + 1, y - 1);
                push(stack, x,     y - 1);
            }
        }

        private void push(Stack<Point2D> stack, int x, int y) {
            if (x < 0 || x > image.getWidth() ||
                y < 0 || y > image.getHeight()) {
                return;
            }

            stack.push(new Point2D(x, y));
        }

        private boolean filled(PixelReader reader, int x, int y) {
            Color color = reader.getColor(x, y);

            return !withinTolerance(color, colorToFill, E);
        }

        private boolean withinTolerance(Color a, Color b, double epsilon) {
            return
                    withinTolerance(a.getRed(),   b.getRed(),   epsilon) &&
                    withinTolerance(a.getGreen(), b.getGreen(), epsilon) &&
                    withinTolerance(a.getBlue(),  b.getBlue(),  epsilon);
        }

        private boolean withinTolerance(double a, double b, double epsilon) {
            return Math.abs(a - b) < epsilon;
        }
    }
}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.geometry.Point2D;
导入javafx.scene.scene;
导入javafx.scene.image.*;
导入javafx.scene.layout.HBox;
导入javafx.scene.paint.Color;
导入javafx.stage.stage;
导入java.util.Stack;
公开类Kraken扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共作废开始(最后阶段){
原始图像=新图像(
"http://s12.postimg.org/wofhjvy2h/image_2.jpg"
);
WritableImage updateable=新的WritableImage(
original.getPixelReader(),