home *** CD-ROM | disk | FTP | other *** search
/ Magazyn WWW 2000 June / www-06-2000.iso / java / LoadingScreen.java < prev    next >
Text File  |  2000-03-15  |  4KB  |  116 lines

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4.  
  5. public class LoadingScreen extends Component {
  6.  
  7.     private Image image;
  8.     private Dimension lastSize;
  9.  
  10.     private Rectangle loadingTextArea;
  11.     private Rectangle progressBarArea;
  12.     private Rectangle pleaseWaitTextArea;
  13.  
  14.     private Font loadingTextFont;
  15.     private Font pleaseWaitTextFont;
  16.  
  17.     private String loadingText="Loading";
  18.     private String pleaseWaitText="please wait";
  19.  
  20.     private int progressValue;
  21.  
  22.     public void paint(Graphics gdc) {
  23.         Dimension size=getSize();
  24.         if (!size.equals(lastSize)) {
  25.             if (image!=null) {
  26.                 image.flush();
  27.                 image=null;
  28.             }
  29.             lastSize=size;
  30.             updateSize();
  31.         }
  32.         if (image==null) {
  33.             image=createImage(new BackgroundImage(size.width, size.height));
  34.         }
  35.         gdc.drawImage(image, 0, 0, this);
  36.         drawLoadingText(gdc);
  37.         drawPleaseWaitText(gdc);
  38.         drawProgressBar(gdc);
  39.     }
  40.  
  41.     private void updateSize() {
  42.         loadingTextArea=new Rectangle((lastSize.width-3*lastSize.width/10)/2, 35*lastSize.height/100, 3*lastSize.width/10, lastSize.height/10);
  43.         progressBarArea=new Rectangle((lastSize.width-4*lastSize.width/10)/2, 49*lastSize.height/100, 4*lastSize.width/10, 5*lastSize.height/100);
  44.         pleaseWaitTextArea=new Rectangle((lastSize.width-3*lastSize.width/10)/2, 58*lastSize.height/100, 3*lastSize.width/10, 7*lastSize.height/100);
  45.  
  46.         int[] fs={ 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
  47.  
  48.         loadingTextFont=new Font("Serif", Font.BOLD, fs[0]);
  49.         for(int i=1; i<fs.length; i++) {
  50.             Font font=new Font("Serif", Font.BOLD, fs[i]);
  51.             FontMetrics fm=getToolkit().getFontMetrics(font);
  52.             int w=fm.stringWidth(loadingText);
  53.             int h=fm.getMaxAscent()+fm.getMaxDescent();
  54.             if ((w<=loadingTextArea.width) && (h<=loadingTextArea.height)) {
  55.                 loadingTextFont=font;
  56.             } else {
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         pleaseWaitTextFont=new Font("Serif", Font.BOLD, fs[0]);
  62.         for(int i=1; i<fs.length; i++) {
  63.             Font font=new Font("Serif", Font.BOLD, fs[i]);
  64.             FontMetrics fm=getToolkit().getFontMetrics(font);
  65.             int w=fm.stringWidth(pleaseWaitText);
  66.             int h=fm.getMaxAscent()+fm.getMaxDescent();
  67.             if ((w<=pleaseWaitTextArea.width) && (h<=pleaseWaitTextArea.height)) {
  68.                 pleaseWaitTextFont=font;
  69.             } else {
  70.                 break;
  71.             }
  72.         }
  73.  
  74.     }
  75.     
  76.     private void drawLoadingText(Graphics gdc) {
  77.         gdc.setColor(Color.white);
  78.         gdc.setFont(loadingTextFont);
  79.         FontMetrics fm=getToolkit().getFontMetrics(loadingTextFont);
  80.         int w=fm.stringWidth(loadingText);
  81.         int h=fm.getMaxAscent()+fm.getMaxDescent();
  82.         int x=(loadingTextArea.width-w)/2+loadingTextArea.x;
  83.         int y=(loadingTextArea.height-h)/2+loadingTextArea.y+fm.getMaxAscent();
  84.         gdc.drawString(loadingText, x, y);
  85.     }
  86.  
  87.     private void drawPleaseWaitText(Graphics gdc) {
  88.         gdc.setColor(Color.white);
  89.         gdc.setFont(pleaseWaitTextFont);
  90.         FontMetrics fm=getToolkit().getFontMetrics(pleaseWaitTextFont);
  91.         int w=fm.stringWidth(pleaseWaitText);
  92.         int h=fm.getMaxAscent()+fm.getMaxDescent();
  93.         int x=(pleaseWaitTextArea.width-w)/2+pleaseWaitTextArea.x;
  94.         int y=(pleaseWaitTextArea.height-h)/2+pleaseWaitTextArea.y+fm.getMaxAscent();
  95.         gdc.drawString(pleaseWaitText, x, y);
  96.     }
  97.  
  98.     private void drawProgressBar(Graphics gdc) {
  99.         gdc.setColor(Color.yellow);
  100.         gdc.drawRect(progressBarArea.x, progressBarArea.y, progressBarArea.width, progressBarArea.height);
  101.         gdc.fillRect(progressBarArea.x, progressBarArea.y, progressBarArea.width*progressValue/100, progressBarArea.height);
  102.     }
  103.  
  104.     public int getProgressValue() {
  105.         return progressValue;
  106.     }
  107.  
  108.     public void setProgressValue(int value) {
  109.         progressValue=value;
  110.         Rectangle r=progressBarArea;
  111.         if (r!=null) {
  112.             repaint(r.x, r.y, r.width, r.height);
  113.         }
  114.     }
  115. }
  116.