HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"JSplash & JTips"
Volume: 3 Issue: 11, p. 14

	

Listing 1.
 
public class JSplash extends JWindow 
  implements KeyListener, MouseListener, ActionListener 
{ 
  public JSplash(JFrame parent, String filename, int timeout) 
  { 
    super(parent); 
    ImageIcon image = new ImageIcon(filename); 
    int w = image.getIconWidth() + 5; 
    int h = image.getIconHeight() + 5; 

    Dimension screen = 
      Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (screen.width - w) / 2; 
    int y = (screen.height - h) / 2; 
    setBounds(x, y, w, h); 

    getContentPane().setLayout(new BorderLayout()); 
    JLabel picture = new JLabel(image); 
    getContentPane().add("Center", picture); 
    picture.setBorder(new BevelBorder(BevelBorder.RAISED)); 

    // Listen for key strokes 
    addKeyListener(this); 

    // Listen for mouse events from here and parent 
    addMouseListener(this); 
    parent.addMouseListener(this); 

    // Timeout after a while 
    Timer timer = new Timer(0, this); 
    timer.setRepeats(false); 
    timer.setInitialDelay(timeout); 
    timer.start(); 
  } 

  public void block() 
  { 
    while(isVisible()) {} 
  } 

  // Dismiss the window on a key press 
  public void keyTyped(KeyEvent event) {} 
  public void keyReleased(KeyEvent event) {} 
  public void keyPressed(KeyEvent event) 
  { 
    setVisible(false); 
    dispose(); 
  } 

  // Dismiss the window on a mouse click 
  public void mousePressed(MouseEvent event) {} 
  public void mouseReleased(MouseEvent event) {} 
  public void mouseEntered(MouseEvent event) {} 
  public void mouseExited(MouseEvent event) {} 
  public void mouseClicked(MouseEvent event) 
  { 
    setVisible(false); 
    dispose(); 
  } 

  // Dismiss the window on a timeout 
  public void actionPerformed(ActionEvent event) 
  { 
    setVisible(false); 
    dispose(); 
  } 
} 

Listing 2.
 
public class JSplashTest 
{ 
  public static void main(String[] args) 
  { 
    JFrame frame = new JFrame("Splash Screen Test"); 
    JSplash splash = new JSplash(frame, "JSplash.gif", 5000); 
    splash.show(); 

    frame.setBounds(100, 100, 400, 400); 
    frame.show(); 

    // Critical - this focus request must be here after 
    // showing the parent to get keystrokes properly. 
    splash.requestFocus(); 
  } 
} 

Listing 3.
 
