Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package edmondskarp.Gui; import edmondskarp.Controller.EdmondsKarpController; import edmondskarp.Model.Graph; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.HeadlessException; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.Point2D; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Observable; import java.util.Observer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.InputMap; import javax.swing.JColorChooser; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.KeyStroke; import org.json.JSONException; /** * * @author gabriele */ public class EdmondsKarpGui extends javax.swing.JFrame implements Observer { private ArrayList<Circle> circles; private int MODE = 0; private boolean isSecond; private boolean isInDragging; private Circle shapeTmp; private final int DRAW = 0; private final int DRAG = 1; private final int ERASE = 3; private EdmondsKarpController controller; private static final EdmondsKarpGui gui = new EdmondsKarpGui(); private Point2D pointTmp; private final JFileChooser chooser; private EdmondsKarpGui() { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(EdmondsKarpGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EdmondsKarpGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EdmondsKarpGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EdmondsKarpGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } initComponents(); this.addWindowListener(new WListener()); setupUndoHotkeys(); circles = new ArrayList<>(); chooser = new JFileChooser(); isSecond = false; isInDragging = false; pointTmp = new Point2D.Double(); } public static EdmondsKarpGui getGui() { return gui; } public void setController(EdmondsKarpController controller) { this.controller = controller; controller.searchDefaultPreference(); updatePrefMenu(); } /** * 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() { jPopupMenu1 = new javax.swing.JPopupMenu(); jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JPopupMenu.Separator(); jMenuItem6 = new javax.swing.JMenuItem(); jPopupMenu2 = new javax.swing.JPopupMenu(); jMenuItem7 = new javax.swing.JMenuItem(); jMenuItem8 = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JPopupMenu.Separator(); jMenuItem9 = new javax.swing.JMenuItem(); jDialog1 = new javax.swing.JDialog(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jButton2 = new javax.swing.JButton(); jDialog2 = new javax.swing.JDialog(); jTabbedPane2 = new javax.swing.JTabbedPane(); jPanel3 = new javax.swing.JPanel(); jTextField1 = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jSliderPosText = new javax.swing.JSlider(); jButtonSetDefCapacity = new javax.swing.JButton(); jRadioButtonRandomCap = new javax.swing.JRadioButton(); jRadioButtonDefaultCap = new javax.swing.JRadioButton(); jPanel4 = new javax.swing.JPanel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jButtonDefaultColor = new javax.swing.JButton(); jButtonAttraversatoColor = new javax.swing.JButton(); jButtonSaturoColor = new javax.swing.JButton(); jLabel11 = new javax.swing.JLabel(); jButtonSelezionatoColor = new javax.swing.JButton(); jPanel5 = new javax.swing.JPanel(); jLabel8 = new javax.swing.JLabel(); jComboBoxDimCircle = new javax.swing.JComboBox<>(); jLabel9 = new javax.swing.JLabel(); jComboBoxDimText = new javax.swing.JComboBox<>(); StrokeCirclejSlider = new javax.swing.JSlider(); StrokeArrowjSlider = new javax.swing.JSlider(); jLabel10 = new javax.swing.JLabel(); jLabel12 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); pencilButton = new javax.swing.JToggleButton(); rubberButton = new javax.swing.JToggleButton(); playButton = new javax.swing.JToggleButton(); backButton = new javax.swing.JButton(); forwardButton = new javax.swing.JButton(); stopButton = new javax.swing.JButton(); runButton = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox<>(); dragButton = new javax.swing.JToggleButton(); undoButton = new javax.swing.JButton(); redoButton = new javax.swing.JButton(); residualButton = new javax.swing.JToggleButton(); jLabelMin = new javax.swing.JLabel(); jLabelMaxFlow = new javax.swing.JLabel(); myPanel = new MyPanel(this); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem5 = new javax.swing.JMenuItem(); jMenuItem1 = new javax.swing.JMenuItem(); jMenuItem4 = new javax.swing.JMenuItem(); jMenuItemExit = new javax.swing.JMenuItem(); jMenu2 = new javax.swing.JMenu(); jMenuItemExample = new javax.swing.JMenuItem(); jMenuItem10 = new javax.swing.JMenuItem(); jMenu3 = new javax.swing.JMenu(); jMenuItemAbout = new javax.swing.JMenuItem(); jPopupMenu1.setLightWeightPopupEnabled(false); jPopupMenu1.setMaximumSize(new java.awt.Dimension(97, 54)); jPopupMenu1.setMinimumSize(new java.awt.Dimension(97, 54)); jMenuItem2.setText("Imposta sorgente"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { setSourceActionPerformed(evt); } }); jPopupMenu1.add(jMenuItem2); jMenuItem3.setText("Imposta pozzo"); jMenuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { setSinkActionPerformed(evt); } }); jPopupMenu1.add(jMenuItem3); jPopupMenu1.add(jSeparator1); jMenuItem6.setText("Elimina nodo"); jMenuItem6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteNodeActionPerformed(evt); } }); jPopupMenu1.add(jMenuItem6); jPopupMenu1.getAccessibleContext().setAccessibleDescription(""); jMenuItem7.setText("Imposta capacit"); jMenuItem7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { setCapacityActionPerformed(evt); } }); jPopupMenu2.add(jMenuItem7); jMenuItem8.setText("Imposta flusso"); jMenuItem8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { setFlowActionPerformed(evt); } }); jPopupMenu2.add(jMenuItem8); jPopupMenu2.add(jSeparator2); jMenuItem9.setText("Elimina arco"); jMenuItem9.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { deleteEdgeActionPerformed(evt); } }); jPopupMenu2.add(jMenuItem9); jDialog1.setTitle("Inserisci il valore"); jLabel1.setText("imposta capacit"); jTextField2.setNextFocusableComponent(jButton2); jButton2.setText("Ok"); jButton2.setFocusable(false); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(31, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton2).addContainerGap(24, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(25, 25, 25))))); javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane()); jDialog1.getContentPane().setLayout(jDialog1Layout); jDialog1Layout.setHorizontalGroup( jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jDialog1Layout.setVerticalGroup( jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); jDialog2.setMinimumSize(new java.awt.Dimension(341, 266)); jTabbedPane2.setMinimumSize(new java.awt.Dimension(349, 304)); jPanel3.setMinimumSize(new java.awt.Dimension(341, 266)); jTextField1.setEnabled(false); jLabel2.setText("Capacit default"); jLabel7.setText("Posizione testo"); jSliderPosText.setMaximum(150); jSliderPosText.setMinimum(30); jSliderPosText.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { jSliderPosTextStateChanged(evt); } }); jButtonSetDefCapacity.setText("Set"); jButtonSetDefCapacity.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonSetDefCapacityActionPerformed(evt); } }); jRadioButtonRandomCap.setSelected(true); jRadioButtonRandomCap.setText("Capcit random"); jRadioButtonRandomCap.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButtonRandomCapActionPerformed(evt); } }); jRadioButtonDefaultCap.setText("Capacit default"); jRadioButtonDefaultCap.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButtonDefaultCapActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jButtonSetDefCapacity, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabel7) .addComponent(jSliderPosText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jRadioButtonRandomCap).addComponent(jRadioButtonDefaultCap)) .addContainerGap(111, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(24, 24, 24) .addComponent(jRadioButtonRandomCap).addGap(18, 18, 18).addComponent(jRadioButtonDefaultCap) .addGap(18, 18, 18) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField1).addComponent(jButtonSetDefCapacity)) .addGap(18, 18, 18).addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSliderPosText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(68, Short.MAX_VALUE))); jTabbedPane2.addTab("Generale", jPanel3); jPanel4.setMinimumSize(new java.awt.Dimension(341, 266)); jLabel3.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N jLabel3.setText("Arco"); jLabel4.setText("default"); jLabel5.setText("attraversato"); jLabel6.setText("saturo"); jButtonDefaultColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { defaultColorActionPerformed(evt); } }); jButtonAttraversatoColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { attraversatoColorActionPerformed(evt); } }); jButtonSaturoColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saturoColorActionPerformed(evt); } }); jLabel11.setText("selezionato"); jButtonSelezionatoColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { selezionatoColorActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel4Layout .createSequentialGroup().addContainerGap().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jLabel3) .addGroup(jPanel4Layout.createSequentialGroup().addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.LEADING)) .addGap(23, 23, 23) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButtonSaturoColor, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonAttraversatoColor, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonSelezionatoColor, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonDefaultColor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(221, Short.MAX_VALUE))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButtonDefaultColor, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonAttraversatoColor, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButtonSaturoColor, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButtonSelezionatoColor, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE) .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(128, 128, 128))); jTabbedPane2.addTab("Colori", jPanel4); jPanel5.setMinimumSize(new java.awt.Dimension(341, 266)); jLabel8.setText("Circonferenza nodi"); jComboBoxDimCircle.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "25", "30", "35", "40", "45", "50", "55", "60", "65" })); jComboBoxDimCircle.setToolTipText(""); jComboBoxDimCircle.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBoxDimCircleActionPerformed(evt); } }); jLabel9.setText("Testo"); jComboBoxDimText.setModel(new javax.swing.DefaultComboBoxModel<>( new String[] { "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30" })); jComboBoxDimText.setSelectedIndex(3); jComboBoxDimText.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { DimTextActionPerformed(evt); } }); StrokeCirclejSlider.setMaximum(60); StrokeCirclejSlider.setMinimum(1); StrokeCirclejSlider.setPaintLabels(true); StrokeCirclejSlider.setPaintTicks(true); StrokeCirclejSlider.setValue(10); StrokeCirclejSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { StrokeCirclejSliderStateChanged(evt); } }); StrokeArrowjSlider.setMaximum(60); StrokeArrowjSlider.setMinimum(1); StrokeArrowjSlider.setPaintLabels(true); StrokeArrowjSlider.setPaintTicks(true); StrokeArrowjSlider.setValue(10); StrokeArrowjSlider.addChangeListener(new javax.swing.event.ChangeListener() { public void stateChanged(javax.swing.event.ChangeEvent evt) { StrokeArrowjSliderStateChanged(evt); } }); jLabel10.setText("Linea Nodi"); jLabel12.setText("Linea Archi"); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap().addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE) .addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jComboBoxDimText, javax.swing.GroupLayout.Alignment.LEADING, 0, 64, Short.MAX_VALUE) .addComponent(jComboBoxDimCircle, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(154, 154, 154)) .addGroup(jPanel5Layout.createSequentialGroup().addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout .createSequentialGroup().addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(StrokeCirclejSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup().addComponent(jLabel12) .addGap(18, 18, 18).addComponent(StrokeArrowjSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBoxDimCircle)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jComboBoxDimText)) .addGap(30, 30, 30) .addGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(StrokeCirclejSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(27, 27, 27) .addGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(StrokeArrowjSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(27, 27, 27))); jTabbedPane2.addTab("Dimensioni", jPanel5); javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane()); jDialog2.getContentPane().setLayout(jDialog2Layout); jDialog2Layout .setHorizontalGroup(jDialog2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTabbedPane2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jDialog2Layout.setVerticalGroup(jDialog2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)); setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE); setBackground(new java.awt.Color(255, 255, 255)); setPreferredSize(new java.awt.Dimension(1100, 650)); jPanel1.setBackground(new java.awt.Color(169, 169, 169)); jPanel1.setPreferredSize(new java.awt.Dimension(824, 50)); pencilButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/draw.png"))); // NOI18N pencilButton.setSelected(true); pencilButton.setToolTipText("draw"); pencilButton.setFocusable(false); pencilButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); pencilButton.setMaximumSize(new java.awt.Dimension(30, 30)); pencilButton.setMinimumSize(new java.awt.Dimension(24, 24)); pencilButton.setName(""); // NOI18N pencilButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { pencilButtonActionPerformed(evt); } }); rubberButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/remove.png"))); // NOI18N rubberButton.setToolTipText("erase"); rubberButton.setFocusable(false); rubberButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); rubberButton.setMaximumSize(new java.awt.Dimension(30, 30)); rubberButton.setMinimumSize(new java.awt.Dimension(24, 24)); rubberButton.setName(""); // NOI18N rubberButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { rubberButtonActionPerformed(evt); } }); playButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/play.png"))); // NOI18N playButton.setToolTipText("play"); playButton.setFocusable(false); playButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); playButton.setMaximumSize(new java.awt.Dimension(30, 30)); playButton.setMinimumSize(new java.awt.Dimension(24, 24)); playButton.setName(""); // NOI18N playButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { playButtonActionPerformed(evt); } }); backButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/back 1.png"))); // NOI18N backButton.setFocusable(false); backButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { backButtonActionPerformed(evt); } }); forwardButton .setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/forward 1.png"))); // NOI18N forwardButton.setFocusable(false); forwardButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { forwardButtonActionPerformed(evt); } }); stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/square.png"))); // NOI18N stopButton.setToolTipText("stop"); stopButton.setFocusable(false); stopButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { stopButtonActionPerformed(evt); } }); runButton.setText("Run"); runButton.setToolTipText("stop"); runButton.setFocusable(false); runButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { runButtonActionPerformed(evt); } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "BFS", "DFS" })); jComboBox1.setFocusable(false); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); dragButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/drag.png"))); // NOI18N dragButton.setToolTipText("erase"); dragButton.setFocusable(false); dragButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); dragButton.setMaximumSize(new java.awt.Dimension(30, 30)); dragButton.setMinimumSize(new java.awt.Dimension(24, 24)); dragButton.setName(""); // NOI18N dragButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dragButtonActionPerformed(evt); } }); undoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/undo.png"))); // NOI18N undoButton.setToolTipText("undo"); undoButton.setFocusable(false); undoButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { undoButtonActionPerformed(evt); } }); redoButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edmondskarp/Gui/icon/redo.png"))); // NOI18N redoButton.setToolTipText("redo"); redoButton.setFocusable(false); redoButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { redoButtonActionPerformed(evt); } }); residualButton.setText("Residuo"); residualButton.setToolTipText("grafo residuo"); residualButton.setFocusable(false); residualButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); residualButton.setMaximumSize(new java.awt.Dimension(30, 30)); residualButton.setMinimumSize(new java.awt.Dimension(24, 24)); residualButton.setName(""); // NOI18N residualButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { residualButtonActionPerformed(evt); } }); jLabelMin.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N jLabelMin.setText("Min = 0"); jLabelMaxFlow.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N jLabelMaxFlow.setText("Flow = 0"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(pencilButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(rubberButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(dragButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(undoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(redoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(24, 24, 24) .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(5, 5, 5) .addComponent(playButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(5, 5, 5) .addComponent(forwardButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(runButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(26, 26, 26) .addComponent(residualButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(40, 40, 40).addComponent(jLabelMin).addGap(35, 35, 35).addComponent(jLabelMaxFlow) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(stopButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(forwardButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(backButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(playButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(rubberButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pencilButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(runButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(dragButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(undoButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(redoButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(residualButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jComboBox1, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelMaxFlow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabelMin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(670, 670, 670))); runButton.getAccessibleContext().setAccessibleDescription("run"); getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START); myPanel.setBackground(new java.awt.Color(255, 255, 255)); myPanel.setMaximumSize(new java.awt.Dimension(1920, 1080)); myPanel.setPreferredSize(new java.awt.Dimension(800, 600)); myPanel.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseReleased(java.awt.event.MouseEvent evt) { myPanelMouseReleased(evt); } public void mouseClicked(java.awt.event.MouseEvent evt) { myPanelMouseClicked(evt); } }); myPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(java.awt.event.MouseEvent evt) { myPanelMouseDragged(evt); } }); javax.swing.GroupLayout myPanelLayout = new javax.swing.GroupLayout(myPanel); myPanel.setLayout(myPanelLayout); myPanelLayout.setHorizontalGroup(myPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 973, Short.MAX_VALUE)); myPanelLayout.setVerticalGroup(myPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 668, Short.MAX_VALUE)); getContentPane().add(myPanel, java.awt.BorderLayout.CENTER); jMenu1.setText("File"); jMenu1.setPreferredSize(new java.awt.Dimension(50, 21)); jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem5.setText("Nuovo"); jMenuItem5.setPreferredSize(new java.awt.Dimension(110, 25)); jMenuItem5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { newActionPerformed(evt); } }); jMenu1.add(jMenuItem5); jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem1.setText("Apri"); jMenuItem1.setPreferredSize(new java.awt.Dimension(110, 25)); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { OpenActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK)); jMenuItem4.setText("Salva"); jMenuItem4.setPreferredSize(new java.awt.Dimension(110, 25)); jMenuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SaveActionPerformed(evt); } }); jMenu1.add(jMenuItem4); jMenuItemExit.setText("Exit"); jMenuItemExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemExitActionPerformed(evt); } }); jMenu1.add(jMenuItemExit); jMenuBar1.add(jMenu1); jMenu2.setText("Modifica"); jMenu2.setMaximumSize(new java.awt.Dimension(70, 32767)); jMenu2.setPreferredSize(new java.awt.Dimension(90, 21)); jMenuItemExample.setText("Carica esempio"); jMenuItemExample.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemExampleActionPerformed(evt); } }); jMenu2.add(jMenuItemExample); jMenuItem10.setText("Preference"); jMenuItem10.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { preferenceActionPerformed(evt); } }); jMenu2.add(jMenuItem10); jMenuBar1.add(jMenu2); jMenu3.setText("help"); jMenu3.setPreferredSize(new java.awt.Dimension(40, 21)); jMenuItemAbout.setText("About"); jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemAboutActionPerformed(evt); } }); jMenu3.add(jMenuItemAbout); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); pack(); }// </editor-fold>//GEN-END:initComponents private void myPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myPanelMouseClicked if (evt.getButton() == MouseEvent.BUTTON1) { if (MODE == ERASE) { eraseShape(evt.getPoint()); } else if (MODE == DRAW) { if (isSecond) { Circle circ = getSelectedCircle(evt.getPoint()); if (circ != null && circ != shapeTmp) { addArrow(circ); shapeTmp.setSelect(false); isSecond = false; update(); return; } } else { Circle circ = getSelectedCircle(evt.getPoint()); if (circ != null) { shapeTmp = circ; shapeTmp.setSelect(true); isSecond = true; update(); return; } } if (getSelectedCircle(evt.getPoint()) == null && !isSecond) { addCircle(evt.getPoint()); } else { isSecond = false; shapeTmp.setSelect(false); update(); } } } else if (evt.getButton() == MouseEvent.BUTTON3) { if (getSelectedCircle(evt.getPoint()) != null) { pointTmp.setLocation(evt.getPoint().getX(), evt.getPoint().getY()); jPopupMenu1.show(myPanel, evt.getX(), evt.getY()); } else if (getSelectedArrow(evt.getPoint()) != null) { pointTmp.setLocation(evt.getPoint().getX(), evt.getPoint().getY()); jPopupMenu2.show(myPanel, evt.getX(), evt.getY()); } } }//GEN-LAST:event_myPanelMouseClicked private void myPanelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myPanelMouseDragged if (MODE == DRAG) { if (!isInDragging) { if (shapeTmp != null) { shapeTmp.setSelect(false); isSecond = false; } shapeTmp = getSelectedCircle(evt.getPoint()); if (shapeTmp != null) { shapeTmp.setFirstPoint(evt.getPoint()); shapeTmp.needUpdate(); isInDragging = true; } else { // addCircle(evt.getPoint()); isInDragging = false; shapeTmp = null; } } else { shapeTmp.setFirstPoint(evt.getPoint()); shapeTmp.needUpdate(); shapeTmp.updateArrow(); update(); } } }//GEN-LAST:event_myPanelMouseDragged private void myPanelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_myPanelMouseReleased if (isInDragging) { isInDragging = false; controller.saveState(); } }//GEN-LAST:event_myPanelMouseReleased private void pencilButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pencilButtonActionPerformed // TODO add your handling code here: rubberButton.setSelected(false); dragButton.setSelected(false); MODE = DRAW; }//GEN-LAST:event_pencilButtonActionPerformed private void rubberButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rubberButtonActionPerformed // TODO add your handling code here: pencilButton.setSelected(false); dragButton.setSelected(false); MODE = ERASE; }//GEN-LAST:event_rubberButtonActionPerformed private void playButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playButtonActionPerformed // TODO add your handling code here: controller.play(); }//GEN-LAST:event_playButtonActionPerformed private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed // TODO add your handling code here: playButton.setSelected(false); controller.stop(); jLabelMin.setText("Min = 0"); jLabelMaxFlow.setText("Flow = 0"); }//GEN-LAST:event_stopButtonActionPerformed private void setSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setSourceActionPerformed // TODO add your handling code here: controller.setSource(getSelectedCircle(pointTmp)); }//GEN-LAST:event_setSourceActionPerformed private void setSinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setSinkActionPerformed // TODO add your handling code here: controller.setSink(getSelectedCircle(pointTmp)); }//GEN-LAST:event_setSinkActionPerformed private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_runButtonActionPerformed // TODO add your handling code here: controller.run(); }//GEN-LAST:event_runButtonActionPerformed private void forwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forwardButtonActionPerformed // TODO add your handling code here: controller.oneStepForward(); }//GEN-LAST:event_forwardButtonActionPerformed private void OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenActionPerformed try { controller.open(openGraph()); } catch (JSONException ex) { Logger.getLogger(EdmondsKarpGui.class.getName()).log(Level.SEVERE, null, ex); } update(); }//GEN-LAST:event_OpenActionPerformed private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed saveGraph(); }//GEN-LAST:event_SaveActionPerformed private void newActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newActionPerformed // TODO add your handling code here: controller.newGraph(); controller.saveState(); }//GEN-LAST:event_newActionPerformed private void deleteNodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteNodeActionPerformed // TODO add your handling code here: eraseCircle(getSelectedCircle(pointTmp)); }//GEN-LAST:event_deleteNodeActionPerformed private void setCapacityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setCapacityActionPerformed // TODO add your handling code here: jLabel1.setText("Set capacity"); jTextField2.setText(""); jDialog1.setVisible(true); jDialog1.setLocation((int) pointTmp.getX(), (int) pointTmp.getY()); jDialog1.pack(); }//GEN-LAST:event_setCapacityActionPerformed private void setFlowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setFlowActionPerformed // TODO add your handling code here: jLabel1.setText("Set flow"); jTextField2.setText(""); jDialog1.setVisible(true); jDialog1.setLocation((int) pointTmp.getX(), (int) pointTmp.getY()); jDialog1.pack(); }//GEN-LAST:event_setFlowActionPerformed private void deleteEdgeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteEdgeActionPerformed // TODO add your handling code here: eraseArrow(getSelectedArrow(pointTmp)); }//GEN-LAST:event_deleteEdgeActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed // TODO add your handling code here: if (!jTextField2.getText().equals("")) { if (jLabel1.getText().equals("Set capacity")) { Arrow arrow = getSelectedArrow(pointTmp); int edge = arrow.getSelectedEdge(pointTmp); int capacity = 0; try { capacity = Integer.parseInt(jTextField2.getText()); } catch (NumberFormatException numberFormatException) { displayMessage("Errore: inserire un valore numerico"); return; } controller.setCapacity(arrow, capacity, edge); } else { Arrow arrow = getSelectedArrow(pointTmp); int edge = arrow.getSelectedEdge(pointTmp); int flow = 0; try { flow = Integer.parseInt(jTextField2.getText()); } catch (NumberFormatException numberFormatException) { displayMessage("Errore: inserire un valore numerico"); return; } controller.setFlow(arrow, flow, edge); } update(); } jDialog1.setVisible(false); }//GEN-LAST:event_jButton2ActionPerformed private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed // TODO add your handling code here: controller.onStepBack(); }//GEN-LAST:event_backButtonActionPerformed private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed // TODO add your handling code here: controller.setVisit(jComboBox1.getSelectedIndex()); }//GEN-LAST:event_jComboBox1ActionPerformed private void preferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preferenceActionPerformed // TODO add your handling code here: jButtonAttraversatoColor.setBackground(Config.getConfig().getUsedArrow()); jButtonDefaultColor.setBackground(Config.getConfig().getDefaultArrow()); jButtonSaturoColor.setBackground(Config.getConfig().getFilledArrow()); jButtonSelezionatoColor.setBackground(Config.getConfig().getSelectedArrow()); jDialog2.setVisible(true); }//GEN-LAST:event_preferenceActionPerformed private void DimTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DimTextActionPerformed // TODO add your handling code here: Config.getConfig() .setDimText(Integer.parseInt(jComboBoxDimText.getItemAt(jComboBoxDimText.getSelectedIndex()))); controller.saveConfig(); update(); }//GEN-LAST:event_DimTextActionPerformed private void defaultColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultColorActionPerformed // TODO add your handling code here: Color c = JColorChooser.showDialog(this, "Selezione Colore", Color.WHITE); jButtonDefaultColor.setBackground(c); Config.getConfig().setDefaultArrow(c); controller.saveConfig(); update(); }//GEN-LAST:event_defaultColorActionPerformed private void attraversatoColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_attraversatoColorActionPerformed // TODO add your handling code here: Color c = JColorChooser.showDialog(this, "Selezione Colore", Color.WHITE); jButtonAttraversatoColor.setBackground(c); Config.getConfig().setUsedArrow(c); controller.saveConfig(); update(); }//GEN-LAST:event_attraversatoColorActionPerformed private void saturoColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saturoColorActionPerformed // TODO add your handling code here: Color c = JColorChooser.showDialog(this, "Selezione Colore", Color.WHITE); jButtonSaturoColor.setBackground(c); Config.getConfig().setFilledArrow(c); controller.saveConfig(); update(); }//GEN-LAST:event_saturoColorActionPerformed private void selezionatoColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selezionatoColorActionPerformed // TODO add your handling code here: Color c = JColorChooser.showDialog(this, "Selezione Colore", Color.WHITE); jButtonSelezionatoColor.setBackground(c); Config.getConfig().setSelectedArrow(c); controller.saveConfig(); update(); }//GEN-LAST:event_selezionatoColorActionPerformed private void jComboBoxDimCircleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxDimCircleActionPerformed // TODO add your handling code here: Config.getConfig().setDimCircle( Integer.parseInt(jComboBoxDimCircle.getItemAt(jComboBoxDimCircle.getSelectedIndex()))); controller.saveConfig(); update(); }//GEN-LAST:event_jComboBoxDimCircleActionPerformed private void jSliderPosTextStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderPosTextStateChanged // TODO add your handling code here: Config.getConfig().setPosText(jSliderPosText.getValue()); controller.saveConfig(); update(); }//GEN-LAST:event_jSliderPosTextStateChanged private void jButtonSetDefCapacityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSetDefCapacityActionPerformed // TODO add your handling code here: if (!jTextField1.getText().equals("")) { int capacity = 0; try { capacity = Integer.parseInt(jTextField1.getText()); } catch (NumberFormatException numberFormatException) { displayMessage("Errore: inserire un valore numerico"); return; } Config.getConfig().setFixedCapacity(capacity); controller.saveConfig(); } }//GEN-LAST:event_jButtonSetDefCapacityActionPerformed private void jRadioButtonRandomCapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonRandomCapActionPerformed // TODO add your handling code here: jRadioButtonDefaultCap.setSelected(false); jTextField1.setEnabled(false); Config.getConfig().setRandomCapacity(true); controller.saveConfig(); }//GEN-LAST:event_jRadioButtonRandomCapActionPerformed private void jRadioButtonDefaultCapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonDefaultCapActionPerformed // TODO add your handling code here: jRadioButtonRandomCap.setSelected(false); jTextField1.setEnabled(true); Config.getConfig().setRandomCapacity(false); controller.saveConfig(); }//GEN-LAST:event_jRadioButtonDefaultCapActionPerformed private void jMenuItemExampleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExampleActionPerformed // TODO add your handling code here: controller.loadExample(); }//GEN-LAST:event_jMenuItemExampleActionPerformed private void dragButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dragButtonActionPerformed // TODO add your handling code here: MODE = DRAG; rubberButton.setSelected(false); pencilButton.setSelected(false); }//GEN-LAST:event_dragButtonActionPerformed private void undoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_undoButtonActionPerformed // TODO add your handling code here: controller.restoreState(true); }//GEN-LAST:event_undoButtonActionPerformed private void redoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redoButtonActionPerformed // TODO add your handling code here: controller.restoreState(false); }//GEN-LAST:event_redoButtonActionPerformed private void StrokeCirclejSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_StrokeCirclejSliderStateChanged // TODO add your handling code here: Config.getConfig().setStrokeCircle(StrokeCirclejSlider.getValue()); controller.saveConfig(); update(); }//GEN-LAST:event_StrokeCirclejSliderStateChanged private void StrokeArrowjSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_StrokeArrowjSliderStateChanged // TODO add your handling code here: Config.getConfig().setStrokeArrow(StrokeArrowjSlider.getValue()); controller.saveConfig(); update(); }//GEN-LAST:event_StrokeArrowjSliderStateChanged private void residualButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_residualButtonActionPerformed // TODO add your handling code here: Config.getConfig().setResitualMode(!Config.getConfig().getResidualMode()); controller.saveConfig(); update(); }//GEN-LAST:event_residualButtonActionPerformed private void jMenuItemAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAboutActionPerformed // TODO add your handling code here: displayMessage("Created by Gabriele Gelardi\nemail: gabriele.gelardi@gmail.com"); }//GEN-LAST:event_jMenuItemAboutActionPerformed private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExitActionPerformed // TODO add your handling code here: controller.exit(); }//GEN-LAST:event_jMenuItemExitActionPerformed public boolean isPlaySelected() { return playButton.isSelected(); } public void setUpPlayButton() { playButton.setSelected(false); } public void eraseCircle(Circle circle) { circles.remove(circle); controller.removeNode(circle); circle.removeArrows(); controller.saveState(); update(); } public void eraseArrow(Arrow arrow) { arrow.getFrom().removeArrowFrom(arrow); arrow.getTo().removeArrowTo(arrow); controller.removeEdge(arrow); controller.saveState(); update(); } private void eraseShape(Point2D point) { for (Circle circle : circles) { if (circle.getShape().contains(point)) { eraseCircle(circle); break; } else { Arrow arrow = circle.checkForArrow(point); if (arrow != null) { eraseArrow(arrow); break; } } } update(); } private Circle getSelectedCircle(Point2D point) { for (Circle circle : circles) { if (circle.getShape().contains(point)) { return circle; } } return null; } private Arrow getSelectedArrow(Point2D point) { for (Circle circle : circles) { Arrow arrow = circle.checkForArrow(point); if (arrow != null) { return arrow; } } return null; } public void update(Graphics g) { Graphics2D g2 = (Graphics2D) g; for (Circle circle : circles) { circle.draw(g2); circle.drawText(g2); circle.updateArrow(); circle.drawArrows(g2); } Config.getConfig().setNeedUpdate(false); } public void update() { myPanel.repaint(); } public void addCircle(Point point) { Circle circle = new Circle(); circle.setFirstPoint(point); circles.add(circle); controller.addNode(circle); update(); } private void addArrow(Circle circ) { if (controller.checkAddInverseArrow(shapeTmp.getName(), circ.getName())) { controller.saveState(); return; } Arrow arrow = new Arrow(shapeTmp, circ); if (controller.addEdge(arrow)) { shapeTmp.addArrowFrom(arrow); circ.addArrowTo(arrow); controller.saveState(); update(); } } public void displayMessage(String str) { JOptionPane.showMessageDialog(this, str); } public int checkSave() { int response = JOptionPane.showConfirmDialog(null, "Sono state fatte modifiche,\nvuoi salvare", "Attenzione", JOptionPane.YES_NO_OPTION); if (response == 0) { saveGraph(); } return response; } public ArrayList getCircles() { return circles; } public void setCircles(ArrayList array) { circles = array; } private File openGraph() { //Visualizzo la finestra di dialogo int risposta = chooser.showOpenDialog(this); if (risposta == JFileChooser.APPROVE_OPTION) {//Se ho premuto il tasto apri return chooser.getSelectedFile(); } return null; } public void saveGraph() { int option2 = JOptionPane.NO_OPTION; File f = null; String str = null; while (option2 == JOptionPane.NO_OPTION) //Finche' non decido di salvare { //Visualizzo la finestra di dialogo int option = chooser.showSaveDialog(this); if (option == JFileChooser.APPROVE_OPTION) //Se ho premuto il tasto salva { try { //Recupero il file selezionato f = chooser.getSelectedFile(); //Recupero il path del file str = f.getCanonicalPath(); //Se il nome del file non contiene l'estensione, la aggiungo io a mano //Se il file esiste chiedo se lo voglio sovrascrivere if (f.exists()) { option2 = JOptionPane.showConfirmDialog(this, "Il file esiste gia'.\nLo vuoi sovrascrivere?", "Sovrascrittura", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); } else { option2 = JOptionPane.YES_OPTION; } } catch (IOException | HeadlessException ex) { } } else { option2 = JOptionPane.CANCEL_OPTION; } } if (option2 == JOptionPane.YES_OPTION) { try { controller.save(str); } catch (JSONException ex) { Logger.getLogger(EdmondsKarpGui.class.getName()).log(Level.SEVERE, null, ex); } } } private void setupUndoHotkeys() { String UNDO = "Undo action key"; String REDO = "Redo action key"; Action undoAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { controller.restoreState(true); } }; Action redoAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { controller.restoreState(false); } }; myPanel.getActionMap().put(UNDO, undoAction); myPanel.getActionMap().put(REDO, redoAction); InputMap[] inputMaps = new InputMap[] { myPanel.getInputMap(JComponent.WHEN_FOCUSED), myPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT), myPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW), }; for (InputMap i : inputMaps) { i.put(KeyStroke.getKeyStroke("control Z"), UNDO); i.put(KeyStroke.getKeyStroke("control Y"), REDO); i.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), UNDO); } } public void updatePrefMenu() { jRadioButtonDefaultCap.setSelected(!Config.getConfig().isRandomCapacity()); jRadioButtonRandomCap.setSelected(Config.getConfig().isRandomCapacity()); if (!Config.getConfig().isRandomCapacity()) { jTextField1.setText(Config.getConfig().getFixedCapacity() + ""); jTextField1.setEnabled(true); } jSliderPosText.setValue(Config.getConfig().getPosText()); jComboBoxDimCircle.setSelectedItem(Config.getConfig().getDimCircle() + ""); jComboBoxDimText.setSelectedItem(Config.getConfig().getDimText() + ""); StrokeArrowjSlider.setValue((int) Config.getConfig().getStrokeArrow()); StrokeCirclejSlider.setValue((int) Config.getConfig().getStrokeCircle()); } public void resetLabel() { jLabelMin.setText("Min = 0"); jLabelMaxFlow.setText("Flow = 0"); } @Override public void update(Observable arg0, Object arg1) { if (arg0 instanceof Graph) { if ((int) arg1 == 1) { jLabelMaxFlow.setText("Flow = " + ((Graph) arg0).getMaxFlow()); } else if ((int) arg1 == 0) { jLabelMin.setText("Min = " + ((Graph) arg0).getMinFlow()); } } } private class WListener extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { controller.exit(); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JSlider StrokeArrowjSlider; private javax.swing.JSlider StrokeCirclejSlider; private javax.swing.JButton backButton; private javax.swing.JToggleButton dragButton; private javax.swing.JButton forwardButton; private javax.swing.JButton jButton2; private javax.swing.JButton jButtonAttraversatoColor; private javax.swing.JButton jButtonDefaultColor; private javax.swing.JButton jButtonSaturoColor; private javax.swing.JButton jButtonSelezionatoColor; private javax.swing.JButton jButtonSetDefCapacity; private javax.swing.JComboBox<String> jComboBox1; private javax.swing.JComboBox<String> jComboBoxDimCircle; private javax.swing.JComboBox<String> jComboBoxDimText; private javax.swing.JDialog jDialog1; private javax.swing.JDialog jDialog2; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JLabel jLabelMaxFlow; private javax.swing.JLabel jLabelMin; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem10; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem4; private javax.swing.JMenuItem jMenuItem5; private javax.swing.JMenuItem jMenuItem6; private javax.swing.JMenuItem jMenuItem7; private javax.swing.JMenuItem jMenuItem8; private javax.swing.JMenuItem jMenuItem9; private javax.swing.JMenuItem jMenuItemAbout; private javax.swing.JMenuItem jMenuItemExample; private javax.swing.JMenuItem jMenuItemExit; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPopupMenu jPopupMenu1; private javax.swing.JPopupMenu jPopupMenu2; private javax.swing.JRadioButton jRadioButtonDefaultCap; private javax.swing.JRadioButton jRadioButtonRandomCap; private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JPopupMenu.Separator jSeparator2; private javax.swing.JSlider jSliderPosText; private javax.swing.JTabbedPane jTabbedPane2; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JPanel myPanel; private javax.swing.JToggleButton pencilButton; private javax.swing.JToggleButton playButton; private javax.swing.JButton redoButton; private javax.swing.JToggleButton residualButton; private javax.swing.JToggleButton rubberButton; private javax.swing.JButton runButton; private javax.swing.JButton stopButton; private javax.swing.JButton undoButton; // End of variables declaration//GEN-END:variables } class MyPanel extends JPanel { private final EdmondsKarpGui gui; public MyPanel(EdmondsKarpGui gui) { this.gui = gui; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ((Graphics2D) g).setRenderingHints(rh); gui.update(g); } }