Revision 0:79b213914654

View differences:

pom.xml
3 3
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4 4
    <modelVersion>4.0.0</modelVersion>
5 5

  
6
    <name>Alite Visualizer2D package</name>
6
    <name>Alite Vis2D package</name>
7 7
    <groupId>cz.agents.alite</groupId>
8
    <artifactId>visualizer2d</artifactId>
9
    <version>0.0.1-SNAPSHOT</version>
8
    <artifactId>vis2d</artifactId>
9
    <version>1.0-SNAPSHOT</version>
10 10

  
11 11
    <parent>
12 12
        <groupId>cz.agents</groupId>
......
26 26
    </properties>
27 27

  
28 28
    <dependencies>
29
        <!--<dependency>
29
        <dependency>
30 30
            <groupId>cz.agents.alite</groupId>
31 31
            <artifactId>alite</artifactId>
32 32
            <version>1.1.0</version>
33
        </dependency>-->
33
        </dependency>
34 34
        <dependency>
35 35
            <groupId>junit</groupId>
36 36
            <artifactId>junit</artifactId>
......
41 41
            <groupId>java3d</groupId>
42 42
            <artifactId>vecmath</artifactId>
43 43
            <version>1.5.2</version>
44
            <!--<type>jar</type>-->
44
            <type>jar</type>
45 45
        </dependency>
46 46
    </dependencies>
47 47

  
src/main/java/cz/agents/alite/vis2d/Point2dAdapter.java
1
package cz.agents.alite.vis2d;
2

  
3
import javax.vecmath.Point2d;
4

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

  
10
    public static final Point2dAdapter<Point2d> IDENTITY = point2d -> point2d;
11

  
12
    public Point2d convert(T t);
13

  
14
}
src/main/java/cz/agents/alite/vis2d/Visualizable.java
1
package cz.agents.alite.vis2d;
2

  
3
import java.awt.*;
4
import java.awt.geom.Rectangle2D;
5

  
6
/**
7
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
8
 */
9
public interface Visualizable {
10

  
11
    public void paint(Graphics2D canvas, Rectangle2D drawingRectangle);
12

  
13
}
src/main/java/cz/agents/alite/vis2d/VisualizableLayer.java
1
package cz.agents.alite.vis2d;
2

  
3
import cz.agents.alite.vis.Vis;
4
import cz.agents.alite.vis.layer.AbstractLayer;
5

  
6
import java.awt.*;
7
import java.awt.geom.Rectangle2D;
8

  
9
/**
10
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
11
 */
12
public class VisualizableLayer extends AbstractLayer {
13

  
14
    private final VisualizableProvider provider;
15

  
16
    public VisualizableLayer(VisualizableProvider provider) {
17
        this.provider = provider;
18
    }
19

  
20
    @Override
21
    public void paint(Graphics2D canvas) {
22
        Dimension dim = Vis.getDrawingDimension();
23
        Rectangle2D drawingRectangle = new Rectangle(dim);
24
        provider.getData().forEach(element -> element.paint(canvas, drawingRectangle));
25
    }
26
}
src/main/java/cz/agents/alite/vis2d/VisualizableProvider.java
1
package cz.agents.alite.vis2d;
2

  
3
/**
4
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
 */
6
public interface VisualizableProvider {
7

  
8
    public Iterable<? extends Visualizable> getData();
9

  
10
}
src/main/java/cz/agents/alite/vis2d/demo/AngleProvider.java
1
package cz.agents.alite.vis2d.demo;
2

  
3
import javax.vecmath.Point2d;
4
import java.util.Timer;
5
import java.util.TimerTask;
6

  
7
/**
8
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
9
 */
10
class AngleProvider {
11

  
12
    private static final double RADIUS = 100;
13
    private static final double ANGLE_STEP = 0.05;
14
    private static final int PERIOD = 25;
15
    public static final double TRANS_Y = 500;
16
    public static final double TRANS_X = 500;
17

  
18
    private final Timer timer = new Timer("AngleProviderTimer", true);
19
    private volatile double angle = 0;
20
    private volatile Point2d point = new Point2d();
21

  
22
    public AngleProvider() {
23
        TimerTask task = new TimerTask() {
24
            @Override
25
            public void run() {
26
                angle += ANGLE_STEP;
27
                updatePoint();
28
            }
29
        };
30

  
31
        timer.scheduleAtFixedRate(task, 0, PERIOD);
32
    }
33

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

  
39
    public double getAngle() {
40
        return angle;
41
    }
42

  
43
    public Point2d getPoint() {
44
        return point;
45
    }
46
}
src/main/java/cz/agents/alite/vis2d/demo/Demo.java
1
package cz.agents.alite.vis2d.demo;
2

  
3
import cz.agents.alite.vis.VisManager;
4
import cz.agents.alite.vis.layer.common.ColorLayer;
5
import cz.agents.alite.vis2d.VisualizableLayer;
6
import cz.agents.alite.vis2d.implementation.PointVis;
7

  
8
import javax.vecmath.Point2d;
9
import java.awt.*;
10
import java.util.Arrays;
11

  
12
import static cz.agents.alite.vis2d.Point2dAdapter.IDENTITY;
13

  
14
/**
15
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
16
 */
