Previous


1.1 Changes: Philosophers Applet

FramedArea.java, which is part of the Dining Philosophers applet, uses the size method. In 1.1 this method has been renamed to getSize. Here's the JDK 1.1 version of the FramedArea.java file using the new method name:
import java.awt.*;

class FramedArea extends Panel {
    PhilosopherArea philosopherArea;

    public FramedArea(PhilAnimator controller) {
        super();

        //Set layout to one that makes its contents as big as possible.
        setLayout(new GridLayout(1,0));

	philosopherArea = new PhilosopherArea(controller);
        add(philosopherArea);
        validate();
    }

    public Insets insets() {
        return new Insets(4,4,5,5);
    }

    public void paint(Graphics g) {
        Dimension d = getSize();
        Color bg = getBackground();

        g.setColor(bg);
        g.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
        g.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
    }

    public void stopButton() {
	philosopherArea.stopPhilosophers();
	philosopherArea.createPhilosophersAndChopsticks();
	philosopherArea.repaint();
    }

    public void startButton() {
	philosopherArea.startPhilosophers();
    }
}
In addition, PhilosopherArea.java, another part of the Dining Philosophers applet, also uses the size method. Here's the JDK 1.1 version of the PhilosopherArea.java file using the new method name:
import java.awt.*;

class PhilosopherArea extends Canvas {
    PhilAnimator controller;

    static final double MARGIN = 10.0f;
    static final int NUMPHILS = 5;

    Image[] imgs = new Image[3];

    double spacing;

    Philosopher[] philosophers = new Philosopher[NUMPHILS];
    Chopstick[] chopsticks = new Chopstick[NUMPHILS];
    boolean[] redraw = new boolean[NUMPHILS];
    String[] names = { "Arisduktle" , "Dukrates", "Pythagorduke", "Duko", "Dukimedes" };

    boolean running = false;

    public PhilosopherArea(PhilAnimator controller) {
        super();
        this.controller = controller;

        MediaTracker mt;
        mt = new MediaTracker(this);

        imgs[0] = controller.getImage(controller.getCodeBase(), "hungryduke.gif");
        mt.addImage(imgs[0], 0);
        imgs[1] = controller.getImage(controller.getCodeBase(), "rightspoonduke.gif");
        mt.addImage(imgs[1], 1);
        imgs[2] = controller.getImage(controller.getCodeBase(), "bothspoonsduke.gif");
        mt.addImage(imgs[2], 2);

        try {
            mt.waitForID(0);
            mt.waitForID(1);
            mt.waitForID(2);
        } catch (java.lang.InterruptedException e) {
            System.out.println("Couldn't load one of the images");
        }

        spacing = imgs[0].getWidth(this) + MARGIN;
        createPhilosophersAndChopsticks();
    }

    public void paint(Graphics g) {
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, getSize().width, getSize().height);
        for (int i = 0; i < NUMPHILS; i++) {
            redraw[i] = true;
        }
        update(g);
    }

    public void update(Graphics g) {
        for (int i = 0; i < NUMPHILS; i++) {
            if (redraw[i]) {
                philosophers[i].paint(g);
                redraw[i] = false;
            }
        }
    }

    public boolean mouseDown(java.awt.Event evt, int x, int y) {
        if (running) {
            for (int i = 0; i < NUMPHILS; i++)
                philosophers[i].suspend();
        } else {
            for (int i = 0; i < NUMPHILS; i++)
                philosophers[i].resume();
        }

        running = !running;
        return true;
    }

    public synchronized void repaintPhil(int id) {
        redraw[id] = true;
        repaint();
    }

    public void startPhilosophers() {
        for (int i = 0; i < NUMPHILS; i++)
            philosophers[i].start();
        running = true;
    }

    public void stopPhilosophers() {
        for (int i = 0; i < NUMPHILS; i++)
            philosophers[i].stopRequested();
    }

    public void createPhilosophersAndChopsticks() {
        double x, y;
        double radius = 80.0;
        double centerAdj = 85.0;
        double radians;

/* 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, x, y, i);
            repaintPhil(i);   
        }
    }
}
For details about this and other changes to the AWT see GUI Changes: The AWT Grows Up (in the new JDK 1.1 documentation).


Previous