C:\Documents and Settings\01031010262\Desktop\Apresentação_FINAL\Codigo2\DinningPhilsophers\src\DinningTable.java
package DiningPhilosophers;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


@SuppressWarnings("serial")
public class DinningTable extends JFrame implements ActionListener
{
        private final int [] Xcord = {20, 240, 500,430 ,130};
        private final int [] Ycord = {251,50,251,452,452};
        private final String[] names = {"Socrates", "Platão","Aristoteles", "Plutão", "Pantaleão"};
        private final int THINKING = 0, HUNGRY=1, EATING=2;
        private final int NPHILS = 5;
        
        private int[] state = null;
        
        private Semaphore mutex = null;
        private Semaphore[] self = null;
                        
        private Philosopher[] philosophers = null;
        
        private JButton run;
        private JButton pause;
        
        public DinningTable()
        {
                super("Dinning Philosophers");
                philosophers = new Philosopher[NPHILS];
                Init();
                state = new int[NPHILS];
                self = new Semaphore[NPHILS];
                
                for (int i=0; i<NPHILS; i++)
                {
                        state[i] = THINKING;
                        self[i] = new Semaphore(0);
                }
                mutex = new Semaphore(1);
        }
        
        public void Init()
        {
                this.setSize(800,600);
                this.getContentPane().setLayout(null);
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
                
                for(int i =0; i<NPHILS; i++)
                {
                        philosophers[i] = new Philosopher(this,i,names[i]);
                        philosophers[i].setVisible(true);
                        philosophers[i].setBounds(Xcord[i],Ycord[i],200,300);
                        philosophers[i].setForeground(Color.BLACK);
                        this.getContentPane().add(philosophers[i]);
                }
                JLabel tab = new JLabel (new ImageIcon("./images/table.jpg"));
                tab.setBounds(240,230, 200, 300);
                tab.setVisible(true);
                this.getContentPane().add(tab);
                
                run = new JButton();
                run.setText("Run");
                run.setBounds(500,50,100,40);
                run.addActionListener(this);
                run.setEnabled(true);
                
                pause = new JButton();
                pause.setText("Kill");
                pause.setBounds(630,50,100,40);
                pause.addActionListener(this);
                pause.setEnabled(false);
                                
                this.getContentPane().add(run);
                this.getContentPane().add(pause);
        }
        
        public static void main (String[] args)
        {
                DinningTable t = new DinningTable();
                t.setVisible(true);
        }
        
        private final int right(int i) { return (NPHILS + i - 1) % NPHILS; }
        
        private final int left(int i) { return (i + 1) % NPHILS; }
        
        private void test(int k)
        {
                if (state[left(k)] != EATING && state[right(k)] != EATING && state[k] == HUNGRY) 
                {
                 state[k] = EATING;
                 philosophers[k].anEat();
                 self[k].Up();
              }
        }
        
        public void getForks (int i)
        {
                mutex.Down();
            state[i] = HUNGRY;
            philosophers[i].anHungry();
            test(i);
            mutex.Up();
            self[i].Down();
        }
        
        public void releaseForks(int i)
        {
                mutex.Down();
            state[i] = THINKING;
            philosophers[i].AnThink();
            test(left(i));
            test(right(i));
            mutex.Up();
        }

        public void actionPerformed(ActionEvent arg0) 
        {
                if(arg0.getSource() == run)
                        Start();
                else
                        Pause();
        }       
        
        public void Start()
        {
                for (int i=0; i<NPHILS; i++)
                {
                        //philosophers[i] = new Philosopher(this,i,names[i]);
                        Thread t = new Thread(philosophers[i]);
                        t.start();
                }
                run.setEnabled(false);
                pause.setEnabled(true);                 
        }
        
        public void Pause()
        {
                for (int i=0; i<NPHILS; i++)
                        philosophers[i].Kill();
                pause.setEnabled(false);
        }
}