17
public class Demo {
18

  
19

  
20
    public static void main(String[] args) {
21
        final AngleProvider angleProvider = new AngleProvider();
22
        VisManager.setInitParam("Test", 1024, 768, 1024, 768);
23
        VisManager.registerLayer(ColorLayer.create(Color.BLACK));
24
        VisManager.registerLayer(new VisualizableLayer(() -> Arrays.asList(
25
                new PointVis<>(new Point2d(AngleProvider.TRANS_X, AngleProvider.TRANS_Y), 3, Color.RED, IDENTITY),
26
                new PointVis<>(angleProvider.getPoint(), 3, Color.WHITE, IDENTITY))));
27
        VisManager.init();
28
    }
29

  
30
}
src/main/java/cz/agents/alite/vis2d/implementation/PointVis.java
1
package cz.agents.alite.vis2d.implementation;
2

  
3
import cz.agents.alite.vis.Vis;
4
import cz.agents.alite.vis2d.Point2dAdapter;
5
import cz.agents.alite.vis2d.Visualizable;
6

  
7
import javax.vecmath.Point2d;
8
import java.awt.*;
9
import java.awt.geom.Ellipse2D;
10
import java.awt.geom.Rectangle2D;
11

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

  
17
    private final T centerPosition;
18
    private final int radiusInPx;
19
    private final Point2dAdapter<T> adapter;
20
    private final Color color;
21

  
22
    public PointVis(T centerPosition, int radiusInPx, Color color, Point2dAdapter<T> adapter) {
23
        this.centerPosition = centerPosition;
24
        this.radiusInPx = radiusInPx;
25
        this.adapter = adapter;
26
        this.color = color;
27
    }
28

  
29
    @Override
30
    public void paint(Graphics2D canvas, Rectangle2D drawingRectangle) {
31
        Point2d center = adapter.convert(centerPosition);
32
        int x = Vis.transX(center.x);
33
        int y = Vis.transY(center.y);
34
        canvas.setColor(color);
35

  
36
        Ellipse2D point = new Ellipse2D.Double(x - radiusInPx, y - radiusInPx,
37
                2 * radiusInPx + 1, 2 * radiusInPx + 1);
38

  
39
        if (drawingRectangle.intersects(point.getBounds2D())) {
40
            canvas.fill(point);
41
        }
42
    }
43
}
src/main/java/cz/agents/alite/visualizer2d/Layer.java
1
package cz.agents.alite.visualizer2d;
2

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

  
8
    default public void init(VisPane<T> visPane) {}
9

  
10
    default public void deinit(VisPane<T> visPane) {}
11

  
12
    public void paint(VisInfo visInfo, Point2dAdapter<T> adapter);
13

  
14
}
src/main/java/cz/agents/alite/visualizer2d/Point2dAdapter.java
1
package cz.agents.alite.visualizer2d;
2

  
3
import javax.vecmath.Point2d;
4

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

  
10
    public static final Point2dAdapter<Point2d> IDENTITY = point2d -> point2d;
11

  
12
    public Point2d convert(T t);
13

  
14
}
src/main/java/cz/agents/alite/visualizer2d/VisInfo.java
1
package cz.agents.alite.visualizer2d;
2

  
3
import javafx.geometry.Bounds;
4
import javafx.geometry.Point2D;
5
import javafx.scene.canvas.GraphicsContext;
6
import javafx.scene.transform.Affine;
7
import javafx.scene.transform.NonInvertibleTransformException;
8

  
9

  
10

  
11
/**
12
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
13
 */
14
public final class VisInfo {
15

  
16
    private boolean screenTransformSet = true;
17
    private final Affine worldTransform;
18
    private final Affine originalWorldTransform;
19
    private final Affine screenTransform;
20
    private final GraphicsContext canvas;
21
    private final Bounds bounds;
22
    private final Point2D mousePosition;
23
    private final Point2D translation;
24
    private final int rotationDeg;
25
    private final double zoom;
26

  
27
    public VisInfo(Affine worldTransform, GraphicsContext canvas, Bounds bounds, Point2D mousePosition,
28
                   Point2D translation, int rotationDeg, double zoom) {
29
        this.bounds = bounds;
30
        this.canvas = canvas;
31
        this.mousePosition = mousePosition;
32
        this.translation = translation;
33
        this.rotationDeg = rotationDeg;
34
        this.zoom = zoom;
35

  
36
        this.screenTransform = canvas.getTransform();
37
        this.worldTransform = new Affine(screenTransform);
38
        this.worldTransform.append(worldTransform);
39
        this.originalWorldTransform = worldTransform;
40
    }
41

  
42
    public final GraphicsContext getWorldCanvas() {
43
        if (screenTransformSet) {
44
            canvas.setTransform(worldTransform);
45
            screenTransformSet = false;
46
        }
47
        return canvas;
48
    }
49

  
50
    public final GraphicsContext getScreenCanvas() {
51
        if (!screenTransformSet) {
52
            canvas.setTransform(screenTransform);
53
            screenTransformSet = true;
54
        }
55
        return canvas;
56
    }
57

  
58
    public final Bounds getBounds() {
59
        return bounds;
60
    }
61

  
62
    public final Point2D toWorldCoordinates(Point2D screenCoordinates) {
63
        try {
64
            return worldTransform.inverseTransform(screenCoordinates);
65
        } catch (NonInvertibleTransformException e) {
66
            e.printStackTrace();
67
        }
68
        return null;
69
    }
70

  
71
    public Point2D getMousePosition() {
72
        return mousePosition;
73
    }
74

  
75
    public double getZoom() {
76
        return zoom;
77
    }
78

  
79
    public Point2D getOffset() {
80
        return translation;
81
    }
82

  
83
    public int getRotationDeg() {
84
        return rotationDeg;
85
    }
86
}
src/main/java/cz/agents/alite/visualizer2d/VisPane.java
1
package cz.agents.alite.visualizer2d;
2

  
3

  
4
import javafx.animation.AnimationTimer;
5
import javafx.application.Platform;
6
import javafx.beans.property.IntegerProperty;
7
import javafx.beans.property.SimpleIntegerProperty;
8
import javafx.embed.swing.JFXPanel;
9
import javafx.geometry.Point2D;
10
import javafx.scene.Group;
11
import javafx.scene.Scene;
12
import javafx.scene.canvas.Canvas;
13
import javafx.scene.canvas.GraphicsContext;
14
import javafx.scene.transform.Affine;
15
import javafx.scene.transform.NonInvertibleTransformException;
16

  
17
import java.util.concurrent.ScheduledThreadPoolExecutor;
18

  
19
/**
20
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
21
 */
