home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magazyn WWW 2000 June
/
www-06-2000.iso
/
java
/
LoadingScreen.java
< prev
next >
Wrap
Text File
|
2000-03-15
|
4KB
|
116 lines
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class LoadingScreen extends Component {
private Image image;
private Dimension lastSize;
private Rectangle loadingTextArea;
private Rectangle progressBarArea;
private Rectangle pleaseWaitTextArea;
private Font loadingTextFont;
private Font pleaseWaitTextFont;
private String loadingText="Loading";
private String pleaseWaitText="please wait";
private int progressValue;
public void paint(Graphics gdc) {
Dimension size=getSize();
if (!size.equals(lastSize)) {
if (image!=null) {
image.flush();
image=null;
}
lastSize=size;
updateSize();
}
if (image==null) {
image=createImage(new BackgroundImage(size.width, size.height));
}
gdc.drawImage(image, 0, 0, this);
drawLoadingText(gdc);
drawPleaseWaitText(gdc);
drawProgressBar(gdc);
}
private void updateSize() {
loadingTextArea=new Rectangle((lastSize.width-3*lastSize.width/10)/2, 35*lastSize.height/100, 3*lastSize.width/10, lastSize.height/10);
progressBarArea=new Rectangle((lastSize.width-4*lastSize.width/10)/2, 49*lastSize.height/100, 4*lastSize.width/10, 5*lastSize.height/100);
pleaseWaitTextArea=new Rectangle((lastSize.width-3*lastSize.width/10)/2, 58*lastSize.height/100, 3*lastSize.width/10, 7*lastSize.height/100);
int[] fs={ 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
loadingTextFont=new Font("Serif", Font.BOLD, fs[0]);
for(int i=1; i<fs.length; i++) {
Font font=new Font("Serif", Font.BOLD, fs[i]);
FontMetrics fm=getToolkit().getFontMetrics(font);
int w=fm.stringWidth(loadingText);
int h=fm.getMaxAscent()+fm.getMaxDescent();
if ((w<=loadingTextArea.width) && (h<=loadingTextArea.height)) {
loadingTextFont=font;
} else {
break;
}
}
pleaseWaitTextFont=new Font("Serif", Font.BOLD, fs[0]);
for(int i=1; i<fs.length; i++) {
Font font=new Font("Serif", Font.BOLD, fs[i]);
FontMetrics fm=getToolkit().getFontMetrics(font);
int w=fm.stringWidth(pleaseWaitText);
int h=fm.getMaxAscent()+fm.getMaxDescent();
if ((w<=pleaseWaitTextArea.width) && (h<=pleaseWaitTextArea.height)) {
pleaseWaitTextFont=font;
} else {
break;
}
}
}
private void drawLoadingText(Graphics gdc) {
gdc.setColor(Color.white);
gdc.setFont(loadingTextFont);
FontMetrics fm=getToolkit().getFontMetrics(loadingTextFont);
int w=fm.stringWidth(loadingText);
int h=fm.getMaxAscent()+fm.getMaxDescent();
int x=(loadingTextArea.width-w)/2+loadingTextArea.x;
int y=(loadingTextArea.height-h)/2+loadingTextArea.y+fm.getMaxAscent();
gdc.drawString(loadingText, x, y);
}
private void drawPleaseWaitText(Graphics gdc) {
gdc.setColor(Color.white);
gdc.setFont(pleaseWaitTextFont);
FontMetrics fm=getToolkit().getFontMetrics(pleaseWaitTextFont);
int w=fm.stringWidth(pleaseWaitText);
int h=fm.getMaxAscent()+fm.getMaxDescent();
int x=(pleaseWaitTextArea.width-w)/2+pleaseWaitTextArea.x;
int y=(pleaseWaitTextArea.height-h)/2+pleaseWaitTextArea.y+fm.getMaxAscent();
gdc.drawString(pleaseWaitText, x, y);
}
private void drawProgressBar(Graphics gdc) {
gdc.setColor(Color.yellow);
gdc.drawRect(progressBarArea.x, progressBarArea.y, progressBarArea.width, progressBarArea.height);
gdc.fillRect(progressBarArea.x, progressBarArea.y, progressBarArea.width*progressValue/100, progressBarArea.height);
}
public int getProgressValue() {
return progressValue;
}
public void setProgressValue(int value) {
progressValue=value;
Rectangle r=progressBarArea;
if (r!=null) {
repaint(r.x, r.y, r.width, r.height);
}
}
}