Source of SplashScreen.java


  1: //SplashScreen.java
  2: //Shows some claims about Java in rotating "splash screens".

  4: import java.awt.BorderLayout;
  5: import java.awt.Color;
  6: import java.awt.Dimension;
  7: import java.awt.Font;
  8: import java.awt.Toolkit;
  9: import java.awt.event.KeyAdapter;
 10: import java.awt.event.KeyEvent;
 11: import java.awt.event.KeyListener;
 12: import java.util.Random;
 13: import javax.swing.BorderFactory;
 14: import javax.swing.JLabel;
 15: import javax.swing.JPanel;
 16: import javax.swing.JFrame;
 17: import javax.swing.JWindow;

 19: public class SplashScreen extends JFrame
 20: {
 21:     private String[] textChoices =
 22:         {"Java is object-oriented.",      "Java is simple.",
 23:          "Java is distributed.",          "Java is interpreted.",
 24:          "Java is robust.",               "Java is secure.",
 25:          "Java is architecture neutral.", "Java is portable.",
 26:          "Java is high performance.",     "Java is multithreaded.",
 27:          "Java is dynamic.",              "Java is neat.",
 28:          "Java is for you."};
 29:     static int whichOne = 0;

 31:     public SplashScreen()
 32:     {
 33:         setUndecorated(true);

 35:         JPanel content = (JPanel)getContentPane();
 36:         content.setBackground(Color.WHITE);

 38:         //Set the window's bounds
 39:         int width = 450;
 40:         int height =115;
 41:         Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
 42:         int x = new Random().nextInt(screen.width-width);
 43:         int y = new Random().nextInt(screen.height-height);
 44:         setBounds(x, y, width, height);

 46:         //Build the splash screen
 47:         String textChoice = textChoices[whichOne++ % textChoices.length];
 48:         JLabel label = new JLabel(textChoice, JLabel.CENTER);
 49:         label.setFont(new Font("Sans-Serif", Font.BOLD, 24));
 50:         content.add(label);
 51:         Color aReddishColor = new Color(156, 20, 20,  255);
 52:         content.setBorder(BorderFactory.createLineBorder(aReddishColor, 10));

 54:         addKeyListener(new KeyHandler());
 55:     }

 57:    //Display the window, wait a bit, then hide it
 58:     public void show(int duration)
 59:     {

 61:         setVisible(true);
 62:         try { Thread.sleep(duration); } catch (Exception e) {}
 63:         setVisible(false);
 64:     }

 66:     class KeyHandler extends KeyAdapter
 67:     {
 68:         public void keyPressed(KeyEvent e)
 69:         {
 70:             System.exit(0);
 71:         }
 72:     }

 74:     public static void main(String[] args)
 75:     {
 76:         while (true)
 77:         {
 78:             SplashScreen screen = new SplashScreen();
 79:             int multiplier = new Random().nextInt(4) + 2;
 80:             screen.show(multiplier * 1000);
 81:         }
 82:     }
 83: }