【问题标题】:Flood fill using java Pane - JAVA使用 Java 窗格进行洪水填充 - JAVA
【发布时间】:2016-05-20 10:10:11
【问题描述】:

您好,我应该创建一个函数,该函数将使用 Java 在包含 ShapesPane 上进行填充。它应该表现得像 MSPaint,我不需要稍后移动矩形线条或其他形状。我正在考虑将窗格转换为图像,然后使用像素,然后清除所有窗格子并将其作为图像插入,但我无法使其工作。 代码示例:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Paint extends Application {


public static void main(String[] args) {
    launch(args);
}

private Pane pane;


@Override
public void start(Stage primaryStage) {
    pane= new Pane();

    primaryStage.setTitle("Fill");
    Scene scene = new Scene(pane,500,600);
    primaryStage.setScene(scene);
    primaryStage.show();


    pane.setOnMousePressed((e)->{
        doFill(e.getX(),e.getY());
    });

    //RECT 1
    Rectangle rect1=new Rectangle(1,100,200,300);
    rect1.setStroke(Color.BLACK);
    rect1.setStrokeWidth(2);
    rect1.setFill(Color.WHITE);

  //RECT 2
    Rectangle rect2=new Rectangle(50,150,200,400);
    rect2.setStroke(Color.BLACK);
    rect2.setStrokeWidth(2);
    rect2.setFill(Color.WHITE);

    //LINE
    Line line=new Line(0,0,200,550);
    rect2.setStroke(Color.BLACK);
    rect2.setStrokeWidth(2);


    pane.getChildren().addAll(rect1,rect2,line);
}

private void doFill(double eventX, double eventY){
    //**TODO**
}

}

【问题讨论】:

  • 填充?这就是全部?不要过度劳累
  • 好吧,我希望至少有人会告诉我将画布转换为图像的功能,您可以编辑并添加到窗格中...您不必对尝试学习某些东西的人生气。 ..
  • 这个问题有点类似于:Is there a “fill” function for arbitrary shapes in javafx? 请注意,像这样的任意填充通常与基于位图的系统(如 Canvas)比基于矢量的系统(如场景图)更密切相关(即您有将填充区域转移到场景图节点的额外问题的部分原因,对此我没有解决方案的建议)。

标签: java eclipse javafx flood-fill pane


【解决方案1】:

尽管有点混乱,但还是设法完成了该功能。因此,对于为此感到焦虑的每个人:

    private void doFill(double eventX, double eventY,boolean b){

    WritableImage i=pane.snapshot(new SnapshotParameters(), null);
    ArrayList<Integer> pozicie=new ArrayList<Integer>();
    ArrayList<Character> strany=new ArrayList<Character>();
    pozicie.add((int)eventX);
    pozicie.add((int)eventY);
    int c=i.getPixelReader().getColor((int)eventX,(int)eventY).hashCode();
    if(c==usedColor.hashCode()){
        //System.out.println("same color");

        return;}
    strany.add('a');


    while(pozicie.size()!=0){
        char strana=strany.remove(0);
        int x=pozicie.remove(0);
        int y=pozicie.remove(0);
        i.getPixelWriter().setColor(x, y, usedColor);
        if(strana=='d'){
            //iba dole
            if(y<pane.getHeight()-2 && i.getPixelReader().getColor(x, y+1).hashCode()==c){
                pozicie.add(x);
                pozicie.add(y+1);
                strany.add('d');
            }
        }
        else if(strana=='u'){
            //iba hore
            if( y>100 && i.getPixelReader().getColor(x, y-1).hashCode()==c){
                pozicie.add(x);
                pozicie.add(y-1);
                strany.add('u');
            }
        }
        else{


        if(x>2 && i.getPixelReader().getColor(x-1, y).hashCode()==c){
            pozicie.add(x-1);
            pozicie.add(y);
            strany.add('l');
        }
        if(x<pane.getWidth()-2 && i.getPixelReader().getColor(x+1, y).hashCode()==c){
            pozicie.add(x+1);
            pozicie.add(y);
            strany.add('r');
        }
        if( y>101 && i.getPixelReader().getColor(x, y-1).hashCode()==c){
            pozicie.add(x);
            pozicie.add(y-1);
            strany.add('u');
        }
        if(y<pane.getHeight()-2 && i.getPixelReader().getColor(x, y+1).hashCode()==c){
            pozicie.add(x);
            pozicie.add(y+1);
            strany.add('d');
        }
        }
    }
    pane.getChildren().clear();
    pane.getChildren().add(new ImageView(i));

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多