Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package Simulation;
  2.  
  3. import Simulation.Manager.RailManager;
  4. import Simulation.Manager.Railcab;
  5. import Simulation.Manager.Station;
  6. import java.awt.Color;
  7. import java.awt.Graphics2D;
  8. import java.awt.Polygon;
  9. import java.util.ArrayList;
  10. import system.utilities;
  11.  
  12. /**
  13.  *
  14.  * @author Maarten Blokker
  15.  */
  16. public class SimulationTrack {
  17.  
  18.     //track variables
  19.     private int width, height, x = 0, y = 0;
  20.     public int radialSize;
  21.     Polygon left, right;
  22.     //track constants
  23.     private final Simulation simulation;
  24.     private final RailManager manager;
  25.     public static final int MINDIR = -45;
  26.     public static final int MAXDIR = 225;
  27.  
  28.     public SimulationTrack(Simulation simulation, RailManager manager, int radialSize) {
  29.         this.simulation = simulation;
  30.         this.manager = manager;
  31.         this.radialSize = radialSize;
  32.         initTrackModel();
  33.     }
  34.  
  35.     /**
  36.      * inits the track model.<br>
  37.      * the track consists out of polygons, their location is calculated from the centerpoint
  38.      */
  39.     private void initTrackModel() {
  40.         final ArrayList<Station> stations = manager.getStations();
  41.         final ArrayList<Railcab> railcabs = manager.getRailcabs();
  42.         x = simulation.frameWidth / 2-80;
  43.         y = simulation.frameHeight / 2;
  44.         left = new Polygon();
  45.         right = new Polygon();
  46.         int sc = 0;
  47.         for (int i = MINDIR; i < MAXDIR; i++) {
  48.             int xoffl = utilities.calculateXOffset(radialSize, i) + x;
  49.             int yoffl = utilities.calculateYOffset(radialSize, i) + y;
  50.             int xoffr = utilities.calculateXOffset(radialSize - 6, i) + x;
  51.             int yoffr = utilities.calculateYOffset(radialSize - 6, i) + y;
  52.  
  53.             left.addPoint(xoffl, yoffl);
  54.             right.addPoint(xoffr, yoffr);
  55.         }
  56.         for (int i = MINDIR; i < MAXDIR; i += (left.npoints / (stations.size() - 1))) {
  57.             if (sc < railcabs.size()) {
  58.                 railcabs.get(sc).location = i;
  59.             }
  60.             stations.get(sc++).position = i;
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * updates the railmanager
  66.      */
  67.     public void update() {
  68.         manager.update();
  69.     }
  70.  
  71.     /**
  72.      * draws the current track and their stations
  73.      * @param g the graphical component to draw the track with
  74.      */
  75.     public void draw(Graphics2D g) {
  76.         //draw track
  77.         g.setColor(Color.BLACK);
  78.         g.drawPolyline(left.xpoints, left.ypoints, left.npoints);
  79.         g.drawPolyline(right.xpoints, right.ypoints, right.npoints);
  80.  
  81.         //draw stations
  82.         for (Station s : manager.getStations()) {
  83.             s.draw(g, x, y);
  84.         }
  85.         //draw railcabs
  86.         for (Railcab railcab : manager.getRailcabs()) {
  87.             railcab.draw(g, x, y);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement