Drawing ProgressBar using Graphics2D

867 views Asked by At

I wanted to make a custom countdown timer that looks like this. When the countdown starts, I like the green bar to deplete and change color as certain percentage is reached. (Ex. 50% = Orange and 25% = Red)

I got the countdown code down but I do not know how to update the green bar.

private boolean pressed = false;
private long startTime = -1;
private int dur = 10000;


Timer tmr = new Timer(100, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        if (startTime < 0) {
            startTime = System.currentTimeMillis();
        }
        long now = System.currentTimeMillis();
        long clockTime = now - startTime;
        if (clockTime >= dur) {
            clockTime = dur;
            tmr.stop();
        }
        SimpleDateFormat df = new SimpleDateFormat("mm:ss:SSS");
        lblTimer.setText(df.format(dur - clockTime));
    }


 });

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
        tmr.start();
    }

I searched and search but I couldn't find anything related to my problem. All I can find are those that uses JProgressBar which I don't want to use.

Update:

This is the code to fill my green bar.

int xp[] = {115, 115, 288, 294, 449, 457};
int yp[] = {30, 50, 50, 40, 40, 30};

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    GeneralPath cShape = new GeneralPath(GeneralPath.WIND_EVEN_ODD,x2Points.length);
    cShape.moveTo(xp[0], yp[0]);
    for (int i = 1; i < x2Points.length; i++) {
        cShape.lineTo(xp[i], yp[i]);
    }
    cShape.closePath();
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(new Color(0,255,0));
    g2.fill(cShape);
}
0

There are 0 answers