Revision 8:049008e1cee3

View differences:

src/main/java/cz/agents/alite/visualizer2d/demo/AngleProvider.java
1 1
package cz.agents.alite.visualizer2d.demo;
2 2

  
3 3
import javax.vecmath.Point2d;
4
import java.util.List;
4 5
import java.util.Timer;
5 6
import java.util.TimerTask;
7
import java.util.stream.Collectors;
6 8
import java.util.stream.IntStream;
7 9
import java.util.stream.Stream;
8 10

  
......
54 56
        final double angle1 = angle;
55 57
        return IntStream.rangeClosed(1, 10).mapToDouble(i -> i*10).mapToObj(radius -> getPoint(radius, angle1));
56 58
    }
59

  
60
    public List<Point2d> getSquare() {
61
        final double angle1 = angle;
62
        return IntStream.rangeClosed(0, 4).mapToDouble(i -> i*Math.PI/2d)
63
                .mapToObj(deltaAngle -> getPoint(RADIUS, angle1 + deltaAngle)).collect(Collectors.toList());
64
    }
57 65
}
src/main/java/cz/agents/alite/visualizer2d/demo/Demo.java
7 7
import cz.agents.alite.visualizer2d.layers.GroupLayer;
8 8
import cz.agents.alite.visualizer2d.layers.VisInfoLayer;
9 9
import cz.agents.alite.visualizer2d.layers.elements.ElementsLayer;
10
import cz.agents.alite.visualizer2d.layers.elements.LineElement;
10 11
import cz.agents.alite.visualizer2d.layers.elements.PointElement;
11 12

  
12 13
import javax.swing.*;
......
26 27
        GroupLayer<Point2d> layers = GroupLayer.create();
27 28
        layers.addLayer(ColorLayer.create(Color.BLACK));
28 29
        layers.addLayer(ElementsLayer.create(() -> Arrays.asList(
29
                        new PointElement<>(new Point2d(AngleProvider.TRANS_X, AngleProvider.TRANS_Y), 3, Color.RED),
30
                        new PointElement<>(angleProvider.getPoint(), 3, Color.WHITE),
31
                        new PointElement<>(new Point2d(AngleProvider.TRANS_X + 25, AngleProvider.TRANS_Y), 3, Color.GREEN))
30
                        PointElement.get(3, Color.RED, new Point2d(AngleProvider.TRANS_X, AngleProvider.TRANS_Y)),
31
                        PointElement.get(3, Color.WHITE, angleProvider.getPoint()),
32
                        PointElement.get(3, Color.GREEN, new Point2d(AngleProvider.TRANS_X + 25, AngleProvider.TRANS_Y)))
32 33
        ));
33 34
        layers.addLayer(ElementsLayer.create(PointElement.get(3, Color.BLUE, angleProvider::getPointsLine)));
35
        layers.addLayer(ElementsLayer.create(() -> LineElement.get(Color.GREEN, 2, angleProvider.getSquare())));
34 36
        VisInfoLayer visInfoLayer = VisInfoLayer.create();
35 37
        //GroupLayer draggable = DraggableLayer.create(visInfoLayer);
36 38
        layers.addLayer(visInfoLayer);
src/main/java/cz/agents/alite/visualizer2d/layers/elements/Element.java
12 12

  
13 13
    public void paint(VisInfo visInfo, Graphics2D canvas, Point2dAdapter<T> adapter);
14 14

  
15
    public static <T> Element<T> empty() {
16
        return (visInfo, canvas, adapter) -> {};
17
    }