public class JTips extends JFrame 
  implements ActionListener 
{ 
  protected String filename; 
  protected Properties properties; 
  protected Vector tips; 

  protected JButton next, close; 
  protected JCheckBox show; 
  protected JTextArea text; 

  public JTips(String filename, Properties properties) 
  { 
    super("Tip of the Day"); 
    int w = 425; 
    int h = 280; 
    Dimension screen = 
      Toolkit.getDefaultToolkit().getScreenSize(); 
    int x = (screen.width - w) / 2; 
    int y = (screen.height - h) / 2; 
    setBounds(x, y, w, h); 

    this.filename = filename; 
    this.properties = properties; 

    getContentPane().setLayout(new BorderLayout()); 
    tips = new Vector(); 
    readTipFile(); 

    JPanel iconPanel = new JPanel(); 
    iconPanel.setLayout(new BorderLayout()); 
    iconPanel.setBackground(Color.gray); 
    iconPanel.setPreferredSize(new Dimension(53, 53)); 
    JLabel icon = new JLabel(new ImageIcon("tipsIcon.gif")); 
    icon.setVerticalAlignment(JLabel.CENTER); 
    icon.setHorizontalAlignment(JLabel.CENTER); 
    icon.setPreferredSize(new Dimension(53, 53)); 
    iconPanel.add("North", icon); 

    JPanel titlePanel = new JPanel(); 
    JLabel title = new JLabel("Did you know..."); 
    title.setBorder(new EmptyBorder(10, 10, 0, 0)); 
    title.setFont(new Font("Helvetica", Font.PLAIN, 18)); 
    titlePanel.setLayout(new BorderLayout()); 
    titlePanel.setBorder(new EdgeBorder(EdgeBorder.SOUTH)); 
    titlePanel.setPreferredSize(new Dimension(46, 46)); 
    titlePanel.add("Center", title); 

    text = new TipTextArea(); 
    text.setBackground(getBackground()); 

    JPanel centerPanel = new JPanel(); 
    centerPanel.setLayout(new BorderLayout()); 
    centerPanel.add("North", titlePanel); 
    centerPanel.add("Center", text); 

    JPanel tipsPanel = new JPanel(); 
    tipsPanel.setLayout(new BorderLayout()); 
    tipsPanel.setBorder( 
      new CompoundBorder( 
        new EmptyBorder(10, 10, 0, 10), 
        new BevelBorder(BevelBorder.LOWERED))); 
    tipsPanel.add("Center", centerPanel); 
    tipsPanel.add("West", iconPanel); 

    getContentPane().add("Center", tipsPanel); 

    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setLayout( 
      new FlowLayout(FlowLayout.RIGHT, 10, 10)); 
    buttonPanel.add(next = new JButton("Next Tip")); 
    buttonPanel.add(close = new JButton("Close")); 

    JPanel showPanel = new JPanel(); 
    showPanel.setLayout( 
      new FlowLayout(FlowLayout.LEFT, 10, 10)); 
      showPanel.add(show = 
        new JCheckBox("Show tips at startup", getShow())); 

    JPanel navPanel = new JPanel(); 
    navPanel.setLayout(new BorderLayout()); 
    navPanel.add("East", buttonPanel); 
    navPanel.add("West", showPanel); 

    getContentPane().add("South", navPanel); 

    next.addActionListener(this); 
    close.addActionListener(this); 
    show.addActionListener(this); 

    nextTip(); 
  } 

  private void increment() 
  { 
    int current = getNext() + 1; 
    if (current >= tips.size()) current = 0; 
    setNext(current); 
  } 

  private void nextTip() 
  { 
    text.setText((String)tips.elementAt(getNext())); 
    increment(); 
  } 

  private int getNext() 
  { 
    String prop = properties.getProperty("tips.index"); 
    if (prop == null) 
    { 
      setNext(0); 
      return 0; 
    } 
    int next = Integer.parseInt(prop); 
    return next; 
  } 

  private void setNext(int next) 
  { 
    properties.put("tips.index", "" + next); 
  } 

  public boolean getShow() 
  { 
    String prop = properties.getProperty("tips.show"); 
    if (prop == null) 
    { 
      setShow(true); 
      return true; 
    } 
    return prop.equalsIgnoreCase("true"); 
  } 

  public void setShow(boolean show) 
  { 
    properties.put("tips.show", "" + show); 
  } 

  public void readTipFile() 
  { 
    tips.removeAllElements(); 
    try 
    { 
      FileReader file = new FileReader(filename); 
      BufferedReader input = new BufferedReader(file); 

      String line; 
      while ((line = input.readLine()) != null) 
      { 
        line = replaceParagraphMarkers(line); 
        tips.addElement(line); 
      } 

      input.close(); 
      file.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
      tips.addElement("Tip file '" + filename + "' not 
found!"); 
    } 
    catch (IOException e) 
    { 
      tips.addElement("Error reading '" + filename + "'"); 
    } 
  } 

  public String replaceParagraphMarkers(String line) 
  { 
    StringBuffer buffer = new StringBuffer(line); 
    int pos; 
    while ((pos = line.indexOf("\\p")) > -1) 
    { 
      buffer.setCharAt(pos, '\n'); 
      buffer.setCharAt(pos + 1, '\n'); 
      line = buffer.toString(); 
    } 
    return line; 
  } 

  public void actionPerformed(ActionEvent event) 
  { 
    Object source = event.getSource(); 
    if (source == close) 
 { 
      setVisible(false); 
    } 
    if (source == next) 
    { 
      nextTip(); 
    } 
    if (source == show) 
    { 
      setShow(show.isSelected()); 
    } 
  } 

  public void startup() 
  { 
    if (getShow()) setVisible(true); 
  } 
} 

Listing 4.
 
public class EdgeBorder implements Border, SwingConstants 
{ 
  public static final int RAISED = 1; 
  public static final int LOWERED = 2; 

  protected int edge = NORTH; 
  protected int lift = LOWERED; 

  public EdgeBorder() 
  { 
    this(NORTH); 
  } 

  public EdgeBorder(int edge) 
  { 
    this.edge = edge; 
  } 

  public Insets getBorderInsets(Component component) 
  { 
    switch (edge) 
    { 
      case SOUTH: return new Insets(0, 0, 2, 0); 
      case EAST: return new Insets(0, 2, 0, 0); 
      case WEST: return new Insets(0, 0, 0, 2); 
      default: return new Insets(2, 0, 0, 0); 
    } 
  } 

  public boolean isBorderOpaque() 
  { 
    return true; 
  } 

  public void paintBorder(Component component, 
    Graphics g, int x, int y, int w, int h) 
  { 
    if (lift == RAISED) 
      g.setColor(component.getBackground().brighter()); 
    else 
      g.setColor(component.getBackground().darker()); 
    switch (edge) 
    { 
      case SOUTH: 
        g.drawLine(x, y + h - 2, w, y + h - 2); 
      break; 

      case EAST: 
        g.drawLine(x + w - 2, y, x + w - 2, y + h); 
      break; 

      case WEST: 
        g.drawLine(x + 1, y, x + 1, y + h); 
      break; 

      default: 
        g.drawLine(x, y, x + w, y); 
    } 
    if (lift == RAISED) 
      g.setColor(component.getBackground().darker()); 
    else 
      g.setColor(component.getBackground().brighter()); 
    switch (edge) 
    { 
      case SOUTH: 
        g.drawLine(x, y + h - 1, w, y + h - 1); 
      break; 

      case EAST: 
        g.drawLine(x + w - 1, y, x + w - 1, y + h); 
      break; 

      case WEST: 
        g.drawLine(x + 1, y, x + 1, y + h); 
      break; 

      default: 
        g.drawLine(x, y + 1, x + w, y + 1); 
    } 
  } 
} 

Listing 5.
 
public class TipTextArea extends JTextArea 
{ 
  public TipTextArea() 
  { 
    super(); 
    setFont(new Font("Helvetica", Font.PLAIN, 12)); 
    setBorder(new EmptyBorder(10, 10, 10, 10)); 
    setWrapStyleWord(true); 
    setEditable(false); 
    setLineWrap(true); 
  } 

  public boolean isFocusTraversable() 
  { 
    return false; 
  } 
} 

Listing 6.
 
public class ApplicationTest extends JFrame 
{ 
  public ApplicationTest(String title) 
  { 
    super(title); 
    getContentPane().setLayout(new BorderLayout()); 
    getContentPane().add("Center", new BackgroundPanel()); 
  } 

  public static void main(String[] args) 
  { 
    JFrame frame = new ApplicationTest("Application Test"); 

    JSplash splash = new JSplash(frame, "JSplash.gif", 10000); 
    splash.show(); 

    Dimension dim = 
      Toolkit.getDefaultToolkit().getScreenSize(); 
    frame.setBounds(100, 50, 
      dim.width - 200, dim.height - 150); 
    frame.show(); 

    // Critical - this focus request must be here after 
    // showing the parent to get the focus properly. 
    splash.requestFocus(); 

    JTips tips = new JTips("jtips.tip", new Properties()); 
    splash.block(); 
    tips.startup(); 
  } 
} 

Listing 7.
 
public class BackgroundPanel extends JPanel 
{ 
  public BackgroundPanel() 
  { 
    super(); 
    setOpaque(true); 
  } 

  public void paintComponent(Graphics g) 
  { 
    int w = getSize().width - 1; 
    int h = getSize().height - 1; 
    for (int y = 0; y < h; y += 10) 
    { 
      g.setColor(Color.blue); 
      g.fillRect(0, y, w, y + 5); 
      g.setColor(Color.blue.darker()); 
      g.fillRect(0, y + 5, w, y + 10); 
    } 
  } 
} 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.