Source of SplashScreenRandom.java


  1: //SplashScreenRandom.java
  2: //Shows some claims about Java in randomly appearing "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 SplashScreenRandom 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."};

 30:     public SplashScreenRandom()
 31:     {
 32:         setUndecorated(true);

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

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

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

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

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

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

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

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