Taxi Layout

by Dave on March 20th, 2012
No notes
Syntax: Java
Show lines - Hide lines - Show in textbox - Download
package taxi;
 
import java.awt.BorderLayout;
 
public class TaxiServiceDispatchCentre extends JFrame {
 
	private JPanel contentPane;
	public JList listIncoming;
	public JList listDowntown;
	public JList listAirport;
	public JList listNorth;
	public JList listSouth;
	public JList listEast;
	public JList listWest;
	public static DispatchCenter model;
	//public java.util.Timer clientDispatch = new java.util.Timer();
	public Timer clientDispatcher;
 
 
 
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TaxiServiceDispatchCentre frame = new TaxiServiceDispatchCentre();
 
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
 
	/**
	 * Create the frame.
	 */
	public TaxiServiceDispatchCentre() {
		setTitle("Taxi Service Dispatch Center");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 965, 494);
 
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
 
		JMenu muSimulation = new JMenu("Simulation");
		menuBar.add(muSimulation);
 
		JMenuItem mIStart = new JMenuItem("Start Clients Coming");
		mIStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clientDispatcher.start();
			}
		});
		muSimulation.add(mIStart);
 
		JMenuItem mIStop = new JMenuItem("Stop Clients From Coming");
		mIStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clientDispatcher.stop();
			}
		});
		muSimulation.add(mIStop);
 
		JMenu muSettings = new JMenu("Settings");
		menuBar.add(muSettings);
 
		JMenuItem mISim = new JMenuItem("Simulation Rate");
		mISim.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane pane = new JOptionPane("Input");
				pane.setWantsInput(true);
				String input = pane.showInputDialog("Iterations Per Second (1 to 20)");
				int delay = 1000/(Integer.parseInt(input));
				if(Integer.parseInt(input) > 0 && Integer.parseInt(input) <= 20  ){
					clientDispatcher.setDelay(delay);
					System.out.println(delay + "");
				}
				else{
					JOptionPane.showMessageDialog(pane,
						    "Choose a valid number",
						    "Inane error",
						    JOptionPane.ERROR_MESSAGE);
 
				}
 
 
 
 
			}
		});
		muSettings.add(mISim);
 
		JMenu muAdmin = new JMenu("Admin");
		menuBar.add(muAdmin);
 
		JMenuItem mIStats = new JMenuItem("Statistics");
		muAdmin.add(mIStats);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{259, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
 
		JPanel panel_1 = new JPanel();
		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
		gbc_panel_1.insets = new Insets(0, 0, 5, 5);
		gbc_panel_1.fill = GridBagConstraints.BOTH;
		gbc_panel_1.gridx = 0;
		gbc_panel_1.gridy = 0;
		contentPane.add(panel_1, gbc_panel_1);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[]{0, 0};
		gbl_panel_1.rowHeights = new int[]{0, 0, 0};
		gbl_panel_1.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_1.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_1.setLayout(gbl_panel_1);
 
		JLabel lblIncoming = new JLabel("Incoming");
		lblIncoming.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblIncoming = new GridBagConstraints();
		gbc_lblIncoming.fill = GridBagConstraints.BOTH;
		gbc_lblIncoming.insets = new Insets(0, 0, 5, 0);
		gbc_lblIncoming.gridx = 0;
		gbc_lblIncoming.gridy = 0;
		panel_1.add(lblIncoming, gbc_lblIncoming);
 
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		panel_1.add(scrollPane, gbc_scrollPane);
 
		listIncoming = new JList();
		scrollPane.setViewportView(listIncoming);
 
		Component verticalStrut_1 = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
		gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 5);
		gbc_verticalStrut_1.gridx = 1;
		gbc_verticalStrut_1.gridy = 0;
		contentPane.add(verticalStrut_1, gbc_verticalStrut_1);
 
		Component verticalStrut = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.insets = new Insets(0, 0, 5, 5);
		gbc_verticalStrut.gridx = 2;
		gbc_verticalStrut.gridy = 0;
		contentPane.add(verticalStrut, gbc_verticalStrut);
 
		JPanel panel_2 = new JPanel();
		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
		gbc_panel_2.insets = new Insets(0, 0, 5, 5);
		gbc_panel_2.fill = GridBagConstraints.BOTH;
		gbc_panel_2.gridx = 3;
		gbc_panel_2.gridy = 0;
		contentPane.add(panel_2, gbc_panel_2);
		GridBagLayout gbl_panel_2 = new GridBagLayout();
		gbl_panel_2.columnWidths = new int[]{0, 0};
		gbl_panel_2.rowHeights = new int[]{0, 0, 0};
		gbl_panel_2.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_2.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_2.setLayout(gbl_panel_2);
 
		JLabel lblDowntown = new JLabel("Downtown");
		GridBagConstraints gbc_lblDowntown = new GridBagConstraints();
		gbc_lblDowntown.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblDowntown.insets = new Insets(0, 0, 5, 0);
		gbc_lblDowntown.gridx = 0;
		gbc_lblDowntown.gridy = 0;
		panel_2.add(lblDowntown, gbc_lblDowntown);
 
		JScrollPane scrollPane_1 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.gridx = 0;
		gbc_scrollPane_1.gridy = 1;
		panel_2.add(scrollPane_1, gbc_scrollPane_1);
 
		listDowntown = new JList();
		scrollPane_1.setViewportView(listDowntown);
 
		JPanel panel_3 = new JPanel();
		GridBagConstraints gbc_panel_3 = new GridBagConstraints();
		gbc_panel_3.insets = new Insets(0, 0, 5, 5);
		gbc_panel_3.fill = GridBagConstraints.BOTH;
		gbc_panel_3.gridx = 4;
		gbc_panel_3.gridy = 0;
		contentPane.add(panel_3, gbc_panel_3);
		GridBagLayout gbl_panel_3 = new GridBagLayout();
		gbl_panel_3.columnWidths = new int[]{0, 0};
		gbl_panel_3.rowHeights = new int[]{0, 0, 0};
		gbl_panel_3.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_3.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_3.setLayout(gbl_panel_3);
 
		JLabel label_2 = new JLabel("Airport");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_label_2.insets = new Insets(0, 0, 5, 0);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 0;
		panel_3.add(label_2, gbc_label_2);
 
		JScrollPane scrollPane_2 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
		gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_2.gridx = 0;
		gbc_scrollPane_2.gridy = 1;
		panel_3.add(scrollPane_2, gbc_scrollPane_2);
 
		listAirport = new JList();
		scrollPane_2.setViewportView(listAirport);
 
		JPanel panel_4 = new JPanel();
		GridBagConstraints gbc_panel_4 = new GridBagConstraints();
		gbc_panel_4.insets = new Insets(0, 0, 5, 5);
		gbc_panel_4.fill = GridBagConstraints.BOTH;
		gbc_panel_4.gridx = 5;
		gbc_panel_4.gridy = 0;
		contentPane.add(panel_4, gbc_panel_4);
		GridBagLayout gbl_panel_4 = new GridBagLayout();
		gbl_panel_4.columnWidths = new int[]{0, 0};
		gbl_panel_4.rowHeights = new int[]{0, 0, 0};
		gbl_panel_4.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_4.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_4.setLayout(gbl_panel_4);
 
		JLabel lblNorth = new JLabel("North");
		GridBagConstraints gbc_lblNorth = new GridBagConstraints();
		gbc_lblNorth.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblNorth.insets = new Insets(0, 0, 5, 0);
		gbc_lblNorth.gridx = 0;
		gbc_lblNorth.gridy = 0;
		panel_4.add(lblNorth, gbc_lblNorth);
 
		JScrollPane scrollPane_3 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_3 = new GridBagConstraints();
		gbc_scrollPane_3.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_3.gridx = 0;
		gbc_scrollPane_3.gridy = 1;
		panel_4.add(scrollPane_3, gbc_scrollPane_3);
 
		listNorth = new JList();
		scrollPane_3.setViewportView(listNorth);
 
		JPanel panel_5 = new JPanel();
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.insets = new Insets(0, 0, 5, 5);
		gbc_panel_5.fill = GridBagConstraints.BOTH;
		gbc_panel_5.gridx = 6;
		gbc_panel_5.gridy = 0;
		contentPane.add(panel_5, gbc_panel_5);
		GridBagLayout gbl_panel_5 = new GridBagLayout();
		gbl_panel_5.columnWidths = new int[]{0, 0};
		gbl_panel_5.rowHeights = new int[]{0, 0, 0};
		gbl_panel_5.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_5.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_5.setLayout(gbl_panel_5);
 
		JLabel lblSouth = new JLabel("South");
		GridBagConstraints gbc_lblSouth = new GridBagConstraints();
		gbc_lblSouth.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblSouth.insets = new Insets(0, 0, 5, 0);
		gbc_lblSouth.gridx = 0;
		gbc_lblSouth.gridy = 0;
		panel_5.add(lblSouth, gbc_lblSouth);
 
