# (DD02) Frame — Class Detailed Design [553 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `algo_Calc.Frame` |
| Layer | Controller |
| Module | `algo_Calc` |

## Class Overview

`Frame` is a Swing-based launcher window for the sorting visualizer application. It builds a small menu frame with one button per sorting algorithm and delegates the actual visualization work to `Sortingss.vis(int, String)`. The class does not implement sorting itself; instead, it acts as the entry screen and navigation controller for algorithm demos.

---

## Method: `main(String[] args)`

### 1. Role
This is the application entry point. It starts the GUI on the AWT event dispatch thread, which is the standard Swing startup pattern, and prints any startup exception stack trace if initialization fails.

### 2. Processing Pattern
```mermaid
flowchart TD
A["main(String[] args)"] --> B["EventQueue.invokeLater"]
B --> C["Create Frame"]
C --> D["initialize()"]
D --> E["frame.setVisible(true)"]
C --> F["Exception caught"]
F --> G["printStackTrace()"]
```

### 3. Parameter Analysis

| Parameter | Type | Meaning | Usage |
|-----------|------|---------|-------|
| `args` | `String[]` | Command-line arguments passed by the JVM | Not used directly |

### 4. CRUD Operations / Called Services

| Category | Target | Purpose |
|----------|--------|---------|
| GUI startup | `EventQueue.invokeLater(...)` | Defers window creation to the Swing event thread |
| Object creation | `new Frame()` | Instantiates the main window controller |
| UI display | `window.frame.setVisible(true)` | Shows the frame after construction |
| Error handling | `Exception#printStackTrace()` | Emits startup failures to stderr |

### 5. Dependency Trace

| Dependency | Type | Evidence / Effect |
|------------|------|-------------------|
| `java.awt.EventQueue` | JDK API | Used to schedule GUI creation safely |
| `Frame` constructor | Internal | Creates the UI instance |
| `frame` field | Internal state | Made visible after construction |

### 6. Per-Branch Detail Blocks

- **Normal startup path**: the code posts a `Runnable` to `EventQueue.invokeLater`, creates a `Frame`, and then shows the internal `JFrame` by calling `setVisible(true)`.
- **Failure path**: if any exception escapes construction or visibility setup, the catch block handles it locally and prints the stack trace. No retry or fallback UI exists.
- **Design note**: the method is intentionally thin and delegates all UI assembly to the constructor and `initialize()`.

---

## Method: `Frame()`

### 1. Role
This is the constructor for the launcher window. Its only responsibility is to initialize the frame contents immediately after object creation.

### 2. Processing Pattern
```mermaid
flowchart TD
A["Frame()"] --> B["initialize()"]
```

### 3. Parameter Analysis

| Parameter | Type | Meaning | Usage |
|-----------|------|---------|-------|
| None | - | Default constructor | No parameters are accepted |

### 4. CRUD Operations / Called Services

| Category | Target | Purpose |
|----------|--------|---------|
| Initialization | `initialize()` | Builds the Swing UI and registers button handlers |

### 5. Dependency Trace

| Dependency | Type | Evidence / Effect |
|------------|------|-------------------|
| `initialize()` | Internal method | Primary constructor side effect |
| `Frame` instances | Internal object lifecycle | Cannot be used before UI initialization completes |

### 6. Per-Branch Detail Blocks

- **Construction path**: there is only one path. The constructor invokes `initialize()` immediately and does not branch.
- **Implication**: any initialization failure occurs during object creation, which is why the `main()` method wraps instantiation in a `try/catch`.

---

## Method: `initialize()`

### 1. Role
This method builds the complete menu window for the application. It creates the main `JFrame`, configures its size and layout, and wires each sorting button to launch a corresponding visualization mode through a shared `Sortingss` instance.

