FileDocCategorySizeDatePackage
DiningPhilosophers.javaAPI DocExample5846Tue Dec 12 18:57:46 GMT 2000None

DiningPhilosophers

public class DiningPhilosophers extends JApplet implements ActionListener, ChangeListener

Fields Summary
private JButton
stopStartButton
int
grabDelay
private JSlider
grabDelaySlider
private JLabel
label
private JPanel
philosopherArea
public ImageIcon[]
imgs
Chopstick[]
chopsticks
String[]
names
static final int
NUMPHILS
static final int
HUNGRYDUKE
static final int
RIGHTSPOONDUKE
static final int
BOTHSPOONSDUKE
private int
width
private int
height
private double
spacing
private static final double
MARGIN
private Philosopher[]
philosophers
Constructors Summary
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (stopStartButton.getText().equals("stop/reset")) {
            stopPhilosophers();
            stopStartButton.setText("start");
        } else if (stopStartButton.getText().equals("start")) {
            startPhilosophers();
            stopStartButton.setText("stop/reset");
        }
    
public java.awt.DimensioncreatePhilosophersAndChopsticks()

        double x, y;
        double radius = 80.0;
        double centerAdj = 85.0;
        double radians;

        Dimension preferredSize = new Dimension(0, 0);

/* for a straight line
        y = MARGIN;
*/
        for (int i = 0; i < NUMPHILS; i++)
            chopsticks[i] = new Chopstick();

        for (int i = 0; i < NUMPHILS; i++) {
/* for a straight line
            x = i * spacing;
*/
            radians = i*(2.0 * Math.PI /(double)NUMPHILS);
            x = Math.sin(radians) * radius + centerAdj; 
            y = Math.cos(radians) * radius + centerAdj; 
            philosophers[i] = new Philosopher(this, i, imgs[HUNGRYDUKE]);
            philosophers[i].setBounds((int)x, (int)y, width, height);
            philosopherArea.add(philosophers[i]);
            if ((int)x > preferredSize.width)
		preferredSize.width = (int)x;
            if ((int)y > preferredSize.height)
		preferredSize.height = (int)y;
        }

        preferredSize.width += width;
        preferredSize.height += height;
        return preferredSize;
    
protected java.net.URLgetURL(java.lang.String filename)

        URL codeBase = getCodeBase();
        URL url = null;

        try {
            url = new URL(codeBase, filename);
        } catch (java.net.MalformedURLException e) {
            System.out.println("Couldn't create image: "
                             + "badly specified URL");
            return null;
        }

        return url;
    
public voidinit()


       

        imgs[HUNGRYDUKE] = new ImageIcon(getURL("images/hungryduke.gif"));
        imgs[RIGHTSPOONDUKE] = new ImageIcon(getURL("images/rightspoonduke.gif"));
        imgs[BOTHSPOONSDUKE] = new ImageIcon(getURL("images/bothspoonsduke.gif"));

        width = imgs[HUNGRYDUKE].getIconWidth() + (int)(MARGIN*2.0);
        height = imgs[HUNGRYDUKE].getIconHeight() + (int)(MARGIN*2.0);
        spacing = width + MARGIN;

        GridBagLayout gridBag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        JPanel contentPane = new JPanel();
        contentPane.setLayout(gridBag);

        philosopherArea = new JPanel(null);
        philosopherArea.setBackground(Color.white);
        Dimension preferredSize = createPhilosophersAndChopsticks();
        philosopherArea.setBorder(BorderFactory.createCompoundBorder(
                        BorderFactory.createLoweredBevelBorder(),
                        BorderFactory.createEmptyBorder(5, 5, 5, 5)
        ));
        philosopherArea.setPreferredSize(preferredSize);

        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1.0;
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        gridBag.setConstraints(philosopherArea, c);
        contentPane.add(philosopherArea);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        c.weighty = 0.0;
        gridBag.setConstraints(stopStartButton, c);
        contentPane.add(stopStartButton);

        c.gridwidth = GridBagConstraints.RELATIVE; //don't end row
        c.weightx = 1.0;
        c.weighty = 0.0;
        gridBag.setConstraints(grabDelaySlider, c);
        contentPane.add(grabDelaySlider);

        c.weightx = 0.0;
        c.gridwidth = GridBagConstraints.REMAINDER; //end row
        gridBag.setConstraints(label, c);
        contentPane.add(label);
        contentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        setContentPane(contentPane);

	stopStartButton.addActionListener(this);
	grabDelaySlider.addChangeListener(this);

    
public voidstartPhilosophers()

        for (int i = 0; i < NUMPHILS; i++)
            philosophers[i].philThread.start();
    
public voidstateChanged(javax.swing.event.ChangeEvent e)

        JSlider source = (JSlider)e.getSource();
        grabDelay = source.getValue()*100;
        label.setText(String.valueOf(grabDelay + " milliseconds"));
    
public voidstopPhilosophers()

        for (int i = 0; i < NUMPHILS; i++)
            philosophers[i].philThread.interrupt();