gpt4 book ai didi

java - 在 JLabel 上的 gif 和 png 之间切换

转载 作者:行者123 更新时间:2023-11-29 03:20:46 31 4
gpt4 key购买 nike

我正在尝试制作俄罗斯轮盘游戏,并且正在尝试将 JLabel 更改为左轮手枪旋转的 gif。但是,我可以将它从 gif 更改为左轮手枪的图片。或者有没有办法只播放一次 gif 并让它停止然后将其更改为另一个 gif?

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class RussianRoulette extends JFrame {

private int Chamber;
private int BulletPos;
private boolean ButtonToggle;
JButton Fire = new JButton("Fire");
JButton Spin = new JButton("Spin");
JLabel Gun = new JLabel();
public RussianRoulette() {
ButtonToggle = true;
Chamber = (int) (Math.random() * 6 + 1);
BulletPos = 0;
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
Gun.setBounds(0, 0, 500, 375);
Fire.setBounds(25, 375, 100, 100);
Spin.setBounds(350, 375, 100, 100);
ImageIcon imgThisImg = new ImageIcon("Revolver2.png");
Gun.setIcon(imgThisImg);
Spin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
spinGun();
sleep(600);

}
});

Fire.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
fireGun();
}
});
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.add(Gun);
frame.add(Fire);
frame.add(Spin);
frame.setResizable(true);
frame.setSize(500, 500);
Image im = Toolkit.getDefaultToolkit().getImage("icon.png");
frame.setIconImage(im);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle("Russian Roulette");

}

public void spinGun() {
ImageIcon imgThisImg = new ImageIcon("SpinRevolver.gif");
Gun.setIcon(imgThisImg);
sleep(600);
AudioPlayer player3 = new AudioPlayer("Spin.wav");
player3.play();
BulletPos = (int) (Math.random() * 6 + 1);
}

public void fireGun() {

if (ButtonToggle == false) {
AudioPlayer player5 = new AudioPlayer("Click.wav");
player5.play();
}

if (Chamber == BulletPos && ButtonToggle == true) {
AudioPlayer player2 = new AudioPlayer("Shot.wav");
player2.play();
ButtonToggle = false;
} else {
Chamber++;
CheckNum();
AudioPlayer player = new AudioPlayer("Click.wav");
player.play();
}
}

public void CheckNum() {
if (Chamber > 6) {
Chamber = 1;
}
}

public void reload()
{
//play reload animation
ButtonToggle = true;
}

public void sleep(int ammount)
{
try {
Thread.sleep(ammount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

最佳答案

因此,根据反馈,您希望动画在单击按钮时开始运行并在给定时间段后自动停止...

可能最简单的解决方案是使用 javax.swing.Timer,否则您需要将自己设置为某种 ImageObserver 并中断各种来自图像的事件...就像我说的,更简单...

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SpiningLabel {

public static void main(String[] args) {
new SpiningLabel();
}

public SpiningLabel() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

private ImageIcon spin;
private ImageIcon still;

private JLabel label;
private Timer timer;
private JButton button;

public TestPane() {
spin = new ImageIcon("spin.gif");
still = new ImageIcon("still.png");

label = new JLabel(still);
button = new JButton("Allons-y!");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setIcon(spin);
button.setEnabled(false);
timer.restart();
}
});
timer = new Timer(2000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setIcon(still);
button.setEnabled(true);
}
});
timer.setRepeats(false);

setLayout(new BorderLayout());
add(label);
add(button, BorderLayout.SOUTH);
}
}

}

关于java - 在 JLabel 上的 gif 和 png 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23749251/

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