Package crossroad

Source Code of crossroad.CrossroadView

/*
* CrossroadView.java
*/

package crossroad;

import java.awt.Color;
import java.awt.Graphics;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.TreeSet;
import java.util.concurrent.Semaphore;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
*
* @author shajen
*/
public class CrossroadView extends FrameView {
    /**
     *
     */
    public static final int ROAD_DOWN=1;
    /**
     *
     */
    public static final int ROAD_UP=2;
    /**
     *
     */
    public static final int ROAD_LEFT=3;
    /**
     *
     */
    public static final int ROAD_RIGHT=4;
   
    /**
     *
     */
    public static final int MAX_CAR=20;

    /**
     *
     */
    public static HashMap <Integer, TreeSet<Car> > roads = new HashMap<Integer, TreeSet<Car> >();
    /**
     *
     */
    public static Car carOnCrossRoad=null;

    private int sizeX,sizeY;
    private int roadWidthVertical, stripWidthVertical, stripSpaceVertical,roadStartVertical;
    private int roadWidthHorizontal, stripWidthHorizontal, stripSpaceHorizontal,roadStartHorizontal;
    private int carWidthVertical,carHeightVertical;
    private int carWidthHorizontal,carHeightHorizontal;
    private int signpostSize;

    private Timer t1,t2,t3;
   
    /**
     *
     */
    public static final Semaphore access = new Semaphore(1, true);

    /**
     *
     * @param app
     */
    public CrossroadView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        roads.put(ROAD_DOWN, new TreeSet<Car>());
        roads.put(ROAD_UP, new TreeSet<Car>());
        roads.put(ROAD_LEFT, new TreeSet<Car>());
        roads.put(ROAD_RIGHT, new TreeSet<Car>());

