Jump to content

Progress Bar


BravoTaco

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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