// HelloWithAnthem.java: Applet + Application, a flashing hello import java.awt.*; // Graphics, Color, and Font import java.applet.*; // Audio import java.net.URL; import javax.swing.*; // JApplet and JPanel public class HelloWithAnthem extends JApplet { private URL url = this.getClass().getResource("anthem/us.mid"); private AudioClip musica = Applet.newAudioClip(url); public void init() { getContentPane().add(new MsgPanel3(2000, Color.yellow)); musica.loop(); } public static void main(String[] args) { JFrame f = new JFrame("I am an Appication"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); HelloWithAnthem ha = new HelloWithAnthem(); f.getContentPane().add(ha, BorderLayout.CENTER); ha.init(); ha.start(); f.setSize(350,130); f.setVisible(true); } } class MsgPanel3 extends JPanel implements Runnable { private boolean show = true; private Thread thrd = null; private int msec; private Color c; public MsgPanel3(int ms, Color c) { msec = ms; this.c = c; thrd = new Thread(this); thrd.start(); } public void run() { while(true) { repaint(); try { thrd.sleep(msec); } catch (InterruptedException ex) { System.out.println(ex); } } } public void paintComponent(Graphics g) { super.paintComponent(g); // to clear the viewing area g.setFont( new Font("Arial", Font.BOLD, 16) ); setBackground(c); if(show) g.drawString("Hello", 100, 50); show = !show; } } // Chung (c) 2000