Reputation: 59
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?
Upvotes: 2
Views: 8484
Reputation: 11
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.
Upvotes: 1
Reputation: 11327
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());
}
}
Upvotes: 3