Jump to content

Progress Bar


Recommended Posts

Posted (edited)

This will paint a progress bar going left-right or right-left. It has one class and two callable methods: drawProgressBar() || drawReversedProgressBar()

 

Example Usage In onPaint() with the Graphics2D named g:

Spoiler

Defining The Colors To Use:

Spoiler


ProgressBar.drawProgressBar(15, 260, 160, 8, 8, 8, getConfigs().get(243), maxValue, Color.black, Color.cyan, g);

ProgressBar.drawReversedProgressBar(15, 230, 160, 8, 8, 8, currentTime, maxTime, Color.black, Color.cyan, g);

Not Defining Colors:
Spoiler


ProgressBar.drawProgressBar(15, 260, 160, 8, 8, 8, getConfigs().get(243), maxValue, g);

ProgressBar.drawReversedProgressBar(15, 230, 160, 8, 8, 8, currentTime, maxTime, g);

Code:

Spoiler

import java.awt.*;

public abstract class ProgressBar {

    public static void drawProgressBar(int xPos, int yPos, int width, int height, int arcWidth, int arcHeight, float currentValue, float maxValue, Color borderColor, Color fillColor, Graphics2D g) {
        filledProgressBar(currentValue, maxValue, width, xPos, yPos, height, arcWidth, arcHeight, fillColor, g);
        progressBar(xPos, yPos, width, height, arcWidth, arcHeight, borderColor, g);
    }

    public static void drawProgressBar(int xPos, int yPos, int width, int height, int arcWidth, int arcHeight, float currentValue, float maxValue, Graphics2D g) {
        filledProgressBar(currentValue, maxValue, width, xPos, yPos, height, arcWidth, arcHeight, Color.cyan, g);
        progressBar(xPos, yPos, width, height, arcWidth, arcHeight, Color.black, g);
    }

    public static void drawReversedProgressBar(int xPos, int yPos, int width, int height, int arcWidth, int arcHeight, float currentValue, float maxValue, Color borderColor, Color fillColor, Graphics2D g) {
        reversedFilledProgressBar(currentValue, maxValue, width, xPos, yPos, height, arcWidth, arcHeight, fillColor, g);
        progressBar(xPos, yPos, width, height, arcWidth, arcHeight, borderColor, g);
    }

    public static void drawReversedProgressBar(int xPos, int yPos, int width, int height, int arcWidth, int arcHeight, float currentValue, float maxValue, Graphics2D g) {
        reversedFilledProgressBar(currentValue, maxValue, width, xPos, yPos, height, arcWidth, arcHeight, Color.cyan, g);
        progressBar(xPos, yPos, width, height, arcWidth, arcHeight, Color.black, g);
    }


    private static void progressBar(int xPos, int yPos, int width, int height, int arcWidth, int arcHeight, Color colorOfBorder, Graphics2D g) {
        g.setColor(colorOfBorder);
        g.drawRoundRect(xPos, yPos, width, height, arcWidth, arcHeight);
    }

    private static void filledProgressBar(float currentValue, float maxValue, int width, int xPos, int yPos, int height, int arcWidth, int arcHeight, Color colorOfFill, Graphics2D g) {
        currentValue = (currentValue >= maxValue) ? maxValue : currentValue;
        g.setColor(colorOfFill);
        g.fillRoundRect(xPos, yPos, calculateFillAmount(currentValue, maxValue, width), height, arcWidth, arcHeight);
    }

    private static void reversedFilledProgressBar(float currentValue, float maxValue, int width, int xPos, int yPos, int height, int arcWidth, int arcHeight, Color colorOfFill, Graphics2D g) {
        currentValue = (currentValue >= maxValue) ? maxValue : currentValue;
        g.setColor(colorOfFill);
        g.fillRoundRect(xPos, yPos, width - calculateFillAmount(currentValue, maxValue, width), height, arcWidth, arcHeight);
    }

    private static int calculateFillAmount(float currentValue, float maxValue, int width) {
        float percentValueOfFillAmount = currentValue / maxValue;
        return (int) (percentValueOfFillAmount * width);
    }

}

 

Edited by BravoTaco
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...