Following are some infinite progress bars built in java swing 2d using JPanel’s paintComponent(Graphics g) method.
Windows XP
I have seen this kind of loading icon while setting up windows XP, you might have also seen it:
So how one can simulate same in swing? Here is the answer:
/** * Some custom infinite progress bars in JPanel using Graphics2D object. * @author harsh */ public class CustomInfiniteProgressBar extends JPanel { private static final long serialVersionUID = 223086939802246968L; private static final int DELAY = 100; private static final int MAX_AMOUNT = 100; private static final int SPACE = 30; private static final int NUMBER_OF_RECTS = 5; private Timer timer; private int value = 0, darkRect = 0; public CustomInfiniteProgressBar() { timer = new Timer(DELAY, new TimerActionListener()); timer.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // draw rectangular progress int paddingToFrameBorders = 10; //dummy values for position int smallerSize = 5, largerSize = 10; //dummy values for rectangle size int rectX, rectY; for (int i = 0; i < NUMBER_OF_RECTS; i++) { rectX = paddingToFrameBorders + i * SPACE; rectY = paddingToFrameBorders; if(darkRect == i) { g2d.fillRect(rectX, rectY, largerSize, largerSize); } else { // to center smaller rectangle respective to larger rectangle. rectX += (largerSize - smallerSize) / 2; rectY += (largerSize - smallerSize) / 2; g2d.drawRect(rectX, rectY, smallerSize, smallerSize); } } } /** * Action Listener of Timer. */ private class TimerActionListener implements ActionListener { public void actionPerformed(ActionEvent event) { value++; if(value == MAX_AMOUNT) { // you can have some condition here to stop the timer. timer.stop(); } darkRect = (value % NUMBER_OF_RECTS); repaint(); } } }
Circular infinite progress bar
For this you just have to replace rectangle drawing logic to circle drawing logic and its done:
// global constants private static final int RADIUS = 30; private static final int NUMBER_OF_CIRCLES = 10; private int darkCircle = 0; //inside paintComponent(g) method int centerX, centerY; centerX = centerY = 75; //dummy values for position int circularX, circularY; for (int i = 0; i < NUMBER_OF_CIRCLES; i++) { circularX = centerX + (int) (RADIUS * Math.sin((360 / NUMBER_OF_CIRCLES) * i * 3.14 / 180)); circularY = centerY + (int) (RADIUS * Math.cos((360 / NUMBER_OF_CIRCLES) * i * 3.14 / 180)); if(darkCircle == i) { g2d.fillOval(circularX, circularY, 10, 10); } else { g2d.drawOval(circularX, circularY, 10, 10); } } // in timer action class darkCircle = NUMBER_OF_CIRCLES - 1 - (value % NUMBER_OF_CIRCLES);
Another example of such progress bar:
for this use following code inside paintComponent(g) everything else will same as above:
//inside paintComponent(g) method BasicStroke drawingStroke = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(drawingStroke); for (int i = 0; i < NUMBER_OF_CIRCLES; i++) { circularX = centerX + (int) (RADIUS * Math.sin((360 / NUMBER_OF_CIRCLES) * i * 3.14 / 180)); circularY = centerY + (int) (RADIUS * Math.cos((360 / NUMBER_OF_CIRCLES) * i * 3.14 / 180)); if(darkCircle == i) { g2d.setColor(new Color(100, 200, 150)); g2d.drawLine(circularX, circularY, circularX + 10, circularY + 10); } else { g2d.setColor(Color.black); g2d.drawLine(circularX, circularY, circularX + 10, circularY + 10); } }
Arrow shape for progress bar
I’m not sure if following can be used as progress bar, but it can be used for highlighting something or some similar kind of task.
The code for this as follows:
// inside paintComponent(g) method for (int i = 0; i < NUMBER_OF_RECTS; i++) { if(darkRect == i) g2d.fill(getArrowShape(i * 10, 50, 150)); else g2d.draw(getArrowShape(i * 10, 50, 150)); } // the getArrowShape method /** * Returns arrow shape(a triangle). * Points are: * A (x + i, y) * | \ * | \ * | \ * | C ((x + 10) + i, (y + 10)) * | / * | / * | / * B (x + i, (y + 20)) */ private Shape getArrowShape(int i, int x, int y) { final Polygon result = new Polygon(); result.addPoint(x + i, y); // A result.addPoint(x + i, (y + 20)); // B result.addPoint((x + 10) + i, (y + 10)); // C return result; }
That’s all for now.
Hope You like it. 🙂
One thought on “Infinite progress bars in swing”