### 2. Processing Pattern
```mermaid
flowchart TD
A["initialize()"] --> B["Create Sortingss obj"]
B --> C["Create JFrame"]
C --> D["Set bounds 100,100,456,324"]
D --> E["Set EXIT_ON_CLOSE"]
E --> F["Use null layout"]
F --> G["Add Bubble Sort button"]
F --> H["Add Insertion Sort button"]
F --> I["Add Selection Sort button"]
F --> J["Add Merge Sort button"]
F --> K["Add Quick Sort button"]
F --> L["Add Max Heap Sort button"]
G --> M["obj.vis(1, BUBBLE SORT)"]
H --> N["obj.vis(4, INSERTION SORT)"]
I --> O["obj.vis(2, SELECTION SORT)"]
J --> P["obj.vis(5, MERGE SORT)"]
K --> Q["obj.vis(3, QUICK SORT)"]
L --> R["obj.vis(6, HEAP SORT)"]
M --> S["Hide frame"]
N --> S
O --> S
P --> S
Q --> S
R --> S
```

### 3. Parameter Analysis

| Parameter | Type | Meaning | Usage |
|-----------|------|---------|-------|
| None | - | No inputs are required | The frame is fully self-configuring |

### 4. CRUD Operations / Called Services

| Category | Target | Purpose |
|----------|--------|---------|
| Object creation | `new Sortingss()` | Creates the visualizer service used by all handlers |
| Object creation | `new JFrame()` | Builds the top-level window |
| Configuration | `setBounds(100, 100, 456, 324)` | Sets window position and size |
| Configuration | `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` | Ensures the app exits when the window closes |
| Configuration | `getContentPane().setLayout(null)` | Enables absolute positioning |
| UI assembly | `new JButton(...)` | Creates six navigation buttons |
| Event hookup | `addActionListener(...)` | Registers click behavior for each button |
| Service call | `obj.vis(mode, label)` | Launches the selected sorting visualization |
| UI state change | `frame.setVisible(false)` | Hides the menu after a selection is made |

### 5. Dependency Trace

| Dependency | Type | Evidence / Effect |
|------------|------|-------------------|
| `Sortingss` | Internal collaborator | Receives all sort-launch requests |
| `Sortingss.vis(int, String)` | External method call | Mode and display label are passed for each algorithm |
| `JFrame` | Swing container | Hosts the menu UI |
| `JButton` | Swing control | Exposes user choices |
| `ActionListener` / `ActionEvent` | Swing event API | Handles button clicks |
| Hard-coded mode values `1..6` | Control constants | Map buttons to algorithm implementations |
| Hard-coded labels | UI constants | Used as display names for visualization mode |

### 6. Per-Branch Detail Blocks

- **Shared setup path**: the method first creates one `Sortingss` object and one `JFrame`, then applies common frame configuration before creating buttons.
- **Bubble Sort branch**: clicking the button labeled `Bubble Sort` calls `obj.vis(1, "BUBBLE SORT")`, then hides the launcher window.
- **Insertion Sort branch**: clicking `Insertion Sort` calls `obj.vis(4, "INSERTION SORT")`, then hides the launcher window.
- **Selection Sort branch**: clicking `Selection Sort` calls `obj.vis(2, "SELECTION SORT")`, then hides the launcher window.
- **Merge Sort branch**: clicking `Merge Sort` calls `obj.vis(5, "MERGE SORT")`, then hides the launcher window.
- **Quick Sort branch**: clicking `Quick Sort` calls `obj.vis(3, "QUICK SORT")`, then hides the launcher window.
- **Max Heap Sort branch**: clicking `Max Heap Sort` calls `obj.vis(6, "HEAP SORT")`, then hides the launcher window. The caption differs from the visualization label: the button says `Max Heap Sort`, but the label passed to `vis` is `HEAP SORT`.
- **Behavioral pattern**: every button handler is structurally identical apart from the mode number and label string. The class therefore acts as a menu router rather than a computation engine.
- **Layout note**: all component positions are hard-coded with `setBounds`, so the window depends on absolute positioning and will not adapt automatically to resize events.
- **State note**: the same `Sortingss` instance is reused across all buttons, which centralizes navigation into a single collaborator object.
