使用Graphics2D绘制ProgressBar

时间:2016-03-26 02:44:18

标签: java graphics2d javax.swing.timer

我想制作一个看起来像this的自定义倒数计时器。 倒计时开始时,我喜欢绿色条消耗并改变颜色,达到一定的百分比。 (例如50%=橙色,25%=红色)

我倒了倒计时代码,但我不知道如何更新绿色条。

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();
    }

我搜索并搜索但我无法找到与我的问题相关的任何内容。我所能找到的只是使用我不想使用的JProgressBar的那些。

更新

这是填写绿色栏的代码。

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 个答案:

没有答案