任何人都可以帮我弄清楚如何让我的JButton(jbt1)旋转我的图像,这是一个jpg文件(wheelof.jpg)一段时间?我一直试图弄清楚这一点,没有运气寻找任何建议。感谢
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class Game extends JFrame implements ActionListener{
private JButton jbt1 = new JButton("SPIN!");
private ImageIcon image;
private JLabel label1;
private GraphicsPanel canvas = new GraphicsPanel();
public Game(){
JPanel control = new JPanel();
control.setLayout(new FlowLayout(2,2,2));
control.add(jbt1);
ImageIcon image = new ImageIcon("wheelof.jpg");
label1 = new JLabel(image);
control.add(label1);
add(canvas, BorderLayout.CENTER);
add(control, BorderLayout.EAST);
jbt1.addActionListener(new RotateListener());
}//game()
public static void main(String[] args) {
JFrame gui = new Game();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(800,1000);
gui.setTitle("Wheel of Fortune");
}//main()
class RotateListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
}
}
class GraphicsPanel extends JPanel implements ActionListener{
}//graphicsPanel
}//class game
答案 0 :(得分:3)
getRotateInstance(double theta, double anchorx, double anchory)
AffineTransform方法可以在这里运作良好。ArrayList<ImageIcon>
中(实际上,如果您对接口进行编码,则为List<Icon>
)。myImageLabel.setIcon(myIconList.get(index))
将它们交换到您的Timer的ActionListener中。actionPerformed(...)
,即index++
,你修改为在使用它之前,Timer中的ArrayList的大小为index %= myIconList.size();
,这样当你尝试从只有10个项目的ArrayList中获取一个项目时,你就不会传入20的索引。答案 1 :(得分:1)
您可以使用Animated Icon课程。它允许您向类添加Icon
,然后它将按您所需的时间间隔顺序显示每个Icon
。该课程将为您管理Timer
。
为了帮助您创建要显示的图标,您可以使用Rotated Icon类以不同的旋转度创建图标。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class AnimatedIconTest2 extends JPanel
{
public AnimatedIconTest2()
{
setLayout( new BorderLayout() );
String path = "dukeWaveRed.gif";
java.net.URL imgURL = getClass().getResource(path);
ImageIcon duke = new ImageIcon(imgURL, path);
JLabel label1 = new JLabel();
final AnimatedIcon icon1 = new AnimatedIcon(label1, 250, 1);
icon1.setShowFirstIcon( true );
icon1.addIcon( duke );
for (int angle = 30; angle < 360; angle += 30)
{
icon1.addIcon( new RotatedIcon(duke, angle) );
}
label1.setIcon( icon1 );
add(label1, BorderLayout.CENTER);
JButton start = new JButton( "Start" );
add(start, BorderLayout.PAGE_END);
start.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
icon1.start();
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Animated Icon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new AnimatedIconTest2() );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}