JAVA   25
AssignmentGUI
Guest on 13th March 2023 01:21:24 PM


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6.  
  7. public class AssignmentGUI extends JFrame implements ActionListener,WindowListener
  8. {
  9.    JButton btn1;
  10.    JButton btn2;
  11.    JButton btn3;
  12.    JButton btn4;
  13.    JButton btnOK;
  14.    JLabel lblIntro;
  15.  
  16.    public static JLabel status;
  17.    static JLabel zeros;
  18.    static AssignmentGrid grid;
  19.    Vector data;
  20.    LoadFromFile lff;
  21.    FileDialog browse;
  22.    JPanel buttons;
  23.    public static int zCount = 0;
  24.    public static int stage = 1;
  25.  
  26.    public AssignmentGUI()
  27.    {
  28.       btnOK = new JButton("Okay, Let's Go!");
  29.          btnOK.addActionListener(this);
  30.  
  31.       this.setTitle("Visual Assignment Problem Solver");
  32.  
  33.       lblIntro = new JLabel("<html><b></i><font size = 5><center>"
  34.          +"<br>Welcome to the <br><i><font color = maroon>"
  35.          +"Visual Assignment Problem Solver</color></i>.</b>"
  36.          +"<br><font size = 4><center> <i>n</i> companies submit bids to perform <i>n</i> jobs."
  37.          +"<br><br>The goal of the Assignment Problem"
  38.          +"<br>is to assign one job to each company so as to"
  39.          +"<br>minimize the total cost of completing all of the jobs."
  40.          +"<br>The problem is represented here as a grid"
  41.          +"<br>with columns representing jobs to be assigned,"
  42.          +"<br>rows representing the different companies,"
  43.          +"<br>and the values in each cell represent the bids"
  44.          +"<br>by each company for each job.<br>"
  45.          +"<br>This application is a visual, interactive"
  46.          +"<br>implementation of the <i>Hungarian Method.</i><br>"
  47.          +"<br>Designed and implemented by <b>Brian Flink</b>"
  48.          +"<br> as a class project for <b>Moshe Rosenfeld's</b>"
  49.          +"<br>TCSS 343 Spring 2001 class at the"
  50.          +"<br><i><b>University of Washington, Tacoma.</b>"
  51.          +"<br><br></i></center></font></html>", javax.swing.SwingConstants.CENTER);
  52.  
  53.       grid = new AssignmentGrid();
  54.  
  55.       this.getContentPane().setLayout(new BorderLayout());
  56.       this.getContentPane().add(lblIntro, BorderLayout.CENTER);
  57.       this.getContentPane().add(btnOK, BorderLayout.SOUTH);
  58.  
  59.       //this.pack();
  60.       this.setSize(new Dimension(560, 580));
  61.       this.setVisible(true);
  62.       this.addWindowListener(this);
  63.  
  64.       status = new JLabel("    Find the file with your source data.");
  65.       zeros = new JLabel("    Zeros Found: " + String.valueOf(zCount));
  66.  
  67.       btn1 = new JButton("Browse");
  68.          btn1.addActionListener(this);
  69.       btn2 = new JButton("Load");
  70.          btn2.addActionListener(this);
  71.          btn2.setEnabled(false);
  72.       btn3 = new JButton("Start");
  73.          btn3.addActionListener(this);
  74.          btn3.setEnabled(false);
  75.       btn4 = new JButton("Next Aug.");
  76.          btn4.addActionListener(this);
  77.  
  78.       //button panel
  79.       buttons = new JPanel(new GridLayout(1,5));
  80.       buttons.add(btn1);
  81.       buttons.add(btn2);
  82.       buttons.add(btn3);
  83.       buttons.add(zeros);
  84.    }
  85.  
  86.    public void actionPerformed(ActionEvent e)
  87.    {
  88.       Object source = e.getSource();
  89.  
  90.       if(source == btnOK)
  91.       {
  92.          //Remove intro contents
  93.          this.getContentPane().remove(lblIntro);
  94.          this.getContentPane().remove(btnOK);
  95.  
  96.          this.getContentPane().add(status, BorderLayout.NORTH);
  97.          this.getContentPane().add(buttons, BorderLayout.SOUTH);
  98.          this.getContentPane().add(grid, BorderLayout.CENTER);
  99.  
  100.          this.pack();
  101.       }
  102.  
  103.       if(source == btn1)
  104.       {
  105.          if(btn1.getText().equals("Browse"))
  106.          {
  107.             browse = new FileDialog(this);
  108.             browse.show();
  109.             status.setText("    Load the data to the table.");
  110.          }
  111.          else
  112.          {
  113.             if(stage == 1)
  114.                grid.next();
  115.  
  116.             else if(stage == 2)
  117.             {
  118.                grid.next();
  119.                AssignmentGrid.done = false;
  120.             }
  121.  
  122.             else if(stage == 3)
  123.             {
  124.                if(AssignmentGrid.done)
  125.                   grid.next();
  126.                else
  127.                   grid.greedy();
  128.             }
  129.  
  130.             else if(stage == 4)
  131.                grid.next();
  132.  
  133.             else if(stage == 5)
  134.             {
  135.                this.setStatus();
  136.                grid.pathStep();
  137.                AssignmentGrid.done = false;
  138.             }
  139.  
  140.             else if(stage == 6)
  141.             {
  142.                this.setStatus();
  143.                if(AssignmentGrid.done == false)
  144.                   grid.markRowWithSelectedZero();
  145.             }
  146.  
  147.             else if(stage == 7)
  148.                grid.next();
  149.  
  150.             else if(stage == 8)
  151.                grid.next();
  152.  
  153.             else if(stage == 9)
  154.                grid.next();
  155.  
  156.             else if(stage == 10)
  157.                this.setStatus();
  158.  
  159.             this.repaint();
  160.          }
  161.       }
  162.  
  163.       if(source == btn2)
  164.       {
  165.          if(btn2.getText().equals("Load"))
  166.          {
  167.             lff = new LoadFromFile(browse.getDirectory(), browse.getFile());
  168.             grid.initVector(lff.GetData());
  169.             this.repaint();
  170.             status.setText("    Press start to begin solving the assignment problem.");
  171.          }
  172.          else
  173.          {
  174.             grid.next();
  175.  
  176.             if(stage == 10)
  177.                this.setStatus();
  178.          }
  179.       }
  180.  
  181.       if(source == btn3)
  182.       {
  183.          if(btn3.getText().equals("Start"))
  184.          {
  185.             status.setText("    Stage 1:   Find the minimum value in each row and subtract from all in row.");
  186.             btn1.setText("Next Step");
  187.             btn2.setText("Next Stage");
  188.             buttons.add(btn4, 2);
  189.             btn4.setVisible(true);
  190.          }
  191.          else
  192.          {
  193.             grid.solve();
  194.             this.setStatus();
  195.             this.repaint();
  196.          }
  197.       }
  198.  
  199.       if(source == btn4)
  200.       {
  201.          if(stage < 10)
  202.          {
  203.             while(stage < 10 && stage != 5)
  204.                grid.next();
  205.             do
  206.             {
  207.                findAug();
  208.             }while(stage < 10 && !grid.isAug);
  209.          }
  210.       }
  211.       this.setButtons(source);
  212.    }
  213.    //Handle button enabling. If done in the blocks above, it causes a delay
  214.    //if it attempts to disable a button immediately after it is activated.
  215.    private void setButtons(Object source)
  216.    {
  217.       if(source == btnOK)
  218.       {
  219.          btn1.setEnabled(true);
  220.          btn2.setEnabled(false);
  221.          btn3.setEnabled(false);
  222.          btn4.setEnabled(false);
  223.       }
  224.  
  225.       if(source == btn1)
  226.       {
  227.          if(btn1.getText().equals("Browse"))
  228.          {
  229.             btn1.setEnabled(true);
  230.             btn2.setEnabled(true);
  231.             btn3.setEnabled(false);
  232.             btn4.setEnabled(false);
  233.          }
  234.          else
  235.          {
  236.             btn1.setEnabled(true);
  237.             btn2.setEnabled(true);
  238.             btn3.setEnabled(true);
  239.             btn4.setEnabled(true);
  240.  
  241.             if(stage == 10)
  242.             {
  243.                buttons.remove(btn1);
  244.                btn1 = new JButton("Next Step");
  245.                btn1.addActionListener(this);
  246.                buttons.add(btn1, 0);
  247.                btn1.setEnabled(false);
  248.  
  249.                btn2.setEnabled(false);
  250.                btn3.setEnabled(false);
  251.                btn4.setEnabled(false);
  252.             }
  253.          }
  254.       }
  255.  
  256.       if(source == btn2)
  257.       {
  258.          if(btn2.getText().equals("Load"))
  259.          {
  260.             btn1.setEnabled(false);
  261.  
  262.             buttons.remove(btn2);
  263.             btn2 = new JButton("Load");
  264.             btn2.addActionListener(this);
  265.             buttons.add(btn2, 1);
  266.             btn2.setEnabled(false);
  267.  
  268.             btn3.setEnabled(true);
  269.             btn4.setEnabled(false);
  270.          }
  271.          else
  272.          {
  273.             if(stage == 10)
  274.             {
  275.                btn1.setEnabled(false);
  276.  
  277.                buttons.remove(btn2);
  278.                btn2 = new JButton("Next Stage");
  279.                btn2.addActionListener(this);
  280.                buttons.add(btn2, 1);
  281.                btn2.setEnabled(false);
  282.  
  283.                btn3.setEnabled(false);
  284.                btn4.setEnabled(false);            }
  285.             else
  286.             {
  287.                btn1.setEnabled(true);
  288.                btn2.setEnabled(true);
  289.                btn3.setEnabled(true);
  290.                btn4.setEnabled(true);
  291.             }
  292.          }
  293.       }
  294.  
  295.       if(source == btn3)
  296.       {
  297.          if(btn3.getText().equals("Start"))
  298.          {
  299.             btn3.setText("Solve");
  300.             btn1.setEnabled(true);
  301.             btn2.setEnabled(true);
  302.             btn3.setEnabled(true);
  303.             btn4.setEnabled(true);
  304.  
  305.          }
  306.          else
  307.          {
  308.             btn1.setEnabled(false);
  309.             btn2.setEnabled(false);
  310.  
  311.             buttons.remove(btn3);
  312.             btn3 = new JButton("Solve");
  313.             btn3.addActionListener(this);
  314.             buttons.add(btn3, 3);
  315.             btn3.setEnabled(false);
  316.  
  317.             btn4.setEnabled(false);
  318.          }
  319.       }
  320.  
  321.       if(source == btn4)
  322.       {
  323.          if(stage < 10)
  324.          {
  325.             btn1.setEnabled(true);
  326.             btn2.setEnabled(true);
  327.             btn3.setEnabled(true);
  328.             btn4.setEnabled(true);
  329.          }
  330.          else
  331.          {
  332.             btn1.setEnabled(false);
  333.             btn2.setEnabled(false);
  334.             btn3.setEnabled(false);
  335.  
  336.             buttons.remove(btn4);
  337.             btn4 = new JButton("Next Aug.");
  338.             btn4.addActionListener(this);
  339.             buttons.add(btn4, 2);
  340.             btn4.setEnabled(false);
  341.          }
  342.       }
  343.    }
  344.  
  345.    //Finds the next augmenting path
  346.    private void findAug()
  347.    {
  348.       if(stage == 5)
  349.       {
  350.          do
  351.          {
  352.             grid.pathStep();
  353.          }while(!grid.isAug && stage == 5);
  354.       }
  355.       else
  356.          while(stage < 10 && stage != 5)
  357.             grid.next();
  358.    }
  359.  
  360.    public static void setStatus()
  361.    {
  362.       if(stage == 1)
  363.          status.setText("    Stage 1:   Find the minimum value in each row and subtract from all in row.");
  364.       else if(stage == 2)
  365.          status.setText("    Stage 2:   Find the minimum value in each column and subtract from all in column.");
  366.       else if(stage == 3)
  367.          status.setText("    Stage 3:   Mark zeros found on veritces.");
  368.       else if(stage == 4)
  369.          status.setText("    Stage 4:   Mark unselected zeros.");
  370.       else if(stage == 5)
  371.          status.setText("    Stage 5:   Find augmenting paths. Path must terminate on an unselected zero. ");
  372.       else if(stage == 6)
  373.          status.setText("    Stage 6:   Lock out rows containing selected zeros.");
  374.       else if(stage == 7)
  375.          status.setText("    Stage 7:   Find minimum value that is not locked out.");
  376.       else if(stage == 8)
  377.          status.setText("    Stage 8:   Add minimum value to dark gray cells. Subtract from white cells.");
  378.       else if(stage == 9)
  379.          status.setText("    Stage 9:   Go back to stage 3. Repeat process until all zeros are found.");
  380.       else if(stage == 10)
  381.       {
  382.          status.setText("    Assignment problem has been solved!   Minumum cost = " + grid.getSum());      }
  383.       else
  384.          stage = 10;
  385.    }
  386.  
  387.    public static void setCount()
  388.    {
  389.       zeros.setText("    Zeros Found: " + String.valueOf(zCount));
  390.    }
  391.  
  392.    //Exits when close icon is clicked
  393.    public void windowClosing (WindowEvent e)
  394.    {
  395.       System.exit(0);
  396.    }
  397.  
  398.    //Overwrite WindowListener methods
  399.    public void windowActivated(WindowEvent e){}
  400.    public void windowClosed(WindowEvent e){}
  401.    public void windowDeactivated(WindowEvent e){}
  402.    public void windowDeiconified(WindowEvent e){}
  403.    public void windowIconified(WindowEvent e){}
  404.    public void windowOpened(WindowEvent e){}
  405.  
  406.    //Run main.
  407.    public static void main(String[] args)
  408.    {
  409.       new AssignmentGUI();
  410.    }
  411. }

Raw Paste

Login or Register to edit or fork this paste. It's free.