单击时将图像更改为另一个图像

时间:2015-11-07 08:17:25

标签: java image swing paintcomponent mouselistener

我想在我点击blankCardPic后将blankCardPic更改为clubsAcePic,并且在2秒后,它将从clubsAcePic更改为blankCardPic。我知道可以通过使用JLabels轻松完成,但我想学习如何使用paintComponent来完成它。

现在,当我点击blankCardPic时,它不会更改为clubsAcesPic。 请帮忙。谢谢。

import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GameUI extends JPanel implements MouseListener  {

    /**
     * 
     */
    private static final long serialVersionUID = 8525183729697452509L;

    private boolean clicked = false;
    private JFrame frame;
    private BufferedImage blankCardPic;
    private BufferedImage clubsAcePic;

    public GameUI() {

        loadImages();

        frame = new JFrame();
        frame.setContentPane(this);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

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

    public void loadImages() {
        try {
            blankCardPic = ImageIO.read(getClass().getResourceAsStream("resources/images/blank.png"));
            clubsAcePic = ImageIO.read(getClass().getResourceAsStream("resources/images/clubsAce.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public BufferedImage blankCard() {
        try {
            blankCardPic = ImageIO.read(new File("resources/images/blank.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return blankCardPic;
    }

    public BufferedImage revealClubsAce() {
        try {
            clubsAcePic = ImageIO.read(new File("resources/images/clubsAce.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clubsAcePic;
    }

    private void render(Graphics g) {
        if (!clicked) {
            g.drawImage(blankCardPic, 100,100, null);
        } else {
            g.drawImage(clubsAcePic, 100,100, null);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        render(g);
        Toolkit.getDefaultToolkit().sync();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        setFocusable(true);
        requestFocus();
        addMouseListener(this);

        if(e.getButton() == MouseEvent.BUTTON1) {
            clicked = true;
            repaint();
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
                clicked = false;
            }
        });
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}

0 个答案:

没有答案