22
public final class VisPane<T> {
23

  
24
    public static final int REFRESH_IN_MS = 40; //25 FPS
25
    private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(1);
26

  
27
    private Layer<T> layer;
28
    //TODO solve volatile vs synchronized
29
    private volatile Point2dAdapter<T> adapter;
30
    private volatile Point2D mousePosition = new Point2D(0, 0);
31

  
32
    private AnimationTimer timer;
33
    private boolean running = false;
34

  
35
    private Point2D translation;
36
    private double zoom = 1;
37
    private int rotationDeg = 0;
38
    private final Affine transform = new Affine();
39

  
40
    private final Canvas canvas = new Canvas();
41
    private final JFXPanel panel = new JFXPanel();
42

  
43
    private final IntegerProperty canvasWidth = new SimpleIntegerProperty(1);
44
    private final IntegerProperty canvasHeight = new SimpleIntegerProperty(1);
45

  
46

  
47
    public VisPane(Layer<T> layer, Point2dAdapter<T> adapter) {
48
        this.layer = layer;
49
        this.adapter = adapter;
50
        //VisPaneMouseListener mouseListener = new VisPaneMouseListener(this);
51
        /*
52
        canvas.addMouseListener(mouseListener);
53
        canvas.addMouseMotionListener(mouseListener);
54
        canvas.addMouseWheelListener(mouseListener);
55
        canvas.addKeyListener(new VisPaneKeyListener(this));
56
        */
57

  
58
        /*panel.addComponentListener(new ComponentAdapter() {
59
            @Override
60
            public void componentResized(ComponentEvent e) {
61
                synchronized (VisPane.this) {
62
                    reinitializeBuffers = true;
63
                    canvas.setBounds(panel.getBounds());
64
                }
65
            }
66
        });*/
67
    }
68

  
69
    public synchronized void start() {
70
        if (!running) {
71
            Platform.runLater(() -> initFX(panel));
72
            running = true;
73
            layer.init(this);
74
            timer = new AnimationTimer() {
75
                @Override
76
                public void handle(long now) {
77
                    update();
78
                }
79
            };
80
            timer.start();
81
        }
82
    }
83

  
84
    private void initFX(JFXPanel fxPanel) {
85
        // This method is invoked on JavaFX thread
86
        Scene scene = createScene();
87
        fxPanel.setScene(scene);
88
    }
89

  
90
    private Scene createScene() {
91
        Group root  =  new  Group();
92
        Scene scene = new Scene(root);
93
        root.getChildren().add(canvas);
94
        canvas.widthProperty().bind(scene.widthProperty());
95
        canvas.heightProperty().bind(scene.heightProperty());
96
        new VisPaneMouseListener(this, canvas);
97

  
98

  
99
        return scene;
100
    }
101

  
102

  
103
    public synchronized void stop() {
104
        if (running) {
105
            running = false;
106
            timer.stop();
107
            //TODO wait for stop
108
            layer.deinit(this);
109
        }
110
    }
111

  
112
    public JFXPanel getPanel() {
113
        return panel;
114
    }
115

  
116
    public Point2dAdapter<T> getAdapter() {
117
        return adapter;
118
    }
119

  
120
    public void setAdapter(Point2dAdapter<T> adapter) {
121
        this.adapter = adapter;
122
    }
123

  
124
    public synchronized Layer<T> getLayer() {
125
        return layer;
126
    }
127

  
128
    public synchronized void setLayer(Layer<T> layer) {
129
        boolean wasRunning = running;
130
        stop();
131
        this.layer = layer;
132
        if (wasRunning) {
133
            start();
134
        }
135
    }
136

  
137
    public final void translate(double deltaX, double deltaY) {
138
        final Point2D deltaScreen = new Point2D(deltaX, deltaY);
139

  
140
        synchronized (transform) {
141
            try {
142
                Point2D deltaWorld = transform.inverseDeltaTransform(deltaScreen);
143
                transform.appendTranslation(deltaWorld.getX(), deltaWorld.getY());
144
            } catch (NonInvertibleTransformException e) {
145
                e.printStackTrace();
146
            }
147
        }
148
    }
149

  
150
    public final void scaleBy(double multiplyFactor, double zoomCenterX, double zoomCenterY) {
151
        synchronized (transform) {
152
            try {
153
                Point2D scaleCenterWorld = transform.inverseTransform(zoomCenterX, zoomCenterY);
154
                transform.appendScale(multiplyFactor, multiplyFactor, scaleCenterWorld);
155
                zoom *= multiplyFactor;
156
            } catch (NonInvertibleTransformException e) {
157
                e.printStackTrace();
158
            }
159
        }
160
    }
161

  
162
   /* public final void rotate(int deltaAngleDeg) {
163
        final double deltaAngleRad = Math.toRadians(deltaAngleDeg);
164
        Rectangle bounds = panel.getBounds();
165
        final Point2D centerScreen = new Point2D.Double(bounds.getCenterX(), bounds.getCenterY());
166
        synchronized (transform) {
167
            Point2D centerWorld = new Point2D.Double();
168
            try {
169
                transform.inverseTransform(centerScreen, centerWorld);
170
                transform.rotate(deltaAngleRad, centerWorld.getX(), centerWorld.getY());
171
                rotationDeg += deltaAngleDeg;
172
            } catch (NoninvertibleTransformException e) {
173
                e.printStackTrace();
174
            }
175
        }
176
    }*/
177

  
178
    private void update() {
179
        //if (canvas.isDisplayable()) {
180
        GraphicsContext graphics = canvas.getGraphicsContext2D();
181

  
182
            Affine screenTransform = graphics.getTransform();
183
            Affine worldTransform = getWorldTransform();
184

  
185
            VisInfo visInfo = new VisInfo(worldTransform, graphics, canvas.getBoundsInLocal(), mousePosition, translation, rotationDeg, zoom);
186

  
187
            layer.paint(visInfo, adapter);
188

  
189
            graphics.setTransform(screenTransform);
190

  
191
            //strategy.show();
192
        //}
193
    }
194

  
195
    private Affine getWorldTransform() {
196
        synchronized (transform) {
197
            return new Affine(transform);
198
        }
199
    }
200

  
201
/*    public void addMouseListener(MouseListener mouseListener) {
202
        canvas.addMouseListener(mouseListener);
203
    }
204

  
205
    public void addMouseMotionListener(MouseMotionListener mouseMotionListener) {
206
        canvas.addMouseMotionListener(mouseMotionListener);
207
    }
208

  
209
    public void removeMouseMotionListener(MouseMotionListener mouseMotionListener) {
210
        canvas.removeMouseMotionListener(mouseMotionListener);
211
    }
212

  
213
    public void removeMouseListener(MouseListener mouseListener) {
214
        canvas.removeMouseListener(mouseListener);
215
    }*/
216

  
217
    public void updateMousePosition(Point2D mousePostion) {
218
        this.mousePosition = mousePostion;
219
    }
220
}
src/main/java/cz/agents/alite/visualizer2d/VisPaneKeyListener.java
1
package cz.agents.alite.visualizer2d;
2

  
3
import java.awt.event.KeyAdapter;
4
import java.awt.event.KeyEvent;
5

  
6
/**
7
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
8
 */
