Revision 7:d7ec22964605

View differences:

src/main/java/cz/agents/alite/visualizer2d/VisPane.java
10 10
import java.awt.geom.NoninvertibleTransformException;
11 11
import java.awt.geom.Point2D;
12 12
import java.awt.image.BufferStrategy;
13
import java.util.Optional;
14 13
import java.util.concurrent.ScheduledFuture;
15 14
import java.util.concurrent.ScheduledThreadPoolExecutor;
16 15
import java.util.concurrent.TimeUnit;
......
27 26
    //TODO solve volatile vs synchronized
28 27
    private volatile Point2dAdapter<T> adapter;
29 28
    private volatile Point mousePosition = new Point();
30
    private volatile Optional<VisInfo> visCanvas = Optional.empty();
31 29

  
32 30
    private boolean running = false;
33 31
    private ScheduledFuture<?> updateFuture;
......
108 106
        }
109 107
    }
110 108

  
111
    public Optional<VisInfo> getVisCanvas() {
112
        return visCanvas;
113
    }
114

  
115 109
    public final void translate(int deltaX, int deltaY) {
116 110
        final Point2D deltaScreen = new Point2D.Double(deltaX, deltaY);
117 111

  
......
136 130
                transform.inverseTransform(scaleCenterScreen, presScale);
137 131
                transform.scale(multiplyFactor, multiplyFactor);
138 132
                transform.inverseTransform(scaleCenterScreen, postScale);
139
                transform.translate(postScale.getX()-presScale.getX(), postScale.getY()-presScale.getY());
133
                transform.translate(postScale.getX() - presScale.getX(), postScale.getY() - presScale.getY());
140 134
                zoom *= multiplyFactor;
141 135
            } catch (NoninvertibleTransformException e) {
142 136
                e.printStackTrace();
......
164 158
        if (canvas.isDisplayable()) {
165 159
            reinitializeBuffers();
166 160

  
167
            AffineTransform originalTransform = graphics.getTransform();
168
            AffineTransform affineTransform = calculateAffineTransform();
169
            visCanvas = Optional.of(new VisInfo(affineTransform, graphics, panel.getBounds(), mousePosition, rotationDeg, zoom));
161
            AffineTransform screenTransform = graphics.getTransform();
162
            AffineTransform worldTransform = getWorldTransform();
163
            VisInfo visInfo = new VisInfo(worldTransform, graphics, panel.getBounds(), mousePosition, rotationDeg, zoom);
170 164

  
171
            layer.paint(visCanvas.get(), adapter);
165
            layer.paint(visInfo, adapter);
172 166

  
173
            graphics.setTransform(originalTransform);
167
            graphics.setTransform(screenTransform);
174 168
            strategy.show();
175 169
        }
176 170
    }
......
192 186
        return false;
193 187
    }
