source: tmcsimulator/trunk/src/tmcsim/highwaymodel/TrafficEventsAnimator.java @ 474

Revision 474, 7.0 KB checked in by jdalbey, 7 years ago (diff)

TrafficEventsAnimator? updated for revised highway model.

Line 
1package tmcsim.highwaymodel;
2
3import tmcsim.highwaymodel.Highways;
4import tmcsim.highwaymodel.TrafficEvent;
5import java.io.FileInputStream;
6import java.io.FileNotFoundException;
7import java.rmi.RemoteException;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.LinkedList;
11import java.util.List;
12import java.util.Map;
13import java.util.Properties;
14import java.util.Scanner;
15import java.util.logging.Level;
16import java.util.logging.Logger;
17import javax.swing.JFrame;
18import tmcsim.cadsimulator.managers.TrafficModelManager;
19import tmcsim.common.SimulationException;
20
21/**
22 * Read and process all Traffic Events in a file and show
23 * resulting highway network.
24 * A snapshot of the highway model after each event is saved in a buffer
25 * and a graphical display allows the user to scroll through the history.
26 * This application is used by Traffic Event authors to assist
27 * in verifying the correctness of their data file.
28 * @author jdalbey
29 */
30public class TrafficEventsAnimator extends javax.swing.JPanel
31{
32    /**
33     * Error logger.
34     */
35    private final static Logger logger = Logger.getLogger("trafficmodeleventdriver");
36
37    /**
38     * Highways in traffic network
39     */
40    final private Highways highways;
41
42    /**
43     * LinkedList of batch events
44     */
45    private final LinkedList<TrafficEvent> eventQueue;
46    /**
47     * Highway history
48     */
49    private List<String> history;
50    /**
51     * name of events file obtained from properties
52     */
53    private String events_file = ""; 
54    /**
55     * Creates the JPanel and builds the highways and events.
56     */
57    public TrafficEventsAnimator() throws RemoteException, SimulationException
58    {
59        initComponents();
60
61        // Initialize the highway model
62        highways = new Highways(
63                "config/vds_data/postmile_coordinates.txt");
64        final String CONFIG_FILE_NAME = "traffic_model_config.properties";
65        String propertiesFile = "config" + System.getProperty("file.separator") 
66                 + "traffic_model_config" + System.getProperty("file.separator")  + CONFIG_FILE_NAME;
67        Properties props = TrafficModelManager.loadProperties(propertiesFile);
68        events_file = props.getProperty("Events_File");
69        FileInputStream fis = null;
70        try
71        {
72            fis = new FileInputStream(events_file);
73        } catch (FileNotFoundException ex)
74        {
75            Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, 
76                    "Missing Traffic Events file " + events_file);
77            System.exit(-1);
78        }
79        Scanner fileScanner = new Scanner(fis);       
80        // Read all lines from the file of events and put in a queue
81        eventQueue = TrafficModelManager.readBatchFile(fileScanner);
82    }
83    /** Load the traffic events into a history buffer.  This  */
84    public void load()
85    {
86        txtDisplay.setText("Loading traffic events from: " + events_file);
87        history = new ArrayList<String>();
88        // If we have any events left to process
89        while (!eventQueue.isEmpty())
90        {
91            // Get next event
92            TrafficEvent nextEvent = eventQueue.peek();
93            System.out.println(nextEvent.eventTime);
94            // apply colorization to highways
95            highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir,
96                    nextEvent.postmile, nextEvent.range, nextEvent.color);
97            // get the highwys as a string and add to the history list
98            history.add(highways.toString()+"\n"+nextEvent.eventTime);
99            // Remove this event from the queue, we're done with it.
100            eventQueue.remove();
101        }
102        scrollBar.setMaximum(history.size());
103        // display the first item from the history
104        txtDisplay.setText(history.get(0));
105    }
106    /**
107     * This method is called from within the constructor to initialize the form.
108     * WARNING: Do NOT modify this code. The content of this method is always
109     * regenerated by the Form Editor.
110     */
111    @SuppressWarnings("unchecked")
112    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
113    private void initComponents() {
114
115        scrollBar = new javax.swing.JScrollBar();
116        jScrollPane1 = new javax.swing.JScrollPane();
117        txtDisplay = new javax.swing.JTextArea();
118
119        scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() {
120            public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {
121                scrollbarValueChanged(evt);
122            }
123        });
124
125        txtDisplay.setColumns(20);
126        txtDisplay.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N
127        txtDisplay.setRows(5);
128        jScrollPane1.setViewportView(txtDisplay);
129
130        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
131        this.setLayout(layout);
132        layout.setHorizontalGroup(
133            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
134            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
135                .addComponent(scrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
136                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
137                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
138        );
139        layout.setVerticalGroup(
140            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
141            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
142            .addComponent(scrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
143        );
144    }// </editor-fold>//GEN-END:initComponents
145
146    private void scrollbarValueChanged(java.awt.event.AdjustmentEvent evt)//GEN-FIRST:event_scrollbarValueChanged
147    {//GEN-HEADEREND:event_scrollbarValueChanged
148        // when the scroll bar is moved show the corresponding snapshot from history
149        txtDisplay.setText(history.get(evt.getValue()) );
150        repaint();
151    }//GEN-LAST:event_scrollbarValueChanged
152
153    /** Entry point for the application.  Builds a surrounding JFrame and
154     * runs the application.
155     * @param args
156     * @throws RemoteException
157     * @throws SimulationException
158     */
159    public static void main(String[] args) throws RemoteException, SimulationException
160    {
161        JFrame frame = new JFrame("Traffic Events Animator");
162        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
163        frame.setSize(1050,450);
164        TrafficEventsAnimator display = new TrafficEventsAnimator();
165        frame.add(display);
166        frame.setVisible(true);
167        display.load();
168    }
169
170    // Variables declaration - do not modify//GEN-BEGIN:variables
171    private javax.swing.JScrollPane jScrollPane1;
172    private javax.swing.JScrollBar scrollBar;
173    private javax.swing.JTextArea txtDisplay;
174    // End of variables declaration//GEN-END:variables
175}
Note: See TracBrowser for help on using the repository browser.