/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.gui; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.Spring; import javax.swing.SpringLayout; import vnsim.map.object.Globals; public class Statistics extends JPanel { private SpringLayout layout; private JLabel carsNoL, realTimeL, simTimeL, step, nrEvents, semTimeL, selection; public JLabel dse, dsw, dne, dnw; public JLabel cycle, phase1, phase2, phase3, phase4; private JScrollPane details; private JTextArea text; private static final long serialVersionUID = 1L; public Statistics(int w, int h) { super(); layout = new SpringLayout(); realTimeL = new JLabel("Real time: "); simTimeL = new JLabel("Sim. time: "); semTimeL = new JLabel(""); selection = new JLabel(""); carsNoL = new JLabel("Cars no: "); step = new JLabel("Step: "); nrEvents = new JLabel("Events pending: "); text = new JTextArea("No info yet!"); text.setEditable(false); details = new JScrollPane(text); dne = new JLabel(); dnw = new JLabel(); dse = new JLabel(); dsw = new JLabel(); cycle = new JLabel(); phase1 = new JLabel(); phase2 = new JLabel(); phase3 = new JLabel(); phase4 = new JLabel(); this.setSize(w - 1, h - 1); this.setLayout(layout); // adds the components to the frame realTimeL.setSize(w, h / 8 - 1); carsNoL.setSize(w, h / 8 - 1); simTimeL.setSize(w, h / 8 - 1); semTimeL.setSize(w, h / 8 - 1); selection.setSize(w, h / 8 - 1); step.setSize(w, h / 8 - 1); nrEvents.setSize(w, h / 8 - 1); details.setSize(w, h / 2 - 1); details.setAutoscrolls(true); dse.setSize(w, h / 8 - 1); dsw.setSize(w, h / 8 - 1); dne.setSize(w, h / 8 - 1); dnw.setSize(w, h / 8 - 1); cycle.setSize(w, h / 8 - 1); phase1.setSize(w, h / 8 - 1); phase2.setSize(w, h / 8 - 1); phase3.setSize(w, h / 8 - 1); phase4.setSize(w, h / 8 - 1); SpringLayout.Constraints c1 = layout.getConstraints(realTimeL); SpringLayout.Constraints c2 = layout.getConstraints(simTimeL); SpringLayout.Constraints c3 = layout.getConstraints(step); SpringLayout.Constraints c4 = layout.getConstraints(nrEvents); SpringLayout.Constraints c5 = layout.getConstraints(details); SpringLayout.Constraints c6 = layout.getConstraints(carsNoL); SpringLayout.Constraints c11 = layout.getConstraints(semTimeL); SpringLayout.Constraints c12 = layout.getConstraints(selection); SpringLayout.Constraints c7 = layout.getConstraints(dse); SpringLayout.Constraints c8 = layout.getConstraints(dsw); SpringLayout.Constraints c9 = layout.getConstraints(dne); SpringLayout.Constraints c10 = layout.getConstraints(dnw); SpringLayout.Constraints c13 = layout.getConstraints(cycle); SpringLayout.Constraints c14 = layout.getConstraints(phase1); SpringLayout.Constraints c15 = layout.getConstraints(phase2); SpringLayout.Constraints c16 = layout.getConstraints(phase3); SpringLayout.Constraints c17 = layout.getConstraints(phase4); c1.setX(Spring.constant(1)); c1.setY(Spring.constant(1)); // c3.setWidth(Spring.constant((screenSize.width * 6) / 7)); // c4.setHeight(Spring.constant((screenSize.height * 6) / 7)); c2.setX(Spring.constant(1)); c2.setY(Spring.constant(1 + h / 32)); c3.setX(Spring.constant(1)); c3.setY(Spring.constant(1 + 2 * h / 32)); c4.setX(Spring.constant(1)); c4.setY(Spring.constant(1 + 3 * h / 32)); c6.setX(Spring.constant(1)); c6.setY(Spring.constant(1 + 4 * h / 32)); c12.setX(Spring.constant(1)); c12.setY(Spring.constant(1 + 6 * h / 32)); c7.setX(Spring.constant(1)); c7.setY(Spring.constant(1 + 7 * h / 32)); c8.setX(Spring.constant(1)); c8.setY(Spring.constant(1 + 8 * h / 32)); c9.setX(Spring.constant(1)); c9.setY(Spring.constant(1 + 9 * h / 32)); c10.setX(Spring.constant(1)); c10.setY(Spring.constant(1 + 10 * h / 32)); c11.setX(Spring.constant(1)); c11.setY(Spring.constant(1 + 11 * h / 32)); c13.setX(Spring.constant(1)); c13.setY(Spring.constant(1 + 13 * h / 32)); c14.setX(Spring.constant(1)); c14.setY(Spring.constant(1 + 14 * h / 32)); c15.setX(Spring.constant(1)); c15.setY(Spring.constant(1 + 15 * h / 32)); c16.setX(Spring.constant(1)); c16.setY(Spring.constant(1 + 16 * h / 32)); c17.setX(Spring.constant(1)); c17.setY(Spring.constant(1 + 17 * h / 32)); c5.setX(Spring.constant(1)); c5.setY(Spring.constant(1 + 20 * h / 32)); c5.setWidth(Spring.constant(w)); c5.setHeight(Spring.constant(10 * h / 32)); this.add(carsNoL); this.add(realTimeL); this.add(simTimeL); this.add(selection); this.add(step); this.add(semTimeL); this.add(nrEvents); this.add(details); this.add(dse); this.add(dsw); this.add(dne); this.add(dnw); this.add(cycle); this.add(phase1); this.add(phase2); this.add(phase3); this.add(phase4); this.setVisible(true); } public void resetLabels() { Globals.demo.st.dsw.setText(""); Globals.demo.st.dnw.setText(""); Globals.demo.st.dse.setText(""); Globals.demo.st.dne.setText(""); Globals.demo.st.cycle.setText(""); Globals.demo.st.phase1.setText(""); Globals.demo.st.phase2.setText(""); Globals.demo.st.phase3.setText(""); Globals.demo.st.phase4.setText(""); Globals.demo.st.repaint(); } public void setCurrentTime(long l) { long ms = l % 1000; l /= 1000; long sec = l % 60; l /= 60; long min = l % 60; l /= 60; this.realTimeL.setText("Real time: " + l + ":" + min + ":" + sec + "." + ms); this.repaint(); } public void setSelection(String s) { this.selection.setText(s); this.repaint(); } public void setSemTime(int color, int remaining) { // long ms = l % 1000; // l /= 1000; // long sec = l % 60; // l /= 60; // long min = l % 60; // l /= 60; // this.semTimeL.setText("Lights time: " +l+":"+min+":"+ sec+"."+ ms); if (color == -1) { this.semTimeL.setText(" "); } else if (color == Globals.GREEN) this.semTimeL.setText("Green: " + remaining); else if (remaining < 0) this.semTimeL.setText("Red: +- " + (-remaining)); else this.semTimeL.setText("Red: " + remaining); this.repaint(); } public void setCount(long l) { this.step.setText("Step: " + l); this.repaint(); } public void setSimulationTime(float f) { long time = (long) f; long ms = time % 1000; time /= 1000; long sec = time % 60; time /= 60; long min = time % 60; time /= 60; this.simTimeL.setText("Sim. time: " + time + ":" + min + ":" + sec + "." + ms); this.repaint(); } public void addInfo(String s) { this.text.append("\n" + s); this.repaint(); } public void clearInfo() { this.text.setText(""); this.repaint(); } public void setNrEvents(int n) { this.nrEvents.setText("Events pending: " + n); this.repaint(); } public void setDemand(String seg, int n, int n2, boolean TL) { if (TL) { if (seg.equals("sw")) this.dsw.setText("SW: " + n + "/" + n2 + " veh/h"); if (seg.equals("se")) this.dse.setText("SE: " + n + "/" + n2 + " veh/h"); if (seg.equals("ne")) this.dne.setText("NE: " + n + "/" + n2 + " veh/h"); if (seg.equals("nw")) this.dnw.setText("NW: " + n + "/" + n2 + " veh/h"); } else { if (seg.equals("sw")) { this.dsw.setText("Queue size: " + n + " m"); } if (seg.equals("ne")) this.dne.setText("Speed: " + n + " km/h"); if (seg.equals("nw")) this.dnw.setText("Suggested: " + n + " km/h"); } this.repaint(); } public void setCarsNo(int n) { this.carsNoL.setText("Cars no: " + n); this.repaint(); } }