9
public class VisPaneKeyListener extends KeyAdapter {
10

  
11
    public static final int ROTATE_ANGLE_STEP_DEG = 5;
12

  
13
    public static final int ROTATE_LEFT_KEY = KeyEvent.VK_Q;
14
    public static final int ROTATE_RIGHT_KEY = KeyEvent.VK_W;
15

  
16
    private final VisPane visPane;
17

  
18
    public VisPaneKeyListener(VisPane visPane) {
19
        this.visPane = visPane;
20
    }
21

  
22
    @Override
23
    public void keyPressed(KeyEvent e) {
24
        switch (e.getKeyCode()) {
25
            case ROTATE_LEFT_KEY:
26
                //visPane.rotate(ROTATE_ANGLE_STEP_DEG);
27
                break;
28
            case ROTATE_RIGHT_KEY:
29
                //visPane.rotate(-ROTATE_ANGLE_STEP_DEG);
30
                break;
31
        }
32
    }
33
}
src/main/java/cz/agents/alite/visualizer2d/VisPaneMouseListener.java
1
package cz.agents.alite.visualizer2d;
2

  
3

  
4
import javafx.scene.Node;
5
import javafx.scene.input.MouseButton;
6
import javafx.scene.input.MouseEvent;
7
import javafx.scene.input.ScrollEvent;
8

  
9

  
10
/**
11
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
12
 */
13
public class VisPaneMouseListener {
14

  
15
    public static final double SCALE_MULTIPLICATOR = 1.1;
16
    private final VisPane visPane;
17

  
18
    private double lastX, lastY;
19

  
20

  
21
    public VisPaneMouseListener(VisPane visPane, Node node) {
22
        this.visPane = visPane;
23
        node.setOnMousePressed(this::mousePressed);
24
        node.setOnMouseDragged(this::mouseDragged);
25
        node.setOnScroll(this::mouseWheelMoved);
26
    }
27

  
28
    public void mousePressed(MouseEvent e) {
29
        lastX = e.getX();
30
        lastY = e.getY();
31
    }
32

  
33
    public void mouseDragged(MouseEvent e) {
34
        if (e.getButton() == MouseButton.PRIMARY) {
35
            double deltaX = e.getX() - lastX;
36
            double deltaY = e.getY() - lastY;
37

  
38
            lastX = e.getX();
39
            lastY = e.getY();
40
            visPane.translate(deltaX, deltaY);
41
        }
42
    }
43

  
44
    /*public void mouseMoved(MouseEvent e) {
45
        visPane.updateMousePosition(e.getPoint());
46
    }*/
47

  
48
    public void mouseWheelMoved(ScrollEvent e) {
49
        double rotation = e.getDeltaY() / e.getMultiplierY();
50
        if (rotation > 0) {
51
            visPane.scaleBy(SCALE_MULTIPLICATOR * rotation, e.getX(), e.getY());
52
        } else if (rotation < 0) {
53
            visPane.scaleBy(1/(SCALE_MULTIPLICATOR * (-rotation)), e.getX(), e.getY());
54
        }
55
    }
56
}
src/main/java/cz/agents/alite/visualizer2d/camera/CameraControl.java
1
package cz.agents.alite.visualizer2d.camera;
2

  
3
/**
4
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
 */
