Friday, June 6, 2014

Draw ArrayList of Path on canvas

This example implement a ArrayList of Path, to store Paths. Then draw on canvas using GraphicsContext.


package javafxdrawpatharray;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXDrawPathArray extends Application {
    
    List<Path> pathList;
    
    @Override
    public void start(Stage primaryStage) {
        Canvas canvas = new Canvas(300, 250);
        GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
        
        initPathList();
        drawSomething(graphicsContext);
         
        StackPane root = new StackPane();
        root.getChildren().add(canvas);
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    private void initPathList(){
        pathList = new ArrayList<>();
        
        Path p = new Path();
        p.getElements().add(new MoveTo(50, 50));
        p.getElements().add(new LineTo(50, 200));
        p.getElements().add(new LineTo(100, 200));
        p.getElements().add(new LineTo(100, 100));
        p.getElements().add(new LineTo(200, 100));
        pathList.add(p);
        
        p = new Path();
        p.getElements().add(new MoveTo(60, 60));
        p.getElements().add(new LineTo(60, 210));
        p.getElements().add(new LineTo(110, 210));
        p.getElements().add(new LineTo(110, 110));
        p.getElements().add(new LineTo(210, 110));
        pathList.add(p);  
        
        p = new Path();
        p.getElements().add(new MoveTo(70, 70));
        p.getElements().add(new LineTo(70, 220));
        p.getElements().add(new LineTo(120, 220));
        p.getElements().add(new LineTo(120, 120));
        p.getElements().add(new LineTo(220, 120));
        pathList.add(p); 
    }
    
    private void drawSomething(GraphicsContext gc){
        
        gc.setFill(Color.RED);
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(1);

        for(Path p : pathList){
            
            ObservableList<PathElement> l = p.getElements();
            
            gc.beginPath();
            for (PathElement pe : l){
                if(pe.getClass()==MoveTo.class){
                    gc.moveTo(((MoveTo)pe).getX(), ((MoveTo)pe).getY());
                }else if(pe.getClass()==LineTo.class){
                    gc.lineTo(((LineTo)pe).getX(), ((LineTo)pe).getY());
                }
            }
            gc.stroke();
            gc.closePath();
        }
    }
    
}

Use functional operation version:
package javafxdrawpatharray2;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.PathElement;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFXDrawPathArray2 extends Application {
    
    List<Path> pathList;
    
    @Override
    public void start(Stage primaryStage) {
        Canvas canvas = new Canvas(300, 250);
        GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
        
        initPathList();
        drawSomething(graphicsContext);
         
        StackPane root = new StackPane();
        root.getChildren().add(canvas);
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
    
    private void initPathList(){
        pathList = new ArrayList<>();
        
        Path p = new Path();
        p.getElements().add(new MoveTo(50, 50));
        p.getElements().add(new LineTo(50, 200));
        p.getElements().add(new LineTo(100, 200));
        p.getElements().add(new LineTo(100, 100));
        p.getElements().add(new LineTo(200, 100));
        pathList.add(p);
        
        p = new Path();
        p.getElements().add(new MoveTo(60, 60));
        p.getElements().add(new LineTo(60, 210));
        p.getElements().add(new LineTo(110, 210));
        p.getElements().add(new LineTo(110, 110));
        p.getElements().add(new LineTo(210, 110));
        pathList.add(p);  
        
        p = new Path();
        p.getElements().add(new MoveTo(70, 70));
        p.getElements().add(new LineTo(70, 220));
        p.getElements().add(new LineTo(120, 220));
        p.getElements().add(new LineTo(120, 120));
        p.getElements().add(new LineTo(220, 120));
        pathList.add(p); 
    }
    
    private void drawSomething(GraphicsContext gc){
        
        gc.setFill(Color.RED);
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(1);

        pathList.stream().map((p) -> p.getElements()).map((l) -> {
            gc.beginPath();
            l.stream().forEach((pe) -> {
                if(pe.getClass()==MoveTo.class){
                    gc.moveTo(((MoveTo)pe).getX(), ((MoveTo)pe).getY());
                }else if(pe.getClass()==LineTo.class){
                    gc.lineTo(((LineTo)pe).getX(), ((LineTo)pe).getY());
                }
            });
            return l;
        }).map((_item) -> {
            gc.stroke();
            return _item;
        }).forEach((_item) -> {
            gc.closePath();
        });
    }
    
}


No comments:

Post a Comment