        t1 = new javax.swing.Timer(40, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              try {
                access.acquire();
                mainPanel.repaint();
                access.release();
              }
              catch (Exception ex) {
              }
          }
        });
        t1.start();

        t2 = new javax.swing.Timer(100, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              try {
                access.acquire();
                genCar();
                access.release();
              }
              catch (Exception ex) {
              }
          }
        });
        t2.start();

        t3 = new javax.swing.Timer(40, new ActionListener() {
          public void actionPerformed(ActionEvent e) {
              try {
                access.acquire();
                runCar();
                access.release();
              }
              catch (Exception ex) {
              }
          }
        });
        t3.start();
    }

    /**
     *
     * @param car
     */
    public static void addCar(Car car) {
        TreeSet<Car> ts=new TreeSet<Car>();
        switch (car.road) {
            case ROAD_DOWN: ts=roads.get(ROAD_DOWN); break;
            case ROAD_UP: ts=roads.get(ROAD_UP); break;
            case ROAD_LEFT: ts=roads.get(ROAD_LEFT); break;
            case ROAD_RIGHT: ts=roads.get(ROAD_RIGHT); break;
        }
        try {
            Car car2 = ts.last();
            if (car2!=null && car.positionRoad<=car2.positionRoad) {
                car.positionRoad=car2.positionRoad + 0.01f;
                ts.add(car);
            }
            else
                ts.add(car);
        }
        catch (Exception ex) {
            ts.add(car);
        }
    }

    /**
     *
     * @param road
     * @return
     */
    public static boolean isFree(int road) {
        try {
            Car car = new Car(0);
            car.positionRoad=-0.000001f;
            Car car2 = roads.get(road).higher(car);
            return car2==null || !(car2.positionRoad<=0.2f);
        }
        catch (NoSuchElementException ex) {
            return true;
        }
    }

    /**
     *
     * @param road
     * @return
     */
    public static boolean isFreeToRun(int road) {
        try {
            Car car = new Car(0);
            car.positionRoad=-0.000001f;
            Car car2 = roads.get(road).lower(car);
            return car2==null || !(car2.positionRoad>=-0.3f);
        }
        catch (NoSuchElementException ex) {
            return true;
        }
    }

    private void deadlock() {
        Car car[] = new Car[4];
        Car bufCar = new Car(0);
        bufCar.positionRoad=-0.000001f;
        int i=0;
        try {
            Car bufCar2=roads.get(ROAD_DOWN).higher(bufCar);
            if (bufCar2.priorityWait)
                car[i++]=bufCar2;
        }
        catch (Exception ex) {
        }
        try {
            Car bufCar2=roads.get(ROAD_UP).higher(bufCar);
            if (bufCar2.priorityWait)
                car[i++]=bufCar2;
        }
        catch (Exception ex) {
        }
        try {
            Car bufCar2=roads.get(ROAD_LEFT).higher(bufCar);
            if (bufCar2.priorityWait)
                car[i++]=bufCar2;
        }
        catch (Exception ex) {
        }
        try {
            Car bufCar2=roads.get(ROAD_RIGHT).higher(bufCar);
            if (bufCar2.priorityWait)
                car[i++]=bufCar2;
        }
        catch (Exception ex) {
        }
       
        if (i>1) {
            boolean isDeadlock=true;
            for (int i2=0; i2<i; ++i2)
                if (car[i2].priorityFree)
                    isDeadlock=false;
           
            if (isDeadlock) {
                int selectedCar=0;
                for (int i2=1; i2<i; ++i2)
                    if (car[i2].stopTime<car[selectedCar].stopTime)
                        selectedCar=i2;
                car[selectedCar].priorityForce=true;
            }
        }
    }

    private void paintRoad(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillRect(0, 0, sizeX, sizeY);

        g.setColor(Color.DARK_GRAY);
        g.fillRect(roadStartVertical, 0, roadWidthVertical, sizeY);
        g.fillRect(0, roadStartHorizontal, sizeX, roadWidthHorizontal);

        g.setColor(Color.WHITE);
        g.fillRect(roadStartVertical+(roadWidthVertical-stripWidthVertical-stripSpaceVertical)/2, 0, stripWidthVertical, roadStartHorizontal);
        g.fillRect(roadStartVertical+(roadWidthVertical-stripWidthVertical+stripSpaceVertical)/2, 0, stripWidthVertical, roadStartHorizontal);
        g.fillRect(roadStartVertical+(roadWidthVertical-stripWidthVertical-stripSpaceVertical)/2, roadStartHorizontal+roadWidthHorizontal, stripWidthVertical, sizeY);
        g.fillRect(roadStartVertical+(roadWidthVertical-stripWidthVertical+stripSpaceVertical)/2, roadStartHorizontal+roadWidthHorizontal, stripWidthVertical, sizeY);

        g.fillRect(0, roadStartHorizontal+(roadWidthHorizontal-stripWidthHorizontal-stripSpaceHorizontal)/2, roadStartVertical, stripWidthHorizontal);
        g.fillRect(0, roadStartHorizontal+(roadWidthHorizontal-stripWidthHorizontal+stripSpaceHorizontal)/2, roadStartVertical, stripWidthHorizontal);
        g.fillRect(roadStartVertical+roadWidthVertical, roadStartHorizontal+(roadWidthHorizontal-stripWidthHorizontal-stripSpaceHorizontal)/2, sizeX, stripWidthHorizontal);
        g.fillRect(roadStartVertical+roadWidthVertical, roadStartHorizontal+(roadWidthHorizontal-stripWidthHorizontal+stripSpaceHorizontal)/2, sizeX, stripWidthHorizontal);
    }

    /**
     *
     * @param g
     */
    public void paintCrossroadCar(Graphics g) {
        Car car=carOnCrossRoad;
        g.setColor(car.color);
        int buf;
        switch (car.road) {
            case ROAD_DOWN:
                buf=(roadWidthVertical+stripWidthVertical+stripSpaceVertical)/2;
                g.fillRect(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2, roadStartHorizontal - carHeightHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad*-1.0f+1.0f))), carWidthVertical, carHeightVertical);
                g.setColor(car.signpostColor);
                if (car.signpostLeft)
                    g.fillRect(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2 - signpostSize/2, roadStartHorizontal - carHeightHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad*-1.0f+1.0f))) - signpostSize/2, signpostSize, signpostSize);
                if (car.signpostRight)
                    g.fillRect(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2 + carWidthVertical - signpostSize/2, roadStartHorizontal - carHeightHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad*-1.0f+1.0f))) - signpostSize/2, signpostSize, signpostSize);
                break;
            case ROAD_UP:
                buf=(roadWidthVertical+stripWidthVertical+stripSpaceVertical)/2;
                g.fillRect(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2, roadStartHorizontal - carHeightHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad))), carWidthVertical, carHeightVertical);
                g.setColor(car.signpostColor);
                if (car.signpostLeft)
                    g.fillRect(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2 + carWidthVertical - signpostSize/2, roadStartHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad))) - signpostSize/2, signpostSize, signpostSize);
                if (car.signpostRight)
                g.fillRect(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2 - signpostSize/2, roadStartHorizontal + ((int)((roadWidthHorizontal+carHeightVertical)*(car.positionCrossroad))) - signpostSize/2, signpostSize, signpostSize);
                break;
            case ROAD_RIGHT:
                buf=(roadWidthHorizontal+stripWidthHorizontal+stripSpaceHorizontal)/2;
                g.fillRect(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad*-1.0f+1.0f))), roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                g.setColor(car.signpostColor);
                if (car.signpostLeft)
                    g.fillOval(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad*-1.0f+1.0f)))-signpostSize/2, roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2+carWidthHorizontal, signpostSize, signpostSize);
                if (car.signpostRight)
                    g.fillOval(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad*-1.0f+1.0f)))-signpostSize/2, roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2, signpostSize, signpostSize);
                break;
            case ROAD_LEFT:
                buf=(roadWidthHorizontal+stripWidthHorizontal+stripSpaceHorizontal)/2;
                g.fillRect(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad))), roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                g.setColor(car.signpostColor);
                if (car.signpostLeft)
                    g.fillOval(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad)))-signpostSize/2+carHeightHorizontal, roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2, signpostSize, signpostSize);
                if (car.signpostRight)
                    g.fillOval(roadStartVertical-carHeightHorizontal+((int)((roadWidthVertical+carHeightHorizontal)*(car.positionCrossroad)))-signpostSize/2+carHeightHorizontal, roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2+carWidthHorizontal, signpostSize, signpostSize);
                break;
        }
    }
   
    private void paintCar(Graphics g) {
        try {
            TreeSet <Car> ts = roads.get(ROAD_DOWN);
            Car car=ts.first();
            while (car!=null) {
                int buf=(roadWidthVertical+stripWidthVertical+stripSpaceVertical)/2;
                g.setColor(car.color);
                if (car.positionRoad>=0.0f) {
                    g.fillRect(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2, roadStartHorizontal+roadWidthHorizontal+((int)(roadStartHorizontal*car.positionRoad)), carWidthVertical, carHeightVertical);
                    g.setColor(car.signpostColor);
                    if (car.signpostLeft)
                        g.fillOval(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2 - signpostSize/2, roadStartHorizontal+roadWidthHorizontal+((int)(roadStartHorizontal*car.positionRoad)) - signpostSize/2, signpostSize, signpostSize);
                    if (car.signpostRight)
                        g.fillOval(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2 + carWidthVertical - signpostSize/2, roadStartHorizontal+roadWidthHorizontal+((int)(roadStartHorizontal*car.positionRoad))  - signpostSize/2, signpostSize, signpostSize);
                }
                else
                    g.fillRect(roadStartVertical+buf+(roadWidthVertical-buf-carWidthVertical)/2, ((int)((roadStartHorizontal-carHeightVertical)*(car.positionRoad+1.0f))), carWidthVertical, carHeightVertical);
                car=ts.higher(car);
            }
        }
        catch (NoSuchElementException ex) {
        }

        try {
            TreeSet <Car> ts = roads.get(ROAD_UP);
            Car car=ts.first();
            while (car!=null) {
                int buf=(roadWidthVertical+stripWidthVertical+stripSpaceVertical)/2;
                g.setColor(car.color);
                if (car.positionRoad>=0.0f) {
                    g.fillRect(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2, ((int)((roadStartHorizontal-carHeightVertical)*(car.positionRoad*-1.0f+1.0f))), carWidthVertical, carHeightVertical);
                    g.setColor(car.signpostColor);
                    if (car.signpostLeft)
                        g.fillOval(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2 - signpostSize/2 + carWidthVertical, ((int)((roadStartHorizontal-carHeightVertical)*(car.positionRoad*-1.0f+1.0f))) - signpostSize/2 + carHeightVertical, signpostSize, signpostSize);
                    if (car.signpostRight)
                        g.fillOval(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2 - signpostSize/2, ((int)((roadStartHorizontal-carHeightVertical)*(car.positionRoad*-1.0f+1.0f))) - signpostSize/2 + carHeightVertical, signpostSize, signpostSize);
                }
                else
                    g.fillRect(roadStartVertical+(roadWidthVertical-buf-carWidthVertical)/2, roadStartHorizontal+roadWidthHorizontal+((int)(roadStartHorizontal*(car.positionRoad*-1.0f))), carWidthVertical, carHeightVertical);
                car=ts.higher(car);
            }
        }
        catch (NoSuchElementException ex) {
        }

        try {
            TreeSet <Car> ts = roads.get(ROAD_LEFT);
            Car car=ts.first();
            while (car!=null) {
                int buf=(roadWidthHorizontal+stripWidthHorizontal+stripSpaceHorizontal)/2;
                g.setColor(car.color);
                if (car.positionRoad>=0.0f) {
                    g.fillRect(((int)((roadStartVertical-carHeightHorizontal)*(car.positionRoad*-1.0f+1.0f))), roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                    g.setColor(car.signpostColor);
                    if (car.signpostLeft)
                        g.fillOval(((int)((roadStartVertical-carHeightHorizontal)*(car.positionRoad*-1.0f+1.0f))) + carHeightHorizontal-signpostSize/2, roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2 - signpostSize/2, signpostSize, signpostSize);
                    if (car.signpostRight)
                        g.fillOval(((int)((roadStartVertical-carHeightHorizontal)*(car.positionRoad*-1.0f+1.0f))) + carHeightHorizontal-signpostSize/2, roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2 - signpostSize/2 + carWidthHorizontal, signpostSize, signpostSize);
                }
                else
                    g.fillRect(roadStartVertical+roadWidthVertical+((int)(roadStartVertical*(car.positionRoad*-1.0f))), roadStartHorizontal+buf+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                car=ts.higher(car);
            }
        }
        catch (NoSuchElementException ex) {
        }

        try {
            TreeSet <Car> ts = roads.get(ROAD_RIGHT);
            Car car=ts.first();
            while (car!=null) {
                int buf=(roadWidthHorizontal+stripWidthHorizontal+stripSpaceHorizontal)/2;
                g.setColor(car.color);
                if (car.positionRoad>=0.0f) {
                    g.fillRect(roadStartVertical+roadWidthVertical+((int)((roadStartVertical)*car.positionRoad)), roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                    g.setColor(car.signpostColor);
                    if (car.signpostLeft)
                        g.fillOval(roadStartVertical+roadWidthVertical+((int)((roadStartVertical)*car.positionRoad))-signpostSize/2, roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2+carWidthHorizontal, signpostSize, signpostSize);
                    if (car.signpostRight)
                        g.fillOval(roadStartVertical+roadWidthVertical+((int)((roadStartVertical)*car.positionRoad))-signpostSize/2, roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2-signpostSize/2, signpostSize, signpostSize);
                }
                else
                    g.fillRect(((int)((roadStartVertical-carHeightHorizontal)*(car.positionRoad+1.0f))), roadStartHorizontal+(roadWidthHorizontal-buf-carWidthHorizontal)/2, carHeightHorizontal, carWidthHorizontal);
                car=ts.higher(car);
            }
        }
        catch (NoSuchElementException ex) {
        }
    }

    private void calculate() {
        sizeX=mainPanel.getWidth();
        sizeY=mainPanel.getHeight();

        roadWidthVertical=sizeX/21;
        stripWidthVertical=roadWidthVertical/14;
        stripSpaceVertical=stripWidthVertical*2;
        roadStartVertical=(sizeX*10)/21;

        //roadWidthHorizontal=sizeY/21;
        //stripWidthHorizontal=roadWidthHorizontal/14;
        //stripSpaceHorizontal=stripWidthHorizontal*2;
        //roadStartHorizontal=(sizeY*10)/21;

        roadWidthHorizontal=roadWidthVertical;
        stripWidthHorizontal=stripWidthVertical;
        stripSpaceHorizontal=stripSpaceVertical;
        roadStartHorizontal=(sizeY*10)/21;

        //carHeightHorizontal=roadStartVertical/15;
        carWidthHorizontal=roadWidthHorizontal/4;
        carHeightHorizontal=carWidthHorizontal*3/2;

        //carHeightVertical=roadStartHorizontal/15;
        carWidthVertical=roadWidthVertical/4;
        carHeightVertical=carWidthVertical*3/2;

        signpostSize=6;
    }

    /**
     *
     * @param g
     */
    public void myPaint(Graphics g) {
        calculate();
        paintRoad(g);
        paintCar(g);
        if (carOnCrossRoad!=null)
            paintCrossroadCar(g);
    }

    private void genCar() {
        int curCar=0;
        curCar+=roads.get(ROAD_DOWN).size();
        curCar+=roads.get(ROAD_UP).size();
        curCar+=roads.get(ROAD_LEFT).size();
        curCar+=roads.get(ROAD_RIGHT).size();

        Random rn = new Random();
        if (curCar<MAX_CAR)
            if (rn.nextInt(6)==0) {
                int road=rn.nextInt(4)+1;
                roads.get(road).add(new Car(road));
            }
    }

    private void runCar(TreeSet <Car> ts) {
        try {
            Car car=ts.first();
            while(car.positionRoad<-1.0f) {
                ts.remove(car);
                car=ts.higher(car);
            }
            synchronized (car) {
                if (car!=null)
                    car.notify();
            }
        }
        catch (Exception ex) {
        }
    }
   
    private void runCar() {
        deadlock();
        if (carOnCrossRoad!=null)
            synchronized (carOnCrossRoad) {
                    carOnCrossRoad.notify();
                }
        runCar(roads.get(ROAD_DOWN));
        runCar(roads.get(ROAD_UP));
        runCar(roads.get(ROAD_LEFT));
        runCar(roads.get(ROAD_RIGHT));
    }

    /**
     *
     */
    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = CrossroadApp.getApplication().getMainFrame();
            aboutBox = new CrossroadAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        CrossroadApp.getApplication().show(aboutBox);
    }

    /**
     *
     */
    @Action
    public void showAddCar() {
        if (addCar == null) {
            JFrame mainFrame = CrossroadApp.getApplication().getMainFrame();
            addCar = new AddCar(mainFrame);
            addCar.setLocationRelativeTo(mainFrame);
        }
        CrossroadApp.getApplication().show(addCar);
    }

    /**
     *
     */
    @Action
    public void autoGenerate() {
        boolean state=autoGenerate.isSelected();
        if (state)
            t2.start();
        else
            t2.stop();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel()
        {
            public void paint(Graphics g)
            {
                super.paint(g);
                myPaint(g);
            }
        };
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        autoGenerate = new javax.swing.JCheckBoxMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setMinimumSize(new java.awt.Dimension(800, 800));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(800, 800));

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 776, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(crossroad.CrossroadApp.class).getContext().getResourceMap(CrossroadView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(crossroad.CrossroadApp.class).getContext().getActionMap(CrossroadView.class, this);
        jMenuItem1.setAction(actionMap.get("showAddCar")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        autoGenerate.setAction(actionMap.get("autoGenerate")); // NOI18N
        autoGenerate.setSelected(true);
        autoGenerate.setText(resourceMap.getString("autoGenerate.text")); // NOI18N
        autoGenerate.setName("autoGenerate"); // NOI18N
        autoGenerate.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                autoGenerateStateChanged(evt);
            }
        });
        fileMenu.add(autoGenerate);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 614, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void autoGenerateStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_autoGenerateStateChanged
        autoGenerate();
    }//GEN-LAST:event_autoGenerateStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBoxMenuItem autoGenerate;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox,addCar;
}
TOP

Related Classes of crossroad.CrossroadView

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.