6
public interface CameraControl {
7

  
8
    public boolean moveScreenRelative(int deltaX, int deltaY, boolean ignoreLimits);
9

  
10
    public boolean moveScreenAbsolute(int deltaX, int deltaY, boolean ignoreLimits);
11

  
12
    public boolean rotateScreenRelative(int rotationInDeg, boolean onlyRotation);
13

  
14
    public boolean rotateAbsolute(int rotationInDeg, boolean onlyRotation);
15

  
16
    public boolean zoomRelative(double scaleMultiplicator, boolean ignoreLimits);
17

  
18
    public boolean zoomAbsolute(double scaleFactor, boolean ignoreLimits);
19
}
src/main/java/cz/agents/alite/visualizer2d/camera/CameraController.java
1
package cz.agents.alite.visualizer2d.camera;
2

  
3
/**
4
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
 */
6
public interface CameraController {
7

  
8
    public void register(CameraControl visPane);
9

  
10
    public void unregister(CameraControl visPane);
11

  
12
}
src/main/java/cz/agents/alite/visualizer2d/camera/InteruptibleController.java
1
package cz.agents.alite.visualizer2d.camera;
2

  
3
/**
4
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
5
 */
6
public class InteruptibleController implements CameraController {
7

  
8
    private final CameraController primaryController;
9
    private final CameraController interrupter;
10

  
11
    public InteruptibleController(CameraController primaryController, CameraController interrupter) {
12
        this.primaryController = primaryController;
13
        this.interrupter = interrupter;
14
    }
15

  
16
    @Override
17
    public void register(CameraControl visPane) {
18

  
19
    }
20

  
21
    @Override
22
    public void unregister(CameraControl visPane) {
23

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

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

  
11
/**
12
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
13
 */
14
class AngleProvider {
15

  
16
    private static final double RADIUS = 100;
17
    private static final double ANGLE_STEP = 0.05;
18
    private static final int PERIOD = 25;
19
    public static final double TRANS_Y = 500;
20
    public static final double TRANS_X = 500;
21

  
22
    private final Timer timer = new Timer("AngleProviderTimer", true);
23
    private volatile double angle = 0;
24
    private volatile Point2d point = new Point2d();
25

  
26
    public AngleProvider() {
27
        TimerTask task = new TimerTask() {
28
            @Override
29
            public void run() {
30
                angle += ANGLE_STEP;
31
                updatePoint();
32
            }
33
        };
34

  
35
        timer.scheduleAtFixedRate(task, 0, PERIOD);
36
    }
37

  
38
    private void updatePoint() {
39
        final double angle1 = angle;
40
        point = getPoint(RADIUS, angle1);
41
    }
42

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

  
47
    public double getAngle() {
48
        return angle;
49
    }
50

  
51
    public Point2d getPoint() {
52
        return point;
53
    }
54

  
55
    public Stream<Point2d> getPointsLine() {
56
        final double angle1 = angle;
57
        return IntStream.rangeClosed(1, 10).mapToDouble(i -> i*10).mapToObj(radius -> getPoint(radius, angle1));
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
    }
65
}
src/main/java/cz/agents/alite/visualizer2d/demo/Demo.java
1
package cz.agents.alite.visualizer2d.demo;
2

  
3

  
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisPane;
6
import cz.agents.alite.visualizer2d.layers.ColorLayer;
7
import cz.agents.alite.visualizer2d.layers.GroupLayer;
8
import cz.agents.alite.visualizer2d.layers.elements.ElementsLayer;
9
import cz.agents.alite.visualizer2d.layers.elements.LineElement;
10
import cz.agents.alite.visualizer2d.layers.elements.PointElement;
11
import javafx.scene.paint.Color;
12

  
13
import javax.swing.*;
14
import javax.vecmath.Point2d;
15
import java.util.stream.Stream;
16

  
17
/**
18
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
19
 */
20
public class Demo {
21

  
22

  
23
    public static void main(String[] args) throws InterruptedException {
24
        final AngleProvider angleProvider = new AngleProvider();
25

  
26
        GroupLayer<Point2d> layers = GroupLayer.create();
27
        layers.addLayer(ColorLayer.create(Color.BLACK));
28
        layers.addLayer(ElementsLayer.create(() -> Stream.of(
29
                        PointElement.get(3, Color.RED, new Point2d(AngleProvider.TRANS_X, AngleProvider.TRANS_Y)),
30
                        PointElement.get(3, Color.WHITE, angleProvider.getPoint()),
31
                        PointElement.get(3, Color.GREEN, new Point2d(AngleProvider.TRANS_X + 25, AngleProvider.TRANS_Y)))
32
        ));
33
        layers.addLayer(ElementsLayer.create(PointElement.get(3, Color.BLUE, angleProvider::getPointsLine)));
34
        layers.addLayer(ElementsLayer.create(() -> LineElement.get(Color.GREEN, 2, angleProvider.getSquare())));
35
        //VisInfoLayer visInfoLayer = VisInfoLayer.create();
36
        //GroupLayer draggable = DraggableLayer.create(visInfoLayer);
37
        //layers.addLayer(visInfoLayer);
38
        VisPane<Point2d> visPane = new VisPane<>(layers, Point2dAdapter.IDENTITY);
39
        JFrame frame = new JFrame("Visualizer2d Demo");
40
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
41
        frame.setSize(1024, 768);
42

  
43
        frame.setContentPane(visPane.getPanel());
44

  
45
        visPane.start();
46
        frame.setVisible(true);
47
    }
48

  
49
}
src/main/java/cz/agents/alite/visualizer2d/layers/ColorLayer.java
1
package cz.agents.alite.visualizer2d.layers;
2

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import javafx.geometry.Bounds;
7
import javafx.scene.canvas.GraphicsContext;
8
import javafx.scene.paint.Color;
9

  
10

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

  
16
    public static <T> ColorLayer<T> create(Color color) {
17
        return new ColorLayer<>(color);
18
    }
19

  
20
    private final Color color;
21

  
22
    private ColorLayer(Color color) {
23
        this.color = color;
24
    }
25

  
26
    @Override
27
    public void paint(VisInfo visInfo, Point2dAdapter<T> adapter) {
28
        GraphicsContext graphics = visInfo.getScreenCanvas();
29
        graphics.setFill(color);
30
        Bounds rectangle = visInfo.getBounds();
31
        graphics.fillRect(rectangle.getMinX(), rectangle.getMinY(), rectangle.getWidth(), rectangle.getWidth());
32
    }
33
}
src/main/java/cz/agents/alite/visualizer2d/layers/DraggableLayer.java
1
package cz.agents.alite.visualizer2d.layers;
2

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import cz.agents.alite.visualizer2d.VisPane;
7

  
8
import java.awt.event.MouseEvent;
9
import java.awt.event.MouseListener;
10
import java.awt.event.MouseMotionListener;
11

  
12
/**
13
 * DraggableLayer allows to move another layer by mouse over screen. The another layer must implement {@link
14
 * Draggable} interface.
15
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
16
 *
17
 * @see Draggable
18
 * @see cz.agents.alite.visualizer2d.layers.textBackgroundLayer.TextBackgroundLayer
19
 */
20
public class DraggableLayer<T> implements Layer<T>, MouseListener, MouseMotionListener {
21

  
22
    private final int mouseButton;
23
    private final Draggable draggable;
24

  
25
    private int lastX, lastY;
26
    private boolean dragging = false;
27

  
28
    /**
29
     * Binds DraggableLayer with given {@code draggable}. Returned layer containts both of these layers.
30
     * @param mouseButton mouse button on which the layer should be dragged
31
     * @param draggable layer to bind
32
     * @return composition of the given {@code draggable} and newly created instance of {@link DraggableLayer}
33
     *
34
     * @see java.awt.event.MouseEvent
35
     */
36
    public static <T> GroupLayer<T> create(int mouseButton, Draggable<T> draggable) {
37
        GroupLayer<T> out = GroupLayer.create();
38

  
39
        out.addLayer(draggable);
40
        out.addLayer(new DraggableLayer<>(mouseButton, draggable));
41

  
42
        return out;
43
    }
44

  
45
    /**
46
     * /**
47
     * Binds DraggableLayer with given {@code draggable}. Returned layer containts both of these layers. The layer
48
     * reacts on left mouse button.
49
     * @param draggable layer to bind
50
     * @return composition of the given {@code draggable} and newly created instance of {@link DraggableLayer}
51
     *
52
     * @see java.awt.event.MouseEvent
53
     */
54
    public static <T> GroupLayer<T> create(Draggable<T> draggable) {
55
        return create(MouseEvent.BUTTON1, draggable);
56
    }
57

  
58
    private DraggableLayer(int mouseButton, Draggable draggable) {
59
        this.mouseButton = mouseButton;
60
        this.draggable = draggable;
61
    }
62

  
63
    @Override
64
    public void init(VisPane visPane) {
65
       // visPane.addMouseListener(this);
66
       // visPane.addMouseMotionListener(this);
67
        System.out.println("Mouse listener added");
68
    }
69

  
70
    @Override
71
    public void deinit(VisPane visPane) {
72
       // visPane.removeMouseMotionListener(this);
73
       // visPane.removeMouseListener(this);
74
    }
75

  
76
    @Override
77
    public void paint(VisInfo visInfo, Point2dAdapter<T> adapter) {}
78

  
79
    @Override
80
    public void mouseDragged(MouseEvent e) {
81
        if (dragging && draggable.isActive()) {
82
            int deltaX = e.getX() - lastX;
83
            int deltaY = e.getY() - lastY;
84

  
85
            lastX = e.getX();
86
            lastY = e.getY();
87

  
88
            draggable.moveLocation(deltaX, deltaY);
89
        }
90
    }
91

  
92
    @Override
93
    public void mouseMoved(MouseEvent e) {}
94

  
95
    @Override
96
    public void mouseClicked(MouseEvent e) {}
97

  
98
    @Override
99
    public void mousePressed(MouseEvent e) {
100
        if (e.getButton() == mouseButton && draggable.isActive() && e.getClickCount() == 1 &&
101
                draggable.isInArea(e.getPoint().x, e.getPoint().y)) {
102
            dragging = true;
103
            lastX = e.getX();
104
            lastY = e.getY();
105
        }
106
    }
107

  
108
    @Override
109
    public void mouseReleased(MouseEvent e) {
110
        dragging = false;
111
    }
112

  
113
    @Override
114
    public void mouseEntered(MouseEvent e) {}
115

  
116
    @Override
117
    public void mouseExited(MouseEvent e) {}
118

  
119
    /**
120
     * This interface allows for layer to be moved on screen by drag and drop.
121
     */
122
    public interface Draggable<T> extends Layer<T> {
123

  
124
        /**
125
         * Method decides if the layer is active and if the dragging is allowed. This method determines if
126
         * {@link DraggableLayer} will react on mouse click by dragging the layer or not.
127
         * @return {@code true} if the layer is active and allows dragging, {@code false} otherwise.
128
         */
129
        public boolean isActive();
130

  
131
        /**
132
         * Tests whether given coordinates are in area of interest given by implementation. This method determines if
133
         * {@link DraggableLayer} will react on mouse click by dragging the layer or not.
134
         * @param x x-coordinate of mouse click
135
         * @param y y-coordinate of mouse click
136
         * @return {@code true} if the layer is allowed to drag from given coordinates, {@code false} otherwise.
137
         */
138
        public boolean isInArea(int x, int y);
139

  
140
        /**
141
         * This method is called during dragging. The parameters represent amount of change in which the layer should
142
         * be moved
143
         * @param deltaX delta in x-coordinates
144
         * @param deltaY delta in y-coordinates
145
         */
146
        public void moveLocation(int deltaX, int deltaY);
147
    }
148
}
src/main/java/cz/agents/alite/visualizer2d/layers/GroupLayer.java
1
package cz.agents.alite.visualizer2d.layers;
2

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import cz.agents.alite.visualizer2d.VisPane;
7

  
8
import java.util.HashSet;
9
import java.util.List;
10
import java.util.Set;
11
import java.util.concurrent.CopyOnWriteArrayList;
12

  
13
/**
14
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
15
 */
16
public class GroupLayer<T> implements Layer<T> {
17

  
18
    public static <T> GroupLayer<T> create() {
19
        return new GroupLayer<>();
20
    }
21

  
22
    private GroupLayer() {}
23

  
24
    private final Set<VisPane<T>> initializedPanes = new HashSet<>();
25
    private final List<Layer<T>> layers = new CopyOnWriteArrayList<>();
26

  
27
    @Override
28
    public synchronized void init(VisPane<T> visPane) {
29
        if (!initializedPanes.contains(visPane)) {
30
            initializedPanes.add(visPane);
31
            layers.forEach(layer -> layer.init(visPane));
32
        }
33
    }
34

  
35
    @Override
36
    public synchronized void deinit(VisPane<T> visPane) {
37
        if (initializedPanes.contains(visPane)) {
38
            initializedPanes.remove(visPane);
39
            layers.forEach(layer -> layer.deinit(visPane));
40
        }
41

  
42
    }
43

  
44
    @Override
45
    public final void paint(VisInfo visInfo, Point2dAdapter<T> adapter) {
46
        for (Layer<T> layer : layers) {
47
            layer.paint(visInfo, adapter);
48
        }
49
    }
50

  
51
    public synchronized void addLayer(Layer<T> layer) {
52
        if (!layers.contains(layer)) {
53
            initializedPanes.forEach(layer::init);
54
            layers.add(layer);
55
        }
56
    }
57

  
58
    public synchronized boolean removeLayer(Layer<T> layer) {
59
        if (!layers.contains(layer)) {
60
            layers.remove(layer);
61
            //TODO ensure thread safety
62
            initializedPanes.forEach(layer::deinit);
63
            return true;
64
        }
65
        return false;
66
    }
67

  
68
}
src/main/java/cz/agents/alite/visualizer2d/layers/HudLayer.java
1
package cz.agents.alite.visualizer2d.layers;
2

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import cz.agents.alite.visualizer2d.layers.elements.Element;
7
import javafx.scene.canvas.GraphicsContext;
8

  
9
import javax.vecmath.Point2d;
10
import java.util.Collections;
11
import java.util.Set;
12
import java.util.function.Supplier;
13

  
14
/**
15
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
16
 */
17
public class HudLayer implements Layer {
18

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

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

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

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

  
34
    @Override
35
    public void paint(VisInfo visInfo, Point2dAdapter adapter) {
36
        GraphicsContext screen = visInfo.getScreenCanvas();
37
        elementsSupplier.get().forEach(element -> element.paint(visInfo, screen, Point2dAdapter.IDENTITY));
38
    }
39
}
src/main/java/cz/agents/alite/visualizer2d/layers/VisInfoLayer.java
1
package cz.agents.alite.visualizer2d.layers;
2

  
3
import cz.agents.alite.visualizer2d.Point2dAdapter;
4
import cz.agents.alite.visualizer2d.VisInfo;
5
import cz.agents.alite.visualizer2d.layers.textBackgroundLayer.*;
6
import javafx.geometry.Point2D;
7
import javafx.scene.paint.Color;
8

  
9
import java.util.Collections;
10

  
11
/**
12
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
13
 */
14
public class VisInfoLayer extends AbstractTextBackgroundLayer {
15

  
16
    private final Color textColor = new Color(164d, 172d, 151, 1);
17

  
18
    public static VisInfoLayer create() {
19
        return new VisInfoLayer(new Color(44, 44, 44, 152), Padding.DEFAUT_PADDING, new BottomOffsetPositionFunction(15, 15));
20
    }
21

  
22
    private VisInfoLayer(Color backgroundColor, Padding padding, PositionFunction positionFunction) {
23
        super(backgroundColor, padding, positionFunction);
24
    }
25

  
26
    @Override
27
    protected Iterable<ColoredTextElement> getTextElements(VisInfo visInfo, Point2dAdapter adapter) {
28
        StringBuilder text = new StringBuilder();
29

  
30
        Point2D offset = visInfo.getOffset();
31
        Point2D mousePosition = visInfo.getMousePosition();
32
        Point2D worldCoordinates = visInfo.toWorldCoordinates(mousePosition);
33

  
34
        text.append(String.format("Zoom: %.2f\n", visInfo.getZoom()));
35
        text.append(String.format("Rotation: %d\n", visInfo.getRotationDeg()));
36
        text.append(String.format("Offset: (%.2f, %.2f)\n", offset.getX(), offset.getY()));
37
        text.append(String.format("Screen: (%f, %f)\n", mousePosition.getX(), mousePosition.getY()));
38
        text.append(String.format("World: (%.2f, %.2f)", worldCoordinates.getX(), worldCoordinates.getY()));
39

  
40
        return Collections.singleton(new ColoredTextElementImpl(text.toString(), textColor));
41

  
42
    }
43
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/Element.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
import javafx.scene.canvas.GraphicsContext;
6

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

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

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

  
18
}
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<? extends Element<T>> get();
10

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

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import javafx.scene.canvas.GraphicsContext;
7

  
8
import java.util.Arrays;
9
import java.util.List;
10
import java.util.function.Supplier;
11
import java.util.stream.Stream;
12
import java.util.stream.StreamSupport;
13

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

  
19
    @SafeVarargs
20
    public static <T> Layer<T> create(Element<T>... elements) {
21
        List<Element<T>> pointList = Arrays.asList(elements);
22
        return create(() -> pointList);
23
    }
24

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

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

  
34
    public static <T> Layer<T> create(ElementsStream<T> elements) {
35
        return new ElementsLayer<>(elements);
36
    }
37

  
38
    private final ElementsStream<T> elements;
39

  
40
    private ElementsLayer(ElementsStream<T> elements) {
41
        this.elements = elements;
42
    }
43

  
44
    //TODO check if forEach is as fast as for each cycle
45
    @Override
46
    public void paint(VisInfo visInfo, Point2dAdapter<T> adapter) {
47
        final GraphicsContext worldCanvas = visInfo.getWorldCanvas();
48
        elements.get().sequential().forEach(element -> element.paint(visInfo, worldCanvas, adapter));
49
    }
50

  
51
}
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<? extends Element<T>> get();
12

  
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
import javafx.scene.canvas.GraphicsContext;
6
import javafx.scene.paint.Color;
7

  
8
import javax.vecmath.Point2d;
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, double 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, double 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, double 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, double 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, double 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 double lineThickness;
45
    private final T first, second;