194 188

  
195
    private AffineTransform calculateAffineTransform() {
189
    private AffineTransform getWorldTransform() {
196 190
        synchronized (transform) {
197 191
            return new AffineTransform(transform);
198 192
        }
src/main/java/cz/agents/alite/visualizer2d/demo/AngleProvider.java
3 3
import javax.vecmath.Point2d;
4 4
import java.util.Timer;
5 5
import java.util.TimerTask;
6
import java.util.stream.IntStream;
7
import java.util.stream.Stream;
6 8

  
7 9
/**
8 10
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
......
33 35

  
34 36
    private void updatePoint() {
35 37
        final double angle1 = angle;
36
        point = new Point2d(TRANS_X + RADIUS * Math.sin(angle1), TRANS_Y + RADIUS * Math.cos(angle1));
38
        point = getPoint(RADIUS, angle1);
39
    }
40

  
41
    private Point2d getPoint(double radius, double angle1) {
42
        return new Point2d(TRANS_X + radius * Math.sin(angle1), TRANS_Y + radius * Math.cos(angle1));
37 43
    }
38 44

  
39 45
    public double getAngle() {
......
43 49
    public Point2d getPoint() {
44 50
        return point;
45 51
    }
52

  
53
    public Stream<Point2d> getPointsLine() {
54
        final double angle1 = angle;
55
        return IntStream.rangeClosed(1, 10).mapToDouble(i -> i*10).mapToObj(radius -> getPoint(radius, angle1));
56
    }
46 57
}
src/main/java/cz/agents/alite/visualizer2d/demo/Demo.java
30 30
                        new PointElement<>(angleProvider.getPoint(), 3, Color.WHITE),
31 31
                        new PointElement<>(new Point2d(AngleProvider.TRANS_X + 25, AngleProvider.TRANS_Y), 3, Color.GREEN))
32 32
        ));
33
        layers.addLayer(ElementsLayer.create(PointElement.get(3, Color.BLUE, angleProvider::getPointsLine)));
33 34
        VisInfoLayer visInfoLayer = VisInfoLayer.create();
34 35
        //GroupLayer draggable = DraggableLayer.create(visInfoLayer);
35 36
        layers.addLayer(visInfoLayer);
src/main/java/cz/agents/alite/visualizer2d/layers/HudLayer.java
5 5
import cz.agents.alite.visualizer2d.VisInfo;
6 6
import cz.agents.alite.visualizer2d.layers.elements.Element;
7 7

  
8
import javax.vecmath.Point2d;
8 9
import java.awt.*;
9 10
import java.util.Collections;
10 11
import java.util.Set;
......
15 16
 */
16 17
public class HudLayer implements Layer {
17 18

  
18
    public HudLayer create(Element element) {
19
        Set<Element> set = Collections.singleton(element);
19
    public HudLayer create(Element<Point2d> element) {
20
        Set<Element<Point2d>> set = Collections.singleton(element);
20 21
        return create(() -> set);
21 22
    }
22 23

  
23
    public HudLayer create(Supplier<Iterable<Element>> elements) {
24
    public HudLayer create(Supplier<Iterable<Element<Point2d>>> elements) {
24 25
        return new HudLayer(elementsSupplier);
25 26
    }
26 27

  
27
    private final Supplier<Iterable<Element>> elementsSupplier;
28
    private final Supplier<Iterable<Element<Point2d>>> elementsSupplier;
28 29

  
29
    private HudLayer(Supplier<Iterable<Element>> elementsSupplier) {
30
    private HudLayer(Supplier<Iterable<Element<Point2d>>> elementsSupplier) {
30 31
        this.elementsSupplier = elementsSupplier;
31 32
    }
32 33

  
src/main/java/cz/agents/alite/visualizer2d/layers/elements/Elements.java
1
package cz.agents.alite.visualizer2d.layers.elements;
2

  
3
/**
4
* @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
*/
6
@FunctionalInterface
7
public interface Elements<T> {
8

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

  
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;
10
import java.util.stream.StreamSupport;
11 11

  
12 12
/**
13 13
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
......
20 20
        return create(() -> pointList);
21 21
    }
22 22

  
23
    public static <T> Layer<T> create(Supplier<Iterable<Element<T>>> elementsSupplier) {
24
        return new ElementsLayer<>(elementsSupplier);
23
    public static <T> Layer<T> create(Elements<T> elements) {
24
        return new ElementsLayer<>(() -> StreamSupport.stream(elements.get().spliterator(), false));
25 25
    }
26 26

  
27
    private final Supplier<Iterable<Element<T>>> elementsSupplier;
27
    public static <T> Layer<T> create(ElementsStream<T> elements) {
28
        return new ElementsLayer<>(elements);
29
    }
28 30

  
29
    private ElementsLayer(Supplier<Iterable<Element<T>>> elementsSupplier) {
30
        this.elementsSupplier = elementsSupplier;
31
    private final ElementsStream<T> elements;
32

  
33
    private ElementsLayer(ElementsStream<T> elements) {
34
        this.elements = elements;
31 35
    }
32 36

  
33 37
    //TODO check if forEach is as fast as for each cycle
34 38
    @Override
35 39
    public void paint(VisInfo visInfo, Point2dAdapter<T> adapter) {
36 40
        final Graphics2D worldCanvas = visInfo.getWorldCanvas();
37
        elementsSupplier.get().forEach(element -> element.paint(visInfo, worldCanvas, adapter));
41
        elements.get().sequential().forEach(element -> element.paint(visInfo, worldCanvas, adapter));
38 42
    }
43

  
39 44
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/ElementsStream.java
1
package cz.agents.alite.visualizer2d.layers.elements;
2

  
3
import java.util.stream.Stream;
4

  
5
/**
6
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
7
 */
8
@FunctionalInterface
9
public interface ElementsStream<T> {
10

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

  
13
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/PointElement.java
6 6
import javax.vecmath.Point2d;
7 7
import java.awt.*;
8 8
import java.awt.geom.Ellipse2D;
9
import java.util.function.Supplier;
10
import java.util.stream.Stream;
9 11

  
10 12
/**
11 13
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
12 14
 */
13 15
public class PointElement<T> implements Element<T> {
14 16

  
17
    public static <T> ElementsStream<T> get(double radius, Color color, Supplier<Stream<T>> positionSupplirer) {
18
        return () -> positionSupplirer.get().map(position -> new PointElement<>(position, radius, color));
19
    }
20

  
15 21
    private final T centerPosition;
16
    private final int radius;
22
    private final double radius;
17 23
    private final Color color;
18 24

  
19
    public PointElement(T centerPosition, int radius, Color color) {
25
    public PointElement(T centerPosition, double radius, Color color) {
20 26
        this.centerPosition = centerPosition;
21 27
        this.radius = radius;
22 28
        this.color = color;

Also available in: Unified diff