SHARE
TWEET

Untitled

a guest Oct 18th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.SwingUtilities;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.util.Hashtable;
  7. import javax.swing.JSlider;
  8. import javax.swing.JLabel;
  9.  
  10. class PrisonersDilemma implements ActionListener, ChangeListener {
  11.     // Adding frame
  12.     JFrame frame;
  13.  
  14.     // Adding panels
  15.     PlayingField field; // panel for playingfield
  16.     JPanel interactive; // panel for interactions
  17.     JPanel buttonPanel; // panel for buttons
  18.     JPanel sliderPanel; // panel for sliders
  19.     JPanel defectionPanel; // defection panel
  20.     JPanel frequencyPanel; // frequency panel
  21.    
  22.     // Adding buttons
  23.     JButton goButton; // create go button
  24.     JButton pauseButton; // create pause button
  25.     JButton resetButton; // create reset button
  26.    
  27.     //Adding sliders
  28.     JSlider defectionSlider;
  29.     JSlider frequencySlider;
  30.    
  31.     //Defection slider properties
  32.     static final int defection_MIN = 0; //
  33.     static final int defection_MAX = 30; //
  34.     static final int defection_INIT = 15; // initial defection factor
  35.    
  36.     //frequency slider properties
  37.     static final int frequency_MIN = 0; //
  38.     static final int frequency_MAX = 60; //
  39.     static final int frequency_INIT = 1; // initial defection factor
  40.    
  41.     // Timer
  42.     Timer timer;
  43.     int stepTime = 100;
  44.    
  45.     // settings of the slider
  46.     private int settingMax = 300;
  47.     private int settingInit = 150;
  48.     private double alpha;
  49.    
  50.     void setAward(int setting) {
  51.       alpha = (double) setting / 100;  
  52.     }
  53.  
  54.     void buildGUI() {
  55.         SwingUtilities.invokeLater( () -> {
  56.           field = new PlayingField();
  57.           frame = new JFrame("Prisoners Dilemma by Rohan & Marijn");
  58.  
  59.           frame.setSize(700,700); // set size of frame
  60.           frame.setVisible(true);
  61.           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  62.          
  63.           // Panel setup
  64.           interactive = new JPanel();
  65.           interactive.setLayout(new BorderLayout());
  66.           buttonPanel = new JPanel();
  67.           sliderPanel = new JPanel();
  68.           sliderPanel.setLayout(new BorderLayout());
  69.           defectionPanel = new JPanel();
  70.           defectionPanel.setLayout(new BorderLayout());
  71.           frequencyPanel = new JPanel();
  72.           frequencyPanel.setLayout(new BorderLayout());
  73.          
  74.           // button setup
  75.           resetButton = new JButton("Reset");
  76.           resetButton.addActionListener(this);
  77.           pauseButton = new JButton("Pause");
  78.           pauseButton.addActionListener(this);
  79.           goButton = new JButton("Go");
  80.           goButton.addActionListener(this);
  81.          
  82.           // defection slider setup
  83.           defectionSlider = new JSlider(JSlider.HORIZONTAL, defection_MIN,
  84.                                         defection_MAX, defection_INIT);
  85.          
  86.           Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
  87.           labelTable.put(0, new JLabel("0.0"));
  88.           labelTable.put(10, new JLabel("1.0"));
  89.           labelTable.put(20, new JLabel("2.0"));
  90.           labelTable.put(30, new JLabel("3.0"));
  91.           defectionSlider.setLabelTable(labelTable);
  92.           defectionSlider.setPaintLabels(true);
  93.          
  94.           // frequency slider setup
  95.           frequencySlider = new JSlider(JSlider.HORIZONTAL, frequency_MIN,
  96.                                         frequency_MAX, frequency_INIT);
  97.                
  98.           frequencySlider.setMajorTickSpacing(10);
  99.           frequencySlider.setPaintTicks(true);
  100.           frequencySlider.setPaintLabels(true);
  101.          
  102.           // defection panel building
  103.           defectionPanel.add(new JLabel("defection factor = "));
  104.           defectionPanel.add(defectionSlider, BorderLayout.EAST);
  105.          
  106.           // frequency panel building
  107.           frequencyPanel.add(new JLabel("frequency = "));
  108.           frequencyPanel.add(frequencySlider, BorderLayout.EAST);
  109.          
  110.           // slider panel building
  111.           sliderPanel.add(defectionPanel, BorderLayout.WEST);
  112.           sliderPanel.add(frequencyPanel, BorderLayout.EAST);
  113.          
  114.           // button Panel building
  115.           buttonPanel.add(goButton, BorderLayout.WEST);
  116.           buttonPanel.add(pauseButton, BorderLayout.CENTER);
  117.           buttonPanel.add(resetButton, BorderLayout.EAST);
  118.          
  119.           // interactive panel building
  120.           interactive.add(buttonPanel, BorderLayout.SOUTH);
  121.           interactive.add(sliderPanel, BorderLayout.NORTH);
  122.          
  123.           frame.add(interactive, BorderLayout.SOUTH);
  124.          
  125.           timer = new Timer(stepTime, null);
  126.           timer.addActionListener(this);
  127.  
  128.           // Frame Building
  129.           field = new PlayingField();
  130.           frame.add(field, BorderLayout.CENTER);
  131.  
  132.         } );
  133.     }
  134.    
  135.     // add actions to buttons
  136.     @Override
  137.     public void actionPerformed(ActionEvent e) {
  138.       if (e.getSource() == timer) {
  139.           field.step();
  140.       } else if (e.getActionCommand().equals("GO")) {
  141.           timer.start();
  142.       } else if (e.getActionCommand().equals("Pause")) {
  143.           timer.stop();
  144.       } else if (e.getActionCommand().equals("Reset")) {
  145.             field.revalidate();
  146.             field = new PlayingField();
  147.             frame.add(field, BorderLayout.CENTER);
  148.       }    
  149.     }
  150.    
  151.     @Override
  152.     public void stateChanged(ChangeEvent e) {
  153.       this.setAward(defectionSlider.getValue());
  154.     }
  155.    
  156.     public static void main( String[] a ) {
  157.         (new PrisonersDilemma()).buildGUI();
  158.     }
  159. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top