46
    private final Iterable<T> next;
47

  
48
    private LineElement(Color lineColor, double 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, GraphicsContext canvas, Point2dAdapter<T> adapter) {
58
        Point2d firstConverted = adapter.convert(first);
59
        Point2d secondConverted = adapter.convert(second);
60

  
61
        canvas.setStroke(lineColor);
62
        canvas.setLineWidth(lineThickness);
63
        //Stroke stroke = new BasicStroke(10);
64
        //canvas.setStroke(stroke);
65
        canvas.beginPath();
66
        canvas.moveTo(firstConverted.x, firstConverted.y);
67
        canvas.lineTo(secondConverted.x, secondConverted.y);
68
        next.forEach(t -> {
69
            final Point2d point = adapter.convert(t);
70
            canvas.lineTo(point.x, point.y);
71
        });
72
        canvas.stroke();
73
    }
74

  
75

  
76

  
77
}
src/main/java/cz/agents/alite/visualizer2d/layers/elements/PointElement.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
import javafx.scene.canvas.GraphicsContext;
6
import javafx.scene.paint.Color;
7

  
8
import javax.vecmath.Point2d;
9
import java.util.function.Supplier;
10
import java.util.stream.Stream;
11

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

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

  
21
    public static <T> ElementsStream<T> get(double radius, Color color, Supplier<Stream<T>> positionSupplirer) {
22
        return () -> positionSupplirer.get().map(position -> new PointElement<>(position, radius, color));
23
    }
