// // SwingingApplet program // Version B1.0 completed May 20, 1998 mea // // Author: Mark Andrews // import java.awt.*; import java.awt.event.*; import java.net.*; import java.applet.*; import javax.swing.*; import java.util.Vector; import java.util.StringTokenizer; public class SwingingApplet extends JApplet implements java.awt.event.ActionListener { JToggleButton btnToggle; JButton btnImage; JLabel labelImage; // Set up strings for image filenames String fnSwing = new String("images/Swing-64.gif"); String fnStop = new String("images/Swing-64_still.gif"); boolean DEBUG = true; public void init() { // Get the first image now so it can start loading. // Picture first = picStop; ImageIcon icon = new ImageIcon(getURL(fnStop)); // Create the GUI components. // Set label properties. labelImage = new JLabel(icon); labelImage.setHorizontalAlignment(JLabel.CENTER); labelImage.setVerticalAlignment(JLabel.CENTER); labelImage.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0) ); // Set picture properties. btnImage = new JButton(icon); btnImage.setHorizontalAlignment(JLabel.CENTER); btnImage.setVerticalAlignment(JLabel.CENTER); btnImage.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0) ); // Set tool-tip text. // labelImage.setToolTipText("Duke swinging"); btnImage.setToolTipText("Duke says hi!"); // Set the preferred size for the label, // and add room for the borders. Insets i1 = labelImage.getBorder().getBorderInsets(labelImage); labelImage.setPreferredSize(new Dimension(64,64)); // Set the preferred size for the picture, // and add room for the borders. Insets i2 = labelImage.getBorder().getBorderInsets(labelImage); btnImage.setPreferredSize(new Dimension(64,64)); // Create and initialize a toggle button. btnToggle = new JToggleButton("Swing!", false); btnToggle.setVerticalTextPosition(AbstractButton.CENTER); btnToggle.setHorizontalTextPosition(AbstractButton.CENTER); btnToggle.setActionCommand("btnToggle"); btnToggle.addActionListener(this); // Lay out the GUI (using box layout) Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); // contentPane.add(labelImage); contentPane.add(btnImage); contentPane.add(btnToggle); btnToggle.setAlignmentX(Component.CENTER_ALIGNMENT); // labelImage.setAlignmentX(Component.CENTER_ALIGNMENT); btnImage.setAlignmentX(Component.CENTER_ALIGNMENT); } // Action method -- executed when the user clicks the button. public void actionPerformed(java.awt.event.ActionEvent e) { ImageIcon icon = null; btnImage.setToolTipText(""); labelImage.setIcon(null); labelImage.setText("Loading..."); if (btnToggle.isSelected()) { btnToggle.setSelected(true); btnToggle.setText("Stop!"); icon = new ImageIcon(getURL(fnSwing)); btnImage.setIcon(icon); icon.setImageObserver(btnImage); // Use for animated gifs. btnImage.setToolTipText("Duke is Swinging!"); } else { btnToggle.setSelected(false); btnToggle.setText("Swing!"); icon = new ImageIcon(getURL(fnStop)); btnImage.setIcon(icon); btnImage.setToolTipText("Howdy, world!"); } labelImage.setText(null); labelImage.setIcon(icon); } // Method to load an image using its URL. /* * XXX: This method should be made obsolete by a new ImageIcon * XXX: constructor: ImageIcon(URL, String, String). */ protected URL getURL(String filename) { URL codeBase = this.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; } }