Swing java动画仅在未从侦听器调用时工作

Swing java动画仅在未从侦听器调用时工作,java,swing,animation,Java,Swing,Animation,我正在尝试用java(swing)中的动画创建一个函数。我想创建一个“堆栈”的卡,然后能够从堆栈中使用一种方法绘制一张卡。然后,我画的卡片应该“翻转”以显示正面 这是很好的工作,除非当我试图绘制一个新的卡使用侦听器 我通过构建堆栈然后绘制一张卡片来启动程序。然后我有一个按钮,可以绘制一张新卡(actionlistener)。动画没有显示,但是我可以看到堆栈减少了一张卡。我尝试创建一个gif来显示问题 我最好的猜测是它与线程或EDT有关 下面是代码(因为Scalr类并不重要,所以我已经删除了它的

我正在尝试用java(swing)中的动画创建一个函数。我想创建一个“堆栈”的卡,然后能够从堆栈中使用一种方法绘制一张卡。然后,我画的卡片应该“翻转”以显示正面

这是很好的工作,除非当我试图绘制一个新的卡使用侦听器

我通过构建堆栈然后绘制一张卡片来启动程序。然后我有一个按钮,可以绘制一张新卡(actionlistener)。动画没有显示,但是我可以看到堆栈减少了一张卡。我尝试创建一个gif来显示问题

我最好的猜测是它与线程或EDT有关

下面是代码(因为Scalr类并不重要,所以我已经删除了它的使用):

import java.awt.*;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
导入java.io.*;
导入java.net.URL;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.imageio.imageio;
导入javax.swing.*;
//导入org.imgscalr.Scalr;因堆栈溢出而移除
公共类测试扩展了JFrame{
public static JPanel cardPanel=new JPanel();
公共静态JLayeredPane layerPane=新JLayeredPane();
私人最终CardFlip CardFlip;
测试(){
CardFlip=新的CardFlip();
此.setLayout(新的BorderLayout());
layerPane.setPreferredSize(新尺寸(700300));
添加(cardPanel,BorderLayout.CENTER);
JButton newJButton=新JButton(“新卡”);
addActionListener(newDrawNewCardListener());
添加(newJButton,BorderLayout.SOUTH);
设置大小(800400);
setLocationRelativeTo(空);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
点位置=新点(10,30);
CardFlip.CreateCardFlip(30,位置,层平面,cardPanel);
System.out.println(“现在正在线程上运行”+thread.currentThread());
试一试{
睡眠(3000);
}捕获(中断异常例外){
Logger.getLogger(Test.class.getName()).log(Level.SEVERE,null,ex);
}
CardFlip.displayCard(“http://imgi.se/image.php?di=IYW9");
}
公共静态void main(字符串[]args){
新测试();
}
类drawNewCardListener实现ActionListener{
@凌驾
已执行的公共无效行动(行动事件ae){
System.out.println(“现在正在线程上运行”+thread.currentThread());
CardFlip.displayCard(“http://imgi.se/image.php?di=IYW9");
}
}
}
类CardFlip扩展了JPanel{
私人点源;
私有缓冲区图像图标;
私人住宅;
私人JPanel JPanel;
private int lastcarddisplayedd=0;
公共作废CreateCardFlip(整数金额、积分位置、JLayeredPane lPaneRef、JPanel jPanelRef){
原点=位置;
设置图标(“http://imgi.se/image.php?di=HPCJ");
lPane=lPaneRef;
jPanel=jPanelRef;
对于(int i=金额;i>0;i--){
原点x+=3;
lPane.add(新卡(原点,图标,i),新整数(金额-i));
jPanel.add(lPane);
lPane.revalidate();
lPane.repaint();
试一试{
睡眠(80);
}捕获(中断异常例外){
Logger.getLogger(cardfrip.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
公共无效显示卡(字符串文件URL){
卡c=(卡)lPane.getComponent(0);
如果(lastCardDisplayedId==0){
原点=新点(c.getBounds().x+370,c.getBounds().y);
}
int cardId=c.getId();
setIcon(fileUrl);
c、 FlipCardRight(LastCardDisplayeId);
lPane.remove(c);
如果(lastCardDisplayedId!=0){
for(int i=0;i最小化){
//buffImg=Scalr.resize(buffImg,Scalr.Method.SPEED,Scalr.Mode.FIT_-EXACT,//iconImg.getIconWidth()-最小化,iconImg.getIconHeight(),Scalr.OP_反别名);
//iconImg=新图像图标(buffImg);
//setBounds(getBounds().x+minimize-1,getBounds().y,iconImg.getIconWidth(),iconImg.getIconHeight());
//wait10miliec();
//}
}
私有void wait10miliec(){
试一试{
睡眠(10);
}捕获(中断异常例外){
Logger.getLogger(Card.class.getName()).log(Level.SEVERE,null,ex);
}
}
公共int getId(){
返回此.id;
}
public int getImgWidth(){
返回iconImg.getIconWidth();
}
}

Question=无论是否从侦听器调用动画,都可以通过什么方式显示动画?

使用计时器将是您的解决方案:)那家伙也有类似的问题,您
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
// import org.imgscalr.Scalr; Removed for Stackoverflow

public class Test extends JFrame{

public static JPanel cardPanel = new JPanel();
public static JLayeredPane layerPane = new JLayeredPane();
private final CardFlipp cardFlipp;


Test() {
    cardFlipp = new CardFlipp();
    this.setLayout(new BorderLayout());
    layerPane.setPreferredSize(new Dimension(700, 300));
    add(cardPanel, BorderLayout.CENTER);
    JButton newJButton = new JButton("New card");
    newJButton.addActionListener(new drawNewCardListener());
    add(newJButton, BorderLayout.SOUTH);
    setSize(800,400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    Point pos = new Point(10,30);
    cardFlipp.createCardFlipp(30, pos, layerPane, cardPanel);
    System.out.println("Now running on thread " + Thread.currentThread());
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
    cardFlipp.displayCard("http://imgi.se/image.php?di=IYW9");
}

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

    class drawNewCardListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Now running on thread " + Thread.currentThread());
        cardFlipp.displayCard("http://imgi.se/image.php?di=IYW9");
    }
    }
}


class CardFlipp extends JPanel{

private Point origin;
private BufferedImage icon;
private JLayeredPane lPane;
private JPanel jPanel;
private int lastCardDisplayedId = 0;

public void createCardFlipp(int amount, Point pos, JLayeredPane lPaneRef, JPanel jPanelRef) {
    origin = pos;
    setIcon("http://imgi.se/image.php?di=HPCJ");
    lPane = lPaneRef;
    jPanel = jPanelRef;
    for(int i = amount; i > 0; i--) {
        origin.x += 3;
        lPane.add(new Card(origin, icon, i), new Integer(amount-i));
        jPanel.add(lPane);
        lPane.revalidate();
        lPane.repaint();
        try {
            Thread.sleep(80);
        } catch (InterruptedException ex) {
            Logger.getLogger(CardFlipp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

public void displayCard(String fileUrl) {
    Card c = (Card) lPane.getComponent(0);
    if(lastCardDisplayedId == 0) {
        origin = new Point(c.getBounds().x + 370, c.getBounds().y);
    }
    int cardId = c.getId();
    setIcon(fileUrl);
    c.flippCardRight(lastCardDisplayedId);
    lPane.remove(c);
    if(lastCardDisplayedId != 0) {
        for(int i = 0; i < lPane.getComponentCount(); i++) {
            c = (Card) lPane.getComponent(i);
            if(c.getId() == lastCardDisplayedId) {
                lPane.remove(c);
                break;
            }
        }
    }
    lPane.repaint();
    lPane.add(new Card(origin, icon, cardId), new Integer(0));
    jPanel.add(lPane);
    lastCardDisplayedId = cardId;
}

private void setIcon(String urlPath) {
    try {
        URL url = new URL(urlPath);
        icon = ImageIO.read(url);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(-1);
        icon = null;
    }
}
}

class Card extends JComponent {

private BufferedImage buffImg;
private ImageIcon iconImg;
private final int id;

public Card(Point origin, BufferedImage img, int count) {
    buffImg = img;
    iconImg = new ImageIcon(buffImg);
    setBounds(origin.x,origin.y,iconImg.getIconWidth(),iconImg.getIconHeight());
    this.id = count;
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(iconImg.getImage(), 0, 0, this);
}

public void flippCardRight(int count) {
    int x = count * 3;
    int newPosX = getBounds().x + iconImg.getIconWidth() + 20;

    while(getBounds().x < newPosX + x) {
        setBounds(getBounds().x+5,getBounds().y,iconImg.getIconWidth(),iconImg.getIconHeight());
        wait10MiliSec();
    }

    // Removed to not need the scalr class
    //int minimize = 10;
    //while(iconImg.getIconWidth() > minimize) {
        //buffImg = Scalr.resize(buffImg, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT, //iconImg.getIconWidth()-minimize, iconImg.getIconHeight(), Scalr.OP_ANTIALIAS);
        //iconImg = new ImageIcon(buffImg);
        //setBounds(getBounds().x+minimize-1,getBounds().y,iconImg.getIconWidth(),iconImg.getIconHeight());
        //wait10MiliSec();
    //}
}

private void wait10MiliSec() {
    try {
        Thread.sleep(10);
    } catch (InterruptedException ex) {
        Logger.getLogger(Card.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public int getId() {
    return this.id;
}

public int getImgWidth() {
    return iconImg.getIconWidth();
}
}