import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class HelloWorldSpectrum extends JApplet
{
    class Display extends JPanel
    {

        Color color;
        Font textFont;

        void setColor(Color color1)
        {
            color = color1;
            repaint();
        }

        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(color);
            g.setFont(textFont);
            g.drawString("Hello World!", 25, 50);
        }

        Display()
        {
            setBackground(Color.black);
            color = Color.red;
            textFont = new Font("Serif", 1, 36);
        }
    }


    Display display;
    JButton startStopButton;
    Timer timer;
    int colorIndex;
    ActionListener timerListener;

    public HelloWorldSpectrum()
    {
        timerListener = new ActionListener() 
        {

            public void actionPerformed(ActionEvent actionevent)
            {
                colorIndex++;
                if(colorIndex > 100)
                    colorIndex = 0;
                float f = (float)colorIndex / 100F;
                display.setColor(Color.getHSBColor(f, 1.0F, 1.0F));
            }

        };
    }

    public void init()
    {
        display = new Display();
        getContentPane().add(display, "Center");
        JPanel jpanel = new JPanel();
        jpanel.setBackground(Color.gray);
        getContentPane().add(jpanel, "South");
        startStopButton = new JButton("Start");
        jpanel.add(startStopButton);
        startStopButton.addActionListener(new ActionListener() 
        {

            public void actionPerformed(ActionEvent actionevent)
            {
                if(timer == null)
                    startAnimation();
                else
                    stopAnimation();
            }

        });
    }

    void startAnimation()
    {
        if(timer == null)
        {
            timer = new Timer(50, timerListener);
            timer.start();
            startStopButton.setText("Stop");
        }
    }

    void stopAnimation()
    {
        if(timer != null)
        {
            timer.stop();
            timer = null;
            startStopButton.setText("Start");
        }
    }

    public void stop()
    {
        stopAnimation();
    }
}

(Applet "HelloWorldSpectrum" would be displayed here
if Java were available.)

Back to Lesson 32 Examples

Back to Java Main Page