📄 splashscreen.java
字号:
/*
* 11/14/2003
*
* SplashScreen.java - A "splash screen" to display while your Java program
* is loading.
* Copyright (C) 2003 Robert Futrell
* email@address.com
* www.website.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.fife.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.net.URL;
import java.net.URLClassLoader;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
/**
* A window to display while your Swing application is loading; it behaves just
* like a "splash screen."
*
* @author Robert Futrell
* @version 0.6
*/
public class SplashScreen extends JWindow {
/**
*
*/
private static final long serialVersionUID = 4219003390109261647L;
private ProgressBar progressBar;
private static final int SMALL_DELAY_MS = 10;
private static final int statusBarHeight = 25;
/*****************************************************************************/
/**
* Creates a new <code>SplashScreen</code> with default color scheme.
*
* @param splashScreenPath Path to an image file that a
* <code>URLClassLoader</code> will understand.
* @param statusText The status text to initially display.
*/
public SplashScreen(String splashScreenPath, String statusText) {
this(splashScreenPath, statusText, new Color(190,190,190),
new Color(44,70,154), new Color(75,141,199), Color.WHITE,
false);
}
/*****************************************************************************/
/**
* Creates a new <code>SplashScreen</code>.
*
* @param splashScreenPath Path to an image file that a
* <code>URLClassLoader</code> will understand.
* @param statusText The status text to initially display.
* @param statusBackground The color to use as the status bar's background.
* @param foreground1 The color to use for the first part of the
* gradient fill of the status bar.
* @param foreground2 The color to use for the second part of the
* gradient fill of the status bar.
* @param textColor The color to use for the status text.
* @param smallDelay If <code>true</code>, then the status bar will appear
* to continuously "fill up," even if your <code>updateStatus</code>
* calls are discrete. For example, if you set the percent complete
* to 40, then to 60, the status bar will appear to continuously
* increment from 40 to 60 instead of making the jump
* instantaneously. A 10ms delay is placed between each 1%
* increment.
*/
public SplashScreen(String splashScreenPath, String statusText,
Color statusBackground, Color foreground1,
Color foreground2, Color textColor,
boolean smallDelay) {
JPanel contentPane = new JPanel(new BorderLayout());
// Get the splash screen image.
URLClassLoader cl = (URLClassLoader)this.getClass().getClassLoader();
URL imageURL = cl.findResource(splashScreenPath);
ImageIcon splashScreen = new ImageIcon(imageURL);
// Create a panel for the splash screen image.
JLabel imageLabel = new JLabel(splashScreen);
JPanel splashPanel = new JPanel(new java.awt.GridLayout(1,1));
splashPanel.add(imageLabel);
contentPane.add(splashPanel);
// Create the "progress bar" at the bottom.
progressBar = new ProgressBar(splashScreen.getIconWidth(),
statusText, statusBackground,
foreground1, foreground2, textColor,
smallDelay);
contentPane.add(progressBar, BorderLayout.SOUTH);
// Combine everything and get ready to go.
setContentPane(contentPane);
this.pack();
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
this.setLocationRelativeTo(null); // Center on the screen.
}
/*****************************************************************************/
/**
* Updates the percent complete bar and the associated status text.
*
* @param statusText The new status text to display.
* @param percentComplete The new percentage to have filled in the percent
* complete bar.
*/
public void updateStatus(String statusText, int percentComplete) {
progressBar.update(statusText, percentComplete);
}
/*****************************************************************************/
/**
* The "progress bar" part of the splash screen. This component is
* somewhat configurable; you can:
* <ul>
* <li>Configure the background color</li>
* <li>Configure the progress "fill in" color, even make it a
* gradient</li>
* <li>Set the status text and its color</li>
* </ul>
*/
private class ProgressBar extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8877618550785815148L;
private Dimension preferredSize;
private Color textColor;
private GradientPaint paint;
private int percentComplete;
private String text;
private int textX, textY;
private boolean smallDelay;
ProgressBar(int width, String initialText, Color background,
Color foreground, Color textColor) {
this(width, initialText, background, foreground, foreground,
textColor, false);
}
ProgressBar(int width, String initialText, Color background,
Color foreground1, Color foreground2,
Color textColor, boolean smallDelay) {
super();
setBackground(background);
preferredSize = new Dimension(width, statusBarHeight);
paint = new GradientPaint(0.0f,0.0f, foreground1,
0.0f,statusBarHeight, foreground2);
this.textColor = textColor;
this.smallDelay = smallDelay;
update(text, percentComplete);
}
public Dimension getPreferredSize() {
return preferredSize;
}
public void paintComponent(Graphics g) {
// Fill in background.
super.paintComponent(g);
// Paint the filled-in portion of the status bar.
int width = getWidth();
Graphics2D g2d = (Graphics2D)g;
Paint oldPaint = g2d.getPaint();
g2d.setPaint(paint);
int filledWidth = width*percentComplete/100;
g2d.fillRect(0,0, filledWidth,getHeight());
g2d.setPaint(oldPaint);
// Paint the status text.
if (text!=null) {
g2d.setColor(textColor);
g2d.drawString(text, textX,textY);
}
}
void update(String text, int percentComplete) {
this.text = text;
if (text!=null) {
FontMetrics fm = getFontMetrics(getFont());
if (fm!=null) {
int stringLength = fm.charsWidth(
text.toCharArray(), 0,text.length());
textX = (getWidth()-stringLength)/2;
textY = (statusBarHeight + fm.getAscent())/2;
}
}
if (smallDelay) {
while (this.percentComplete < percentComplete) {
this.percentComplete++;
paintImmediately(getBounds());
try {
Thread.sleep(SMALL_DELAY_MS);
} catch (Exception e) {
e.printStackTrace();
}
}
}
else {
this.percentComplete = percentComplete;
repaint();
}
}
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -