Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Myra WebTools - RoundManager.java

RoundManager.java

Caricato da: Ht-never
Scarica il programma completo

  1. package MyraTools;
  2.  
  3. import java.awt.Color;
  4. import java.awt.GridLayout;
  5. import java.awt.TextArea;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.Random;
  9. import javax.swing.*;
  10.  
  11. @SuppressWarnings("serial")
  12. public class RoundManager extends JPanel {
  13.         // text fields
  14.         static boolean checks[];
  15.         static TextArea t1 = new TextArea();
  16.         static JTextField fields[] = {
  17.                 new JTextField(),
  18.                 new JTextField()
  19.         };
  20.         // scritte
  21.         static JLabel labels[] = {
  22.                 new JLabel("Numero Utenti: "),
  23.                 new JLabel("Numero Compiti: ")
  24.         };
  25.         JButton go = new JButton("Calcola");
  26.        
  27.         public RoundManager() {
  28.                 this.setBounds(0,0,100,200);
  29.                 VoidPanel p[] = {
  30.                         new VoidPanel(),
  31.                         new VoidPanel()
  32.                 };
  33.                 setLayout(new GridLayout(4,2));
  34.                 int a = 1;
  35.                 // aggiungo elementi tramite Array di oggetti
  36.                 while(a >= 0) {
  37.                         p[1-a].add(labels[1-a]);
  38.                         p[1-a].add(fields[1-a]);
  39.                         a--;
  40.                 } a = 1;
  41.                 while(a >= 0) {
  42.                         this.add(p[1-a]);
  43.                         a--;
  44.                 }
  45.                 add(t1);
  46.                 go.addActionListener(new Generator());
  47.                 add(go); // bottone di invio
  48.         }
  49.         public boolean allChecked() {
  50.                 int i = checks.length - 1;
  51.                 System.out.println(checks.length);
  52.                 while(i >= 0) {
  53.                         if(!checks[i]) return false;
  54.                         i--;
  55.                 }
  56.                 return true;
  57.         }
  58.         public boolean isChecked(int a) {
  59.                 if(checks[a])
  60.                         return true;
  61.                 else
  62.                         return false;
  63.         }
  64.         public class Generator implements ActionListener {
  65.                 Random gen = new Random();
  66.                 int doc = 0, pat = 0;
  67.                 public void actionPerformed(ActionEvent e) {
  68.                         doc = Integer.parseInt(fields[0].getText());
  69.                         pat = Integer.parseInt(fields[1].getText());
  70.                         checks = new boolean[doc];
  71.                         while(((pat >= 1)||(doc >= 1)) && !allChecked()) {
  72.                                 int temp = gen.nextInt(doc);
  73.                                 while(isChecked(temp)) {
  74.                                         temp++;
  75.                                 }
  76.                                 System.out.println(doc);
  77.                                 t1.setText(t1.getText()+"Al Utente " +
  78.                                                 doc + " è stato assegnato il Compito" +
  79.                                                 " " + (temp+1) + "\n");
  80.                                 checks[temp] = true;
  81.                                 pat--;
  82.                                 doc--;
  83.                         }
  84.                 }
  85.         }
  86.         public class VoidPanel extends JPanel {
  87.                 public VoidPanel() {
  88.                         setBackground(Color.gray);
  89.                         setLayout(new GridLayout(1,2));
  90.                 }
  91.         }
  92. }