Step 6: Upgrade map animation

Currently we have a static picture of the road graph displayed over a map. Now we will add assets and trucks as well.

Add animation shapes

Go to the '…​application' bundle and locate the 'animation' package. Add new classes to that package:

StoreShape.java
package com.company.tutorial3.application.animation;

import com.amalgamasimulation.animation.shapes.shapes2d.CircleShape;
import com.amalgamasimulation.animation.shapes.shapes2d.GroupShape;
import com.amalgamasimulation.animation.shapes.shapes2d.TextShape;
import com.amalgamasimulation.geometry.Point;
import com.amalgamasimulation.utils.Colors;
import com.company.tutorial3.simulation.model.Store;

public class StoreShape extends GroupShape {
    public StoreShape(Store store) {
        super(() -> store.getNode().getPoint());
        withShape(new CircleShape(new Point(0, 0), 15, Colors.red).withFillColor(Colors.red));
        withShape(new TextShape(store.getName()));
        withFixedScale(1);
    }
}
TruckShape.java
package com.company.tutorial3.application.animation;

import com.amalgamasimulation.animation.shapes.shapes2d.GroupShape;
import com.amalgamasimulation.animation.shapes.shapes2d.RectangleShape;
import com.amalgamasimulation.geometry.Point;
import com.amalgamasimulation.utils.Colors;
import com.company.tutorial3.simulation.model.Truck;

public class TruckShape extends GroupShape {

    public TruckShape(Truck truck) {
        super(() -> truck.getCurrentAnimationPoint());
        withShape(new RectangleShape(() -> new Point(-5, -5), () -> 10.0, () -> 30.0).withLineColor(Colors.darkBlue)
                .withFillColor(() -> getTruckFillColor(truck)));
        withShape(new RectangleShape(() -> new Point(-4, -9), () -> 8.0, () -> 4.0).withLineColor(Colors.green)
                .withFillColor(Colors.green));
        withRotationAngle(() -> -truck.getHeading() + Math.PI);
        withFixedScale(1.3);
    }

    private java.awt.Color getTruckFillColor(Truck truck) {
        if (truck.getCurrentTask() == null) {
            return Colors.white;
        }
        return truck.getCurrentTask().isMovingWithCargo() ? Colors.blue : Colors.white;
    }
}
WarehouseShape.java
package com.company.tutorial3.application.animation;

import com.amalgamasimulation.animation.shapes.shapes2d.CircleShape;
import com.amalgamasimulation.animation.shapes.shapes2d.GroupShape;
import com.amalgamasimulation.animation.shapes.shapes2d.TextShape;
import com.amalgamasimulation.geometry.Point;
import com.amalgamasimulation.utils.Colors;
import com.company.tutorial3.simulation.model.Warehouse;

public class WarehouseShape extends GroupShape {
    public WarehouseShape(Warehouse warehouse) {
        super(() -> warehouse.getNode().getPoint());
        withShape(new CircleShape(new Point(0, 0), 15, Colors.orange).withFillColor(Colors.orange));
        withShape(new TextShape(warehouse.getName()));
        withFixedScale(1);
    }
}

Also, let’s make a minor update to the generated ArcShape: make the line color for roads grey instead of black:

Updated ArcShape.java
package com.company.tutorial3.application.animation;

import com.amalgamasimulation.animation.shapes.shapes2d.GroupShape;
import com.amalgamasimulation.animation.shapes.shapes2d.PolylineShape;
import com.amalgamasimulation.geometry.Point;
import com.company.tutorial3.simulation.model.Arc;
import com.amalgamasimulation.utils.Colors;


public class ArcShape extends GroupShape {
    
    public ArcShape(Arc road) {
        super(() -> new Point(0, 0));
        withShape(new PolylineShape(() -> road.getPolyline()).withLineColor(Colors.gray));
        asStatic(() -> true);
    }
}

Update SimulationPart

Get to the 'parts.simulation' package of the '…​application' bundle. Open the SimulationPart class. In its onShowModel() method, add the following lines right before the 'showAllMap()' call:

model.getWarehouses().forEach(w -> animationView.addShape(new WarehouseShape(w)));
model.getStores().forEach(w -> animationView.addShape(new StoreShape(w)));
model.getTrucks().forEach(t -> animationView.addShape(new TruckShape(t)));

Add appropriate import statements.

Check the result

Start the simulation. Now you can see new items in the map: orange 🟠 and red 🔴 circles represent a Warehouse and a Store. Trucks are also shown, with empty trucks having white fill color and loaded trucks painted with blue filling.

Animation