import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; /** * a simple applet used to demo the SkinLayout layout manager.
* This is written for JDK1.1. Porting to JDK1.0.2 should be relatively simple - only * requiring changes to the event handling (in other words, remove the listeners and replace * with 1.0.2 event handling code) */ public class TestApplet extends java.applet.Applet { /** * offscreen image size */ private Dimension dimOffScreen; /** * offscreen image - used for double-buffering */ private Image imgOffScreen; /** * offscreen graphics context - used for double-buffering */ private Graphics gOffScreen; /** * layout for the applet */ SkinLayout layout; /** * 'toolbar' back button */ ImageButton backButton; /** * 'toolbar' forward button */ ImageButton forwardButton; /** * 'toolbar' stop button */ ImageButton stopButton; /** * image used for the end of the toolbar */ ImageLabel end; /** * button used to change to layout 1 */ ImageButton layout1Button; /** * button used to change to layout 2 */ ImageButton layout2Button; /** * initialise the applet */ public void init () { try { // call setup at the beginning so any class can load images using a static 'function' call Utils.setup(this); // create a new layout layout = new SkinLayout(Utils.getFileFromServer(getCodeBase() + "layout1.properties")); this.setLayout(layout); // construct buttons backButton = new ImageButton(); forwardButton = new ImageButton(); stopButton = new ImageButton(); end = new ImageLabel(); layout1Button = new ImageButton(); layout2Button = new ImageButton(); this.add(backButton,"back"); this.add(forwardButton,"forward"); this.add(stopButton,"stop"); this.add(end,"end"); this.add(layout1Button,"layout1"); this.add(layout2Button,"layout2"); // the action listener used for changing layouts TestAppletActionListener listener = new TestAppletActionListener(this); layout1Button.addActionListener(listener); layout2Button.addActionListener(listener); this.doLayout(); this.setVisible(true); } catch (Exception e) { e.printStackTrace(); this.stop(); } } /** * handle button clicks */ public void processActionEvent(ActionEvent e) { // change to layout 1 if (e.getSource().equals(layout1Button)) { try { layout.setLayoutFile(Utils.getFileFromServer(getCodeBase() + "layout1.properties")); } catch (IOException ex) { ex.printStackTrace(); } this.doLayout(); this.repaint(); } // change to layout 2 else if (e.getSource().equals(layout2Button)) { try { layout.setLayoutFile(Utils.getFileFromServer(getCodeBase() + "layout2.properties")); } catch (IOException ex) { ex.printStackTrace(); } this.doLayout(); this.repaint(); } } /** * overrides the applet paint method (for double-buffering) */ public void paint(Graphics g) { update(g); } /** * overrides the applet update method (for double-buffering). Drawing of lightweight components happens here */ public void update(Graphics g) { int x,y; Dimension d = getSize(); //Create the offscreen graphics context, if no good one exists. if (gOffScreen == null || d.width != dimOffScreen.width || d.height != dimOffScreen.height) { dimOffScreen = d; imgOffScreen = createImage(d.width, d.height); gOffScreen = imgOffScreen.getGraphics(); } // clear the offscreen graphics context gOffScreen.clearRect(0,0,d.width,d.height); // paint buttons if (backButton.isVisible()) backButton.paint(gOffScreen); if (forwardButton.isVisible()) forwardButton.paint(gOffScreen); if (stopButton.isVisible()) stopButton.paint(gOffScreen); if (end.isVisible()) end.paint(gOffScreen); if (layout1Button.isVisible()) layout1Button.paint(gOffScreen); if (layout2Button.isVisible()) layout2Button.paint(gOffScreen); g.drawImage(imgOffScreen,0,0,this); d = null; } } /** * action listener for the applet */ class TestAppletActionListener implements ActionListener { TestApplet parent; public TestAppletActionListener(TestApplet parent) { this.parent = parent; } public void actionPerformed(ActionEvent e) { parent.processActionEvent(e); } }