24

  
25
    private final T centerPosition;
26
    private final double radius;
27
    private final Color color;
28

  
29
    private PointElement(T centerPosition, double radius, Color color) {
30
        this.centerPosition = centerPosition;
31
        this.radius = radius;
32
        this.color = color;
33
    }
34

  
35
    @Override
36
    public void paint(VisInfo visInfo, GraphicsContext canvas, Point2dAdapter<T> adapter) {
37
        Point2d center = adapter.convert(centerPosition);
38
        canvas.setFill(color);
39
        canvas.setStroke(color);
40

  
41
        canvas.fillOval(center.x - radius, center.y - radius,
42
                2 * radius + 1, 2 * radius + 1);
43
    }
44
}
src/main/java/cz/agents/alite/visualizer2d/layers/textBackgroundLayer/AbstractTextBackgroundLayer.java
1
package cz.agents.alite.visualizer2d.layers.textBackgroundLayer;
2

  
3
import cz.agents.alite.visualizer2d.Layer;
4
import cz.agents.alite.visualizer2d.Point2dAdapter;
5
import cz.agents.alite.visualizer2d.VisInfo;
6
import cz.agents.alite.visualizer2d.layers.DraggableLayer;
7
import javafx.geometry.Bounds;
8
import javafx.geometry.Point2D;
9
import javafx.geometry.Rectangle2D;
10
import javafx.scene.canvas.GraphicsContext;
11
import javafx.scene.paint.Color;
12

  
13
import java.awt.*;
14
import java.awt.image.BufferedImage;
15
import java.util.ArrayList;
16
import java.util.List;
17

  
18
/**
19
 * @author Ondrej Hrstka (ondrej.hrstka at agents.fel.cvut.cz)
20
 */
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff