gpt4 book ai didi

java - 如何在java中创建旋转的圆圈动画?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:51:41 26 4
gpt4 key购买 nike

我想在 JPanel 中间创建一个旋转圆圈,并且能够设置开始/停止旋转,还可以设置为可见/不可见。

有没有人知道怎么做或有任何现有的例子?

private static class RotatingCirclePanel extends JPanel {        
protected void paintComponent(Graphics g) {

}
}

这是一个例子。

enter image description here

已编辑:这是第一个版本。任何人都可以帮助将其制作成 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);
}
}

最佳答案

This answer包含用于创建基于图像的此类图标的代码片段。它基本上使用代码来旋转图标,以及触发旋转以获得动画效果的 Timer

关于java - 如何在java中创建旋转的圆圈动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11650454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com