18

  
15 19
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/Elements.java
6 6
@FunctionalInterface
7 7
public interface Elements<T> {
8 8

  
9
    public Iterable<Element<T>> get();
9
    public Iterable<? extends Element<T>> get();
10 10

  
11 11
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/ElementsLayer.java
7 7
import java.awt.*;
8 8
import java.util.Arrays;
9 9
import java.util.List;
10
import java.util.function.Supplier;
11
import java.util.stream.Stream;
10 12
import java.util.stream.StreamSupport;
11 13

  
12 14
/**
......
20 22
        return create(() -> pointList);
21 23
    }
22 24

  
25
    public static <T> Layer<T> create(Supplier<Element<T>> supplier) {
26
        return create(() -> Stream.of(supplier.get()));
27
    }
28

  
23 29
    public static <T> Layer<T> create(Elements<T> elements) {
24
        return new ElementsLayer<>(() -> StreamSupport.stream(elements.get().spliterator(), false));
30
        ElementsStream<T> out = () -> StreamSupport.stream(elements.get().spliterator(), false);
31
        return new ElementsLayer<>(out);
25 32
    }
26 33

  
27 34
    public static <T> Layer<T> create(ElementsStream<T> elements) {
src/main/java/cz/agents/alite/visualizer2d/layers/elements/ElementsStream.java
8 8
@FunctionalInterface
9 9
public interface ElementsStream<T> {
10 10

  
11
    public Stream<Element<T>> get();
11
    public Stream<? extends Element<T>> get();
12 12

  
13 13
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/LineElement.java
1
package cz.agents.alite.visualizer2d.layers.elements;
2

  
3
import cz.agents.alite.visualizer2d.Point2dAdapter;
4
import cz.agents.alite.visualizer2d.VisInfo;
5

  
6
import javax.vecmath.Point2d;
7
import java.awt.*;
8
import java.awt.geom.Path2D;
9
import java.util.Arrays;
10
import java.util.Collections;
11
import java.util.List;
12
import java.util.function.Supplier;
13
import java.util.stream.Stream;
14

  
15
/**
16
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
17
 */
18
public class LineElement<T> implements Element<T> {
19

  
20
    public static <T> LineElement<T> get(Color color, float lineThickness, T from, T to) {
21
        return new LineElement<>(color, lineThickness, from, to, Collections.EMPTY_LIST);
22
    }
23

  
24
    public static <T> LineElement<T> get(Color color, float lineThickness, T from, T to, T... next) {
25
        return new LineElement<>(color, lineThickness, from, to, (Iterable<T>) Arrays.asList(next).iterator());
26
    }
27

  
28
    public static <T> LineElement<T> get(Color color, float lineThickness, T from, T to, Iterable<T> next) {
29
        return new LineElement<>(color, lineThickness, from, to, next);
30
    }
31

  
32
    public static <T> Element<T> get(Color color, float lineThickness, List<T> list) {
33
        int size = list.size();
34
        if (size < 2) return Element.empty();
35
        else if (size == 2) return get(color, lineThickness, list.get(0), list.get(1));
36
        else return get(color, lineThickness, list.get(0), list.get(1), list.subList(2, size));
37
    }
38

  
39
    public static <T> ElementsStream<T> get(Color color, float lineThickness, Supplier<Stream<List<T>>> points) {
40
        return () -> points.get().map(list -> get(color, lineThickness, list));
41
    }
42

  
43
    private final Color lineColor;
44
    private final float lineThickness;
45
    private final T first, second;
46
    private final Iterable<T> next;
47

  
48
    private LineElement(Color lineColor, float lineThickness, T first, T second, Iterable<T> next) {
49
        this.lineColor = lineColor;
50
        this.lineThickness = lineThickness;
51
        this.first = first;
52
        this.second = second;
53
        this.next = next;
54
    }
55

  
56
    @Override
57
    public void paint(VisInfo visInfo, Graphics2D canvas, Point2dAdapter<T> adapter) {
58

  
59
        Path2D.Double path = new Path2D.Double();
60
        Point2d firstConverted = adapter.convert(first);
61
        Point2d secondConverted = adapter.convert(second);
62
        path.moveTo(firstConverted.x, firstConverted.y);
63
        path.lineTo(secondConverted.x, secondConverted.y);
64
        next.forEach(t -> {
65
            final Point2d point = adapter.convert(t);
66
            path.lineTo(point.x, point.y);
67
        });
68

  
69

  
70
        canvas.setColor(lineColor);
71
        //Stroke stroke = new BasicStroke(10);
72
        //canvas.setStroke(stroke);
73
        canvas.draw(path);
74
    }
75

  
76

  
77

  
78
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/PointElement.java
14 14
 */
15 15
public class PointElement<T> implements Element<T> {
16 16

  
17
    public static <T> PointElement<T> get(double radius, Color color, T position) {
18
        return new PointElement<>(position, radius, color);
19
    }
20

  
17 21
    public static <T> ElementsStream<T> get(double radius, Color color, Supplier<Stream<T>> positionSupplirer) {
18 22
        return () -> positionSupplirer.get().map(position -> new PointElement<>(position, radius, color));
19 23
    }
......
22 26
    private final double radius;
23 27
    private final Color color;
24 28

  
25
    public PointElement(T centerPosition, double radius, Color color) {
29
    private PointElement(T centerPosition, double radius, Color color) {
26 30
        this.centerPosition = centerPosition;
27 31
        this.radius = radius;
28 32
        this.color = color;

Also available in: Unified diff