0

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);
}
Karyuu Ouji
  • 314
  • 3
  • 13
  • Try this: http://stackoverflow.com/questions/14036173/how-can-you-make-a-progress-bar-without-using-jprogressbar – Keno Mar 26 '16 at 02:55
  • 1
    Do you have the code for the 'green bar' – Paul MacGuiheen Mar 26 '16 at 02:56
  • @KenoClayton I understand the example there but I can't figure out how to implement/code it... If my bar was a simple rectangle, it's easy to code it.. but with my custom shape as my bar.. I can't figure out how to do it.. – Karyuu Ouji Mar 26 '16 at 04:11
  • How does that affect it? I think you'd just need to add an if statement before the setPaint method to check if the progress is at a certain point and change the paint to reflect that. – Keno Mar 26 '16 at 04:35
  • @KenoClayton Well, the bar is set to a certain width and should shrink relative to the time remaining... suppose the width of the bar is 342 px and the time is set to 60 seconds... so every seconds the bar would shrink... My head is going to burst trying to figure out a calculation to make this work... I wanted to know the calculation of how many pixels per second i should shrink... – Karyuu Ouji Mar 26 '16 at 04:50
  • If it's shrinking every second by a set amount then all you'd have to do is create a timer that triggers every 1000 milliseconds, and reduce the width of the bar. – Keno Mar 26 '16 at 05:11
  • I found a solution to my problem.. I used the clipping of `Graphics2D` and simplified the code... I will post my code later if I get it to work with changing colors... – Karyuu Ouji Mar 26 '16 at 06:30

0 Answers0