Package trafficjams.model.visualization

Source Code of trafficjams.model.visualization.ControlPanel

package trafficjams.model.visualization;

import jade.core.behaviours.OneShotBehaviour;
import trafficjams.model.factories.RoadMapFactory;
import trafficjams.model.factories.VehicleRegisterFactory;
import trafficjams.model.registers.RoadMap;
import trafficjams.model.registers.TrafficRegister;
import trafficjams.model.registers.VehicleRegister;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* Created by IntelliJ IDEA.
* User: Администратор
* Date: 12.11.11
* Time: 19:18
* To change this template use File | Settings | File Templates.
*/
public class ControlPanel extends JPanel{
    private final JTextField autoCount;
    private final JTextField crossCount;
    private final FileChoozer fileChoozer;

    public ControlPanel(){

        super(new GridLayout());
        this.setSize(1000,700);

        fileChoozer = new FileChoozer((Frame)Visualizator.getInstance(), true);

        JButton bt = new JButton("Open");
        ActionListener al1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showOpenDialog();
            }
        };
        bt.addActionListener(al1);
        this.add(bt);

        bt = new JButton("Save");
        al1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showSaveDialog();
            }
        };
        bt.addActionListener(al1);
        this.add(bt);

        this.add(new JLabel("К-во авто:"));
        this.autoCount = new JTextField("400000");
        autoCount.setSize(200,25);
        this.add(autoCount);

        this.add(new JLabel("К-во перекрестков:"));
        this.crossCount = new JTextField("100");
        crossCount.setSize(200,25);
        this.add(crossCount);

        JButton preview = new JButton("Preview");
        al1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                previewPressed();

            }
        };
        preview.addActionListener(al1);
        this.add(preview);

        JButton start = new JButton("Start");
        al1 = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                startPressed();
            }
        };
        start.addActionListener(al1);
        this.add(start);
    }

    private void startPressed() {
        if (TrafficRegister.getInstance().isInit()){
            Visualizator.getInstance().getHostAgent()
                .addBehaviour( new OneShotBehaviour() {
                                  public void action() {
                                      Visualizator.getInstance().getHostAgent().initTraffic(TrafficRegister.getInstance());
                                  }
                              } );
        }
    }

    private void showOpenDialog() {
        this.fileChoozer.setSave(false);
        this.fileChoozer.show();
    }

    private void showSaveDialog() {
        this.fileChoozer.setSave(true);
        this.fileChoozer.show();
    }

    public void previewPressed() {
        try{
            RoadMap map = null;
            RoadMapFactory fact = RoadMapFactory.getInstance();
            map = fact.makeRoadMap(Integer.parseInt(crossCount.getText())  , Integer.parseInt(crossCount.getText()) , Integer.parseInt(crossCount.getText()));
            TrafficRegister.getInstance().setRoadMap(map);


            VehicleRegister reg = null;
            VehicleRegisterFactory factory = VehicleRegisterFactory.getInstance();
            reg = factory.makeVehicles(map, Integer.parseInt(autoCount.getText())  );
            TrafficRegister.getInstance().setVehicleRegister(reg);


            RoadMapCanvas.getInstance().setRepaintAllowed(true);
            TrafficRegister.getInstance().setInit(true);
        }
        catch (Exception ex){
            TrafficRegister.getInstance().setInit(false);
            TrafficRegister.getInstance().setRoadMap(null);
            TrafficRegister.getInstance().setVehicleRegister(null);
            ex.printStackTrace();
        }
    }
}
TOP

Related Classes of trafficjams.model.visualization.ControlPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.