Skip to content

#08.4 Progress Bar

Valkryst edited this page Jul 18, 2018 · 11 revisions
import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.builder.ProgressBarBuilder;
import com.valkryst.VTerminal.component.ProgressBar;

import javax.swing.*;
import java.io.IOException;

public class Driver {
    public static void main(final String[] args) throws IOException {
        final Screen screen = new Screen();
        screen.addCanvasToFrame();

        final ProgressBarBuilder progressBarBuilder = new ProgressBarBuilder();
        progressBarBuilder.setPosition(10, 10);
        progressBarBuilder.setWidth(20);

        final ProgressBar progressBar = progressBarBuilder.build();

        // Code to animate the progress bar for this demo.
        final Timer timer = new Timer(1000, e -> {
            int pct = progressBar.getPercentComplete();

            if (pct < 100) {
                pct += 5;
            } else {
                pct = 0;
            }

            progressBar.setPercentComplete(pct);
        });
        timer.start();
        // End animation code.

        screen.addComponent(progressBar);

        screen.draw();
    }
}

Code Explanation

final ProgressBarBuilder progressBarBuilder = new ProgressBarBuilder();

Constructs a new ProgressBarBuilder. You can view the documentation here.

You can reuse the builder, so you won't need to create a new ProgressBarBuilder every time you want to create a new progress bar.


progressBarBuilder.setPosition(10, 10);

This tells the builder to place the bar at position (10x, 10y).


progressBarBuilder.setWidth(20);

This sets the width of the bar to 20 cells.


panel.addComponents(progressBarBuilder.build());

This builds the bar and adds it to the panel.

Result