# Java Swing Splash Screen with Progress Bar and Time Limit
Here's a complete implementation of a splash screen with a progress bar that loads within a specified time limit:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TimedSplashScreen extends JWindow {
private JProgressBar progressBar;
private Timer progressTimer;
private Timer countdownTimer;
private int timeLimitSeconds = 5; // 5 seconds time limit
private int progress = 0;
private JLabel timeLabel;
public TimedSplashScreen(int displayTimeSeconds) {
this.timeLimitSeconds = displayTimeSeconds;
// Set up splash screen
setSize(400, 300);
setLocationRelativeTo(null); // Center on screen
// Create content panel with gradient background
JPanel content = new JPanel(new BorderLayout()) {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
GradientPaint gradient = new GradientPaint(
0, 0, new Color(70, 130, 180),
getWidth(), getHeight(), new Color(135, 206, 250));
g2d.setPaint(gradient);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
};
content.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 2));
// Add application logo/text
JLabel logo = new JLabel("My Application", JLabel.CENTER);
logo.setFont(new Font("SansSerif", Font.BOLD, 24));
logo.setForeground(Color.WHITE);
content.add(logo, BorderLayout.CENTER);
// Create progress bar
progressBar = new JProgressBar(0, 100);
progressBar.setStringPainted(true);
progressBar.setForeground(new Color(34, 139, 34));
progressBar.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
// Create time remaining label
timeLabel = new JLabel("Loading...", JLabel.CENTER);
timeLabel.setForeground(Color.WHITE);
timeLabel.setFont(new Font("SansSerif", Font.PLAIN, 12));
// Add components to panel
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.setOpaque(false);
bottomPanel.add(progressBar, BorderLayout.CENTER);
bottomPanel.add(timeLabel, BorderLayout.SOUTH);
content.add(bottomPanel, BorderLayout.SOUTH);
setContentPane(content);
// Set up progress timer
progressTimer = new Timer(50, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
progress++;
progressBar.setValue(progress);
progressBar.setString("Loading " + progress + "%");
if (progress >= 100) {
progressTimer.stop();
dispose();
showMainApplication();
}
}
});
// Set up countdown timer
countdownTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timeLimitSeconds--;
timeLabel.setText("Time remaining: " + timeLimitSeconds + " seconds");
if (timeLimitSeconds <= 0) {
countdownTimer.stop();
progressTimer.stop();
dispose();
showMainApplication();
}
}
});
}
public void showSplash() {
setVisible(true);
progressTimer.start();
countdownTimer.start();
}
private void showMainApplication() {
// Create and show your main application window here
JFrame mainFrame = new JFrame("Main Application");
mainFrame.setSize(800, 600);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
// Add your actual application content here
JLabel welcomeLabel = new JLabel("Welcome to the Application!", JLabel.CENTER);
welcomeLabel.setFont(new Font("SansSerif", Font.PLAIN, 24));
mainFrame.add(welcomeLabel);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Show splash screen for 5 seconds
TimedSplashScreen splash = new TimedSplashScreen(5);
splash.showSplash();
}
});
}
}
```
## Key Features:
1. **Time-Limited Display**:
- The splash screen automatically closes after the specified time (5 seconds by default)
- Countdown timer shows remaining time
2. **Animated Progress Bar**:
- Smooth progress animation from 0% to 100%
- Percentage display on the progress bar
- Custom color scheme
3. **Visual Design**:
- Gradient background
- Centered application title/logo
- Clean border around the splash screen
4. **Automatic Transition**:
- When time expires or progress completes, the main application launches
- Proper disposal of splash screen resources
## Customization Options:
1. **Change Display Time**:
```java
// In main method:
TimedSplashScreen splash = new TimedSplashScreen(10); // 10 seconds
```
2. **Add Your Logo**:
```java
// Replace the JLabel with an ImageIcon:
ImageIcon logoIcon = new ImageIcon("path/to/logo.png");
JLabel logo = new JLabel(logoIcon, JLabel.CENTER);
```
3. **Change Colors**:
```java
// Modify the GradientPaint colors:
new GradientPaint(0, 0, new Color(31, 58, 147),
getWidth(), getHeight(), new Color(69, 162, 152));
```
4. **Add Loading Messages**:
```java
// Add to progress timer action listener:
String[] messages = {"Loading modules...", "Initializing data...", "Almost done..."};
if (progress % 25 == 0) {
timeLabel.setText(messages[progress/25]);
}
```
5. **Make Progress Match Actual Loading**:
Replace the simulated progress with actual loading tasks:
```java
// Instead of the timer, update progress during actual loading:
public void updateProgress(int value) {
progressBar.setValue(value);
progressBar.setString("Loading " + value + "%");
}
```
This implementation provides a professional-looking splash screen that can be easily customized to match your application's branding while enforcing a time limit for the display.
No comments:
Post a Comment