/* * ====================================================\\\\\============= * DemoTimer.java : This applet flashes the text label "Swing can flash!" * in a variety of colors. The applet has control buttons to start, * restart, and stop the flashing, and slider bars to contol the * delay to firing events and time between color changes. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * ================================================================= */ import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.*; public class DemoTimer extends JApplet { Container container = null; Timer timer = null; JLabel label = null; JSlider slider1 = null; JSlider slider2 = null; Color[] color = { Color.blue, Color.green, Color.red, Color.yellow, Color.lightGray }; public void init() { // 1. Get the handle on the applet's content pane. container = this.getContentPane(); // 2. Create a label and attach it to the applet. label = new JLabel("Swing can flash!", JLabel.CENTER); label.setBackground(Color.white); label.setFont(new Font("Dialog", Font.BOLD, 40)); label.setOpaque(true); container.add(label); // 3. Create a horizontal box and attach it at the // bottom portion of the content pane. Box box = Box.createHorizontalBox(); container.add(box, BorderLayout.SOUTH); // 4. Create a vertical box and add it to the horizontal box. Box vbox1 = Box.createVerticalBox(); box.add(vbox1); // 5. Create labels and sliders and attach them to // a vertical box. JLabel initDelay = new JLabel("Initial Delay", JLabel.CENTER); initDelay.setPreferredSize(new Dimension(200, 25)); vbox1.add(initDelay); slider1 = new JSlider(JSlider.HORIZONTAL, 0, 60000, 0); slider1.addChangeListener(new SliderListener()); vbox1.add(slider1); JLabel delay = new JLabel("Timer Delay", JLabel.CENTER); delay.setPreferredSize(new Dimension(200, 25)); vbox1.add(delay); slider2 = new JSlider(JSlider.HORIZONTAL, 0, 2000, 1000); slider2.addChangeListener(new SliderListener()); vbox1.add(slider2); // 6. Create another vertical box and add it to the // horizontal box. Box vbox2 = Box.createVerticalBox(); box.add(vbox2); // 7. Create a Swing panel with grid layout and add it to // the vertical box created in Snippet 6. JPanel panel = new JPanel(); panel.setLayout(new GridLayout(2,2,5,5)); vbox2.add(panel); // 8. Create Start, Stop, and Restart buttons, and add them // to the panel. String[] buttonLabels = {"Start", "Stop", "Restart"}; for (int i=0; i