2008-06-02 19:12:28 +00:00
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
package net.sourceforge.tuned.ui;
|
2008-06-02 19:12:28 +00:00
|
|
|
|
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
import java.awt.BasicStroke;
|
|
|
|
import java.awt.Color;
|
2008-06-02 19:12:28 +00:00
|
|
|
import java.awt.Dimension;
|
2008-05-03 20:43:15 +00:00
|
|
|
import java.awt.Graphics;
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import java.awt.RenderingHints;
|
|
|
|
import java.awt.Stroke;
|
2008-12-27 11:35:53 +00:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
import java.awt.event.ComponentAdapter;
|
|
|
|
import java.awt.event.ComponentEvent;
|
2008-05-03 20:43:15 +00:00
|
|
|
import java.awt.geom.Ellipse2D;
|
|
|
|
import java.awt.geom.Point2D;
|
|
|
|
import java.awt.geom.Rectangle2D;
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
import javax.swing.JComponent;
|
|
|
|
import javax.swing.Timer;
|
2008-05-03 20:43:15 +00:00
|
|
|
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
public class ProgressIndicator extends JComponent {
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
private float radius = 4.0f;
|
|
|
|
private int shapeCount = 3;
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
private float strokeWidth = 2f;
|
|
|
|
private Stroke stroke = new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
private Color progressShapeColor = Color.orange;
|
|
|
|
private Color backgroundShapeColor = new Color(0f, 0f, 0f, 0.25f);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
|
|
|
private final Rectangle2D frame = new Rectangle2D.Double();
|
|
|
|
private final Ellipse2D circle = new Ellipse2D.Double();
|
2008-06-02 19:12:28 +00:00
|
|
|
private final Dimension baseSize = new Dimension(32, 32);
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
private double alpha = 0;
|
|
|
|
private double speed = 1.2;
|
|
|
|
|
|
|
|
private final Timer updateTimer = new Timer(20, new ActionListener() {
|
2008-12-27 11:35:53 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2009-01-01 22:27:53 +00:00
|
|
|
Timer timer = (Timer) e.getSource();
|
|
|
|
|
|
|
|
alpha += (timer.getDelay() * speed) / 1000;
|
|
|
|
|
|
|
|
if (alpha >= 1)
|
|
|
|
alpha -= Math.floor(alpha);
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
repaint();
|
|
|
|
}
|
2008-12-28 20:04:13 +00:00
|
|
|
});
|
2008-05-03 20:43:15 +00:00
|
|
|
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
public ProgressIndicator() {
|
|
|
|
setPreferredSize(baseSize);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
addComponentListener(new ComponentAdapter() {
|
|
|
|
|
|
|
|
@Override
|
2008-12-31 11:54:44 +00:00
|
|
|
public void componentShown(ComponentEvent e) {
|
|
|
|
startAnimation();
|
2008-12-27 11:35:53 +00:00
|
|
|
}
|
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
@Override
|
2008-12-31 11:54:44 +00:00
|
|
|
public void componentHidden(ComponentEvent e) {
|
|
|
|
stopAnimation();
|
2008-12-27 11:35:53 +00:00
|
|
|
}
|
|
|
|
});
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2008-06-02 19:12:28 +00:00
|
|
|
public void paintComponent(Graphics g) {
|
2008-05-03 20:43:15 +00:00
|
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
double a = Math.min(getWidth(), getHeight());
|
|
|
|
|
|
|
|
g2d.scale(a / baseSize.width, a / baseSize.height);
|
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
frame.setFrame(radius, radius, baseSize.width - radius * 2 - 1, baseSize.height - radius * 2 - 1);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
paintShapes(g2d);
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-28 20:04:13 +00:00
|
|
|
private void paintShapes(Graphics2D g2d) {
|
2008-05-03 20:43:15 +00:00
|
|
|
circle.setFrame(frame);
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
g2d.setStroke(stroke);
|
|
|
|
g2d.setPaint(backgroundShapeColor);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
|
|
|
g2d.draw(circle);
|
|
|
|
|
|
|
|
Point2D center = new Point2D.Double(frame.getCenterX(), frame.getMinY());
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
circle.setFrameFromCenter(center, new Point2D.Double(center.getX() + radius, center.getY() + radius));
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
g2d.setStroke(stroke);
|
|
|
|
g2d.setPaint(progressShapeColor);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
// base rotation
|
|
|
|
g2d.rotate(getTheta(alpha, 1.0), frame.getCenterX(), frame.getCenterY());
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
double theta = getTheta(1, shapeCount);
|
2008-05-03 20:43:15 +00:00
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
for (int i = 0; i < shapeCount; i++) {
|
2008-05-03 20:43:15 +00:00
|
|
|
g2d.rotate(theta, frame.getCenterX(), frame.getCenterY());
|
|
|
|
g2d.fill(circle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
private double getTheta(double value, double max) {
|
|
|
|
return (value / max) * 2 * Math.PI;
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
public void startAnimation() {
|
|
|
|
updateTimer.restart();
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
public void stopAnimation() {
|
|
|
|
updateTimer.stop();
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-27 11:35:53 +00:00
|
|
|
public void setShapeCount(int indeterminateShapeCount) {
|
|
|
|
this.shapeCount = indeterminateShapeCount;
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-01 22:27:53 +00:00
|
|
|
|
|
|
|
public void setSpeed(double speed) {
|
|
|
|
this.speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double getSpeed() {
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
|
2008-05-03 20:43:15 +00:00
|
|
|
}
|