2

hey guys i found this class but i have no idea how to use it. i am new to JAVA so go easy on me here is the code. i have tried few ways but nothing worked. it seems edges are smooth.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Arc2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicProgressBarUI;

class ProgressCircleUI extends BasicProgressBarUI {
  @Override public Dimension getPreferredSize(JComponent c) {
    Dimension d = super.getPreferredSize(c);
    int v = Math.max(d.width, d.height);
    d.setSize(v, v);
    return d;
  }
  @Override public void paint(Graphics g, JComponent c) {
    Insets b = progressBar.getInsets(); // area for border
    int barRectWidth  = progressBar.getWidth()  - b.right - b.left;
    int barRectHeight = progressBar.getHeight() - b.top - b.bottom;
    if (barRectWidth <= 0 || barRectHeight <= 0) {
      return;
    }

    // draw the cells
    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(progressBar.getForeground());
    double degree = 360 * progressBar.getPercentComplete();
    double sz = Math.min(barRectWidth, barRectHeight);
    double cx = b.left + barRectWidth  * .5;
    double cy = b.top  + barRectHeight * .5;
    double or = sz * .5;
    double ir = or * .5; //or - 20;
    Shape inner = new Ellipse2D.Double(cx - ir, cy - ir, ir * 2, ir * 2);
    Shape outer = new Arc2D.Double(
        cx - or, cy - or, sz, sz, 90 - degree, degree, Arc2D.PIE);
    Area area = new Area(outer);
    area.subtract(new Area(inner));
    g2.fill(area);
    g2.dispose();

    // Deal with possible text painting
    if (progressBar.isStringPainted()) {
      paintString(g, b.left, b.top, barRectWidth, barRectHeight, 0, b);
    }
  }
}

thing is i do not know how to make it work. any help?

Door D Uthpala
  • 59
  • 1
  • 11
  • 1
    I suggest you check out the docs for `BasicProgressBarUI` as this class extends that one. – jr593 Apr 13 '16 at 09:59
  • 1
    Examples [here](http://stackoverflow.com/a/16182793/230513) and [here](http://stackoverflow.com/a/16333445/230513). – trashgod Apr 13 '16 at 10:07
  • this could help :-) https://github.com/aterai/java-swing-tips/blob/master/ProgressCircle/src/java/example/MainPanel.java – aterai Apr 13 '16 at 10:44

2 Answers2

3

You need to set it for a component or register it globally as L&F default.

myProgressBar.setUI(new ProgressCircleUI());

or

UIManager.put("ProgressBarUI", ProgressCircleUI.class.getName());

Note: in some cases Swing can reset the UI which is set by the first way. So if the first way does not work, try the second.

Update: there is a third way: you can create a subclass of JProgressBar and use it instead of normal ones.

public class JCircleProgressBar extends JProgressBar {

    public void setUI(ComponentUI newUI) {
        super.setUI(new ProgressCircleUI());
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
1
JProgressBar progress = new JProgressBar();
// use JProgressBar#setUI(...) method
progress.setUI(new ProgressCircleUI());
progress.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
progress.setStringPainted(true);
progress.setFont(progress.getFont().deriveFont(24f));
progress.setForeground(Color.ORANGE);

int in = Ingeter.parseInt(nameOfTextField.getValue);   
progress.setValue(in);

You need to create a progress bar and add the class to the progress bar( progress.setUI(new ProgressCircleUI()); Then add the progress bar to a panel. The code you have above is the class that would create a circular progress bar.