我想在中间创建一个旋转圆圈,JPanel
并能够设置开始/停止旋转并设置为可见/不可见。
有没有人知道如何做或任何现有的例子?
private static class RotatingCirclePanel extends JPanel {
protected void paintComponent(Graphics g) {
}
}
这是一个例子。
编辑:这是第一个版本。任何机构都可以帮助制作 gif 示例吗?
public class ProgressWheelPanel extends JPanel {
private double angleInDegrees = 1;
private Timer rotatingTimer;
public ProgressWheelPanel() {
rotatingTimer = new Timer(100, new ActionListener() {
// @Override
public void actionPerformed(ActionEvent e) {
angleInDegrees = angleInDegrees +1;
if (angleInDegrees == 360) {
angleInDegrees = 0;
}
repaint();
}
});
rotatingTimer.setRepeats(false);
rotatingTimer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
rotatingTimer.stop();
g2d.clearRect(0, 0, getWidth(), getHeight());
g2d.setBackground(Color.white);
g2d.setColor(Color.black);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
g2d.rotate(angleInDegrees * Math.PI / 180.0, getWidth() / 2, getHeight() / 2);
g2d.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2 + 100, getHeight() / 2);
//**************************************************************************************
AffineTransform transformer = new AffineTransform();
transformer.translate(5,5);
transformer.scale(2,2);
g2d.getTransform().concatenate(transformer);
//***************************************************************************************
g2d.dispose();
rotatingTimer.start();
}
public void start(){
rotatingTimer.start();
}
public void stop(){
rotatingTimer.stop();
}
public static void main(String[] args) {
final ProgressWheelPanel demo = new ProgressWheelPanel();
WebButton btnStrat= new WebButton("Start"),btnStop= new WebButton("Stop");
btnStrat.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
demo.setVisible(true);
demo.start();
}
});
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
demo.stop();
demo.setVisible(false);
}
});
JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.add(demo,BorderLayout.CENTER);
cp.add(btnStrat,BorderLayout.NORTH);
cp.add(btnStop,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}