//		JLabel lblSouth = new JLabel("South");
//		GridBagConstraints gbc_lblSouth = new GridBagConstraints();
//		gbc_lblSouth.fill = GridBagConstraints.HORIZONTAL;
//		gbc_lblSouth.insets = new Insets(0, 0, 5, 0);
//		gbc_lblSouth.gridx = 0;
//		gbc_lblSouth.gridy = 0;
//		panel_5.add(lblSouth, gbc_lblSouth);
 
		JScrollPane scrollPane_4 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_4 = new GridBagConstraints();
		gbc_scrollPane_4.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_4.gridx = 0;
		gbc_scrollPane_4.gridy = 1;
		panel_5.add(scrollPane_4, gbc_scrollPane_4);
 
		listSouth = new JList();
		scrollPane_4.setViewportView(listSouth);
 
		JPanel panel_6 = new JPanel();
		GridBagConstraints gbc_panel_6 = new GridBagConstraints();
		gbc_panel_6.insets = new Insets(0, 0, 5, 5);
		gbc_panel_6.fill = GridBagConstraints.BOTH;
		gbc_panel_6.gridx = 7;
		gbc_panel_6.gridy = 0;
		contentPane.add(panel_6, gbc_panel_6);
		GridBagLayout gbl_panel_6 = new GridBagLayout();
		gbl_panel_6.columnWidths = new int[]{0, 0};
		gbl_panel_6.rowHeights = new int[]{0, 0, 0};
		gbl_panel_6.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_6.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel_6.setLayout(gbl_panel_6);
 
		JLabel lblEast = new JLabel("East");
		GridBagConstraints gbc_lblEast = new GridBagConstraints();
		gbc_lblEast.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblEast.insets = new Insets(0, 0, 5, 0);
		gbc_lblEast.gridx = 0;
		gbc_lblEast.gridy = 0;
		panel_6.add(lblEast, gbc_lblEast);
 
		JScrollPane scrollPane_5 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_5 = new GridBagConstraints();
		gbc_scrollPane_5.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_5.gridx = 0;
		gbc_scrollPane_5.gridy = 1;
		panel_6.add(scrollPane_5, gbc_scrollPane_5);
 
		listEast = new JList();
		scrollPane_5.setViewportView(listEast);
 
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 8;
		gbc_panel.gridy = 0;
		contentPane.add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
 
		JLabel lblWest = new JLabel("West");
		lblWest.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblWest = new GridBagConstraints();
		gbc_lblWest.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblWest.insets = new Insets(0, 0, 5, 0);
		gbc_lblWest.gridx = 0;
		gbc_lblWest.gridy = 0;
		panel.add(lblWest, gbc_lblWest);
 
		JScrollPane scrollPane_6 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_6 = new GridBagConstraints();
		gbc_scrollPane_6.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_6.gridx = 0;
		gbc_scrollPane_6.gridy = 1;
		panel.add(scrollPane_6, gbc_scrollPane_6);
 
		listWest = new JList();
		scrollPane_6.setViewportView(listWest);
 
		JButton btnDispatch = new JButton("Dispatch");
		btnDispatch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});
		GridBagConstraints gbc_btnDispatch = new GridBagConstraints();
		gbc_btnDispatch.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDispatch.insets = new Insets(0, 0, 0, 5);
		gbc_btnDispatch.gridx = 0;
		gbc_btnDispatch.gridy = 1;
		contentPane.add(btnDispatch, gbc_btnDispatch);
 
		model = new DispatchCenter();
		//clientDispatch.addTimerEvent(new )
		clientDispatcher = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleTimerTick();
			}
		});
		clientDispatcher.start();
		update();
	}
 
	public void update(){
		listDowntown.setListData(model.taxisDT.toArray());
		listAirport.setListData(model.taxisAP.toArray());
		listNorth.setListData(model.taxisN.toArray());
		listEast.setListData(model.taxisE.toArray());
		listWest.setListData(model.taxisW.toArray());
		listSouth.setListData(model.taxisS.toArray());
		listIncoming.setListData(model.queue.toArray());
	}
 
	public void handleTimerTick(){
 
		int pickup= (int) (Math.random()*6 + 1);
		int dropoff= (int) (Math.random()*6 + 1);
		String pickupLocation = new String();
		String dropoffLocation = new String();
		switch (pickup) {
		case 1:
			pickupLocation ="Downtown";
			break;
 
		case 2:
			pickupLocation ="Airport";
			break;
		case 3:
			pickupLocation ="North";
			break;
		case 4:
			pickupLocation ="West";
			break;
		case 5:
			pickupLocation ="East";
			break;
		case 6:
			pickupLocation ="South";
			break;
 
		}
 
		switch(dropoff){
		case 1:
			dropoffLocation = "Downtown";
			break;
		case 2:
			dropoffLocation = "Airport";
			break;
		case 3:
			dropoffLocation = "North";
			break;
		case 4:
			dropoffLocation = "West";
			break;
		case 5:
			dropoffLocation = "East";
			break;
		case 6:
			dropoffLocation = "South";
			break;
 
		}
 
		model.addRequest(pickupLocation, dropoffLocation);
		update();
	}
}
 

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS