Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Simulation;
- import Simulation.Manager.RailManager;
- import Simulation.Manager.Railcab;
- import Simulation.Manager.Station;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Polygon;
- import java.util.ArrayList;
- import system.utilities;
- /**
- *
- * @author Maarten Blokker
- */
- public class SimulationTrack {
- //track variables
- private int width, height, x = 0, y = 0;
- public int radialSize;
- Polygon left, right;
- //track constants
- private final Simulation simulation;
- private final RailManager manager;
- public static final int MINDIR = -45;
- public static final int MAXDIR = 225;
- public SimulationTrack(Simulation simulation, RailManager manager, int radialSize) {
- this.simulation = simulation;
- this.manager = manager;
- this.radialSize = radialSize;
- initTrackModel();
- }
- /**
- * inits the track model.<br>
- * the track consists out of polygons, their location is calculated from the centerpoint
- */
- private void initTrackModel() {
- final ArrayList<Station> stations = manager.getStations();
- final ArrayList<Railcab> railcabs = manager.getRailcabs();
- x = simulation.frameWidth / 2-80;
- y = simulation.frameHeight / 2;
- left = new Polygon();
- right = new Polygon();
- int sc = 0;
- for (int i = MINDIR; i < MAXDIR; i++) {
- int xoffl = utilities.calculateXOffset(radialSize, i) + x;
- int yoffl = utilities.calculateYOffset(radialSize, i) + y;
- int xoffr = utilities.calculateXOffset(radialSize - 6, i) + x;
- int yoffr = utilities.calculateYOffset(radialSize - 6, i) + y;
- left.addPoint(xoffl, yoffl);
- right.addPoint(xoffr, yoffr);
- }
- for (int i = MINDIR; i < MAXDIR; i += (left.npoints / (stations.size() - 1))) {
- if (sc < railcabs.size()) {
- railcabs.get(sc).location = i;
- }
- stations.get(sc++).position = i;
- }
- }
- /**
- * updates the railmanager
- */
- public void update() {
- manager.update();
- }
- /**
- * draws the current track and their stations
- * @param g the graphical component to draw the track with
- */
- public void draw(Graphics2D g) {
- //draw track
- g.setColor(Color.BLACK);
- g.drawPolyline(left.xpoints, left.ypoints, left.npoints);
- g.drawPolyline(right.xpoints, right.ypoints, right.npoints);
- //draw stations
- for (Station s : manager.getStations()) {
- s.draw(g, x, y);
- }
- //draw railcabs
- for (Railcab railcab : manager.getRailcabs()) {
- railcab.draw(g, x, y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement