FramedArea.java
, which is part of the Dining Philosophers applet, uses deprecated methods: Thesize
method and theinsets
method. In 1.1, these methods have been renamed togetSize
andgetInsets
, respectively. In addition, the applet uses the old event handling mechanism.Here's the JDK 1.1 version of the
FramedArea.java
file using the new method name:In addition,import java.awt.*; class FramedArea extends Panel { PhilosopherArea philosopherArea; public FramedArea(PhilAnimator1_1 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 getInsets() { 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(); } }PhilosopherArea.java
, another part of the Dining Philosophers applet, also uses thesize
method and uses the old event handling mechanism. Here's the JDK 1.1 version of thePhilAnimator.java
file using the new event handling scheme:Let's run it!import java.awt.*; // for dealing with the start stop button import java.awt.event.ActionListener; import java.awt.event.ActionEvent; // for dealing with the delay slider import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; public class PhilAnimator1_1 extends java.applet.Applet implements ActionListener, AdjustmentListener { Button stopStartButton = new Button("start"); // delays can go from 500 to 10,000 (they get multiplied by 100 in Philosopher Scrollbar grabDelaySlider = new Scrollbar(Scrollbar.HORIZONTAL, 5, 1, 0, 100); Label label = new Label(" 500 milliseconds"); FramedArea framedArea; public void init() { GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridBag); framedArea = new FramedArea(this); c.fill = GridBagConstraints.BOTH; c.weighty = 1.0; c.gridwidth = GridBagConstraints.REMAINDER; //end row gridBag.setConstraints(framedArea, c); add(framedArea); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1.0; c.weighty = 0.0; gridBag.setConstraints(stopStartButton, c); add(stopStartButton); c.gridwidth = GridBagConstraints.RELATIVE; //don't end row c.weightx = 1.0; c.weighty = 0.0; gridBag.setConstraints(grabDelaySlider, c); add(grabDelaySlider); c.weightx = 0.0; c.gridwidth = GridBagConstraints.REMAINDER; //end row gridBag.setConstraints(label, c); add(label); validate(); stopStartButton.addActionListener(this); grabDelaySlider.addAdjustmentListener(this); } public void actionPerformed(ActionEvent e) { if (stopStartButton.getLabel().equals("stop/reset")) { framedArea.stopButton(); stopStartButton.setLabel("start"); } else if (stopStartButton.getLabel().equals("start")) { framedArea.startButton(); stopStartButton.setLabel("stop/reset"); } } public void adjustmentValueChanged(AdjustmentEvent e) { label.setText(String.valueOf(100*grabDelaySlider.getValue()) + " milliseconds"); } }
For details about this and other changes to the AWT see GUI Changes: The AWT Grows Up .