Tuesday, July 22, 2025

Java Example Boot

# Creating a Standalone Java Boot Application (Without JAR)

Here's how to create a Java Swing application that simulates a boot process and can be run directly without needing a JAR file:

## Complete Implementation

```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class VirtualBoot {
    public static void main(String[] args) {
        // Create and show the boot screen
        SwingUtilities.invokeLater(() -> {
            BootScreen bootScreen = new BootScreen();
            bootScreen.setVisible(true);
            bootScreen.startBooting();
        });
    }
}

class BootScreen extends JFrame {
    private JLabel bootLabel;
    private JProgressBar progressBar;
    private Timer timer;
    private int progressValue = 0;
    private String[] bootMessages = {
        "Initializing virtual hardware...",
        "Loading virtual BIOS...",
        "Checking virtual memory...",
        "Detecting virtual storage...",
        "Loading virtual OS kernel...",
        "Starting virtual services...",
        "Preparing virtual environment...",
        "Virtual boot complete!"
    };
    private int currentMessageIndex = 0;

    public BootScreen() {
        // Configure the window
        setTitle("Virtual Machine Boot");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setUndecorated(true); // Remove window decorations
        setExtendedState(JFrame.MAXIMIZED_BOTH); // Full screen
        
        // Set black background
        getContentPane().setBackground(Color.BLACK);
        setLayout(new BorderLayout());
        
        // Boot label with green text (classic terminal style)
        bootLabel = new JLabel("", SwingConstants.CENTER);
        bootLabel.setForeground(new Color(0, 255, 0)); // Green text
        bootLabel.setFont(new Font("Courier New", Font.BOLD, 20));
        add(bootLabel, BorderLayout.CENTER);
        
        // Progress bar at bottom
        progressBar = new JProgressBar(0, 100);
        progressBar.setStringPainted(true);
        progressBar.setForeground(new Color(0, 255, 0)); // Green
        progressBar.setBackground(Color.BLACK);
        progressBar.setFont(new Font("Courier New", Font.PLAIN, 12));
        add(progressBar, BorderLayout.SOUTH);
        
        // Create timer for boot animation (1.5 second intervals)
        timer = new Timer(1500, e -> updateBootProgress());
    }
    
    private void updateBootProgress() {
        // Update boot message
        if (currentMessageIndex < bootMessages.length) {
            bootLabel.setText(bootMessages[currentMessageIndex]);
            currentMessageIndex++;
            
            // Play beep sound for certain steps
            if (currentMessageIndex == 1 || currentMessageIndex == 4) {
                Toolkit.getDefaultToolkit().beep();
            }
        }
        
        // Update progress (with some randomness)
        progressValue += 8 + (int)(Math.random() * 8);
        if (progressValue > 100) progressValue = 100;
        progressBar.setValue(progressValue);
        progressBar.setString("BOOT PROGRESS: " + progressValue + "%");
        
        // When boot completes
        if (progressValue >= 100) {
            timer.stop();
            
            // Show completion for 2 seconds then launch virtual desktop
            new Timer(2000, e -> {
                dispose();
                launchVirtualDesktop();
            }).start();
        }
    }
    
    private void launchVirtualDesktop() {
        VirtualDesktop desktop = new VirtualDesktop();
        desktop.setVisible(true);
    }
    
    public void startBooting() {
        timer.start();
    }
}

class VirtualDesktop extends JFrame {
    public VirtualDesktop() {
        setTitle("Virtual Desktop Environment");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // Create a simple desktop UI
        JPanel desktop = new JPanel(new BorderLayout());
        desktop.setBackground(new Color(0, 128, 128)); // Teal background
        
        // Menu bar
        JMenuBar menuBar = new JMenuBar();
        JMenu systemMenu = new JMenu("System");
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(e -> System.exit(0));
        systemMenu.add(exitItem);
        menuBar.add(systemMenu);
        setJMenuBar(menuBar);
        
        // Desktop content
        JLabel welcomeLabel = new JLabel("Virtual Machine Ready", SwingConstants.CENTER);
        welcomeLabel.setFont(new Font("Arial", Font.BOLD, 24));
        welcomeLabel.setForeground(Color.WHITE);
        desktop.add(welcomeLabel, BorderLayout.CENTER);
        
        // Status bar
        JPanel statusBar = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        statusBar.add(new JLabel("Virtual CPU: 2 cores | Memory: 4GB | Disk: 50GB"));
        desktop.add(statusBar, BorderLayout.SOUTH);
        
        add(desktop);
    }
}
```

## How to Run Without JAR

1. **Save the code** as `VirtualBoot.java`
2. **Compile** with:
   ```
   javac VirtualBoot.java
   ```
3. **Run** with:
   ```
   java VirtualBoot
   ```

## Key Features

1. **Realistic Boot Sequence**:
   - Simulated hardware initialization
   - Progress percentage with random increments
   - Classic terminal-style green text on black background

2. **Full Boot Process**:
   - BIOS-like startup screen
   - Progress bar showing boot status
   - Automatic transition to virtual desktop

3. **Virtual Desktop**:
   - Simple menu system
   - System information display
   - Clean teal-colored interface

4. **No JAR Required**:
   - Runs directly from compiled class files
   - No need for packaging or special execution

## Customization Options

1. **Change Boot Messages**:
   ```java
   private String[] bootMessages = {
       "Your custom message 1",
       "Your custom message 2",
       // etc...
   };
   ```

2. **Add BIOS Logo**:
   ```java
   // Add to BootScreen constructor
   JLabel logoLabel = new JLabel("VIRTUAL BIOS v1.0", SwingConstants.CENTER);
   logoLabel.setForeground(Color.CYAN);
   logoLabel.setFont(new Font("Courier New", Font.BOLD, 28));
   add(logoLabel, BorderLayout.NORTH);
   ```

3. **Add Sound Effects**:
   ```java
   // Play beep at specific points in updateBootProgress()
   if (currentMessageIndex == 2) {
       Toolkit.getDefaultToolkit().beep();
   }
   ```

4. **Make Boot Faster/Slower**:
   ```java
   // Change the timer interval (milliseconds)
   timer = new Timer(1000, e -> updateBootProgress()); // 1 second intervals
   ```

This implementation provides a complete virtual boot experience that runs as a standard Java application without requiring JAR packaging.

No comments: