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
 

"Improving Your Loading Performance"
Vol. 2, Issue 1, p. 50

	

Listing 1: Project Monitor Source Code.

import java.awt.*;
import java.applet.*;
public class showProgress extends Applet
{
 String classes[] =
 {
  "Main.DialogFrame",
  "Main.ScorePanel",
  "Main.HelpPanel",
  "Main.NetworkConnection",
  "Main.MediaHandler",
  "Main.UpdateThread",
  "Main.mainApplet"
 };
 Panel updateBar;
 private int loadedCount;
 private static final int offLeft = 10;
 private static final int offTop = 10;
 private static final int offBottom = 5;
 private static final int height = 30;
 public void init()
 {
  super.init();
  loadedCount = 0;
  show();
  updateBar = new Panel();
  updateBar.setFont(new Font("Helvetica",Font.PLAIN, 14));

  add(updateBar);
  updateBar.show();
  repaint();
  doLoad();
  updateBar.hide();

  Applet n = new Main.mainApplet();
  n.init();
  n.start();
 }
 public void paint (Graphics g1)
 {
  int barLen =
	updateBar.size().width * loadedCount / classes.length;
  updateBar.reshape(offLeft,
		    offTop,size().width - 2*offLeft,
		    height);
  Graphics g = updateBar.getGraphics();
  FontMetrics fm       = g.getFontMetrics(g.getFont());
  g.setColor (Color.gray);
  g.fillRect (0, 0,
	      updateBar.size().width,
	      updateBar.size().height);
  g.setColor (Color.blue);
  g.fill3DRect (0, 0,
		 barLen,
		 updateBar.size().height, true);
  int integerPercentage = (loadedCount * 100 / classes.length);
  String percentString = "" + integerPercentage + "%";
  g.setColor (Color.black);
  int PixelWidth  = fm.stringWidth(percentString);
  int PixelHeight = fm.getHeight();
  g.drawString(percentString,
	      (updateBar.size().width - PixelWidth)/2,
	      height - 2*offBottom);
  if (loadedCount > 0)
  {
   g.drawString("Loaded " + classes[loadedCount-1],
		 offLeft, height - offBottom);
  }
 }
 private void doLoad()
 {
  int i;
  for (i = 0; i < classes.length; ++ i)
  {
    try
    {
      Class c = Class.forName(classes[i]);
      loadedCount++;
      repaint();
    }
    catch (Exception e) { }
  }
 }
}

 

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.