点击进入后,如何让图像消失?

时间:2011-09-20 06:33:32

标签: java image swing mouse-cursor mouselistener

我在JOptionPane中有一个图像,每当我指向鼠标光标并单击它时,我希望它消失。

这个职位有什么关系吗?

...谢谢

以下是代码:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

class ImageBlinking extends JComponent
{


BufferedImage image;
boolean showImage;
int x = -1;
int y = -1;
Random r;

ImageBlinking()
{
    try
    {
        File sourceimage = new File("ball.gif");
        image = ImageIO.read(sourceimage);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }


    r = new Random();


    ActionListener listener = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            if (image != null)
            {
                if (!showImage)
                {
                    int w = image.getWidth();
                    int h = image.getHeight();
                    int rx = getWidth() - w;
                    int ry = getHeight() - h;
                    if (rx > -1 && ry > -1)
                    {
                        x = r.nextInt(rx);
                        y = r.nextInt(ry);
                    }
                }
                showImage = !showImage;
                repaint();
            }
        }
    };
    Timer timer = new Timer(200, listener);
    timer.start();

    setPreferredSize(new Dimension(1000, 1000));
    JOptionPane.showMessageDialog(null, this);
    timer.stop();
}


public void paintComponent(Graphics g)
{


    g.setColor(Color.black);
    g.fillRect(0, 0, getWidth(), getHeight());
    if (image != null)
    {
        g.drawImage(image, x, y,80,80, this);
        setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }


}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable()
    {
        public void run()
        {
            new ImageBlinking();
        }
    });
}
}

2 个答案:

答案 0 :(得分:3)

(编辑:)
我在Keylistener上放了一个JComponent,然后我看看MouseEvent是否在你的Image上,如果是这样的话,我会停止计时器并将图像的颜色变为黑色

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
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 java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

class BlockBlinking extends JComponent {
    BufferedImage image;
    boolean showImage;
    int x = -1;
    int y = -1;
    int imageW = 20;
    int imageH = 20;

    Random r;
    private Timer timer;
    Color imageColor=null;

    BlockBlinking() {
        {
            try
            {
                File sourceimage = new File("ball.gif");
                image = ImageIO.read(sourceimage);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            this.addMouseListener(new MyMouseListener());

            r = new Random();
            ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    if (image != null) {
                        if (!showImage) {
                            int w = image.getWidth();
                            int h = image.getHeight();
                            int rx = getWidth() - w;
                            int ry = getHeight() - h;
                            if (rx > -1 && ry > -1) {
                                x = r.nextInt(rx);
                                y = r.nextInt(ry);
                            }
                        }
                        showImage = !showImage;
                        repaint();
                    }
                }
            };
            timer = new Timer(500, listener);
            timer.start();

            setPreferredSize(new Dimension(500, 400));
            JOptionPane.showMessageDialog(null, this);
            timer.stop();

        }
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());
        if (image != null) {

            if(imageColor != null){
                Graphics imageGraphic =image.createGraphics();
                imageGraphic.setColor(imageColor);
                imageGraphic.fillRect(0, 0, image.getWidth(), image.getHeight());
            }
            g.drawImage(image, x, y,imageW,imageH, this);
            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new BlockBlinking();
            }
        });
    }

    class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.getX() >= x && e.getX() <= x + imageW && e.getY() >= y && e.getY() <= y + imageH) {
                imageColor = Color.BLACK;
                repaint();
                timer.stop();
            }

        }
    }
}

修改:查看字段imageWimageH

答案 1 :(得分:2)

一种方法是使用JToggleButton

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

class ImageVanish extends JComponent {

    ImageVanish() {
        // put your image reading code here..
        BufferedImage imageSelected = new BufferedImage(
            32,32,BufferedImage.TYPE_INT_ARGB);
        BufferedImage image = new BufferedImage(
            32,32,BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.createGraphics();
        g.setColor(Color.ORANGE);
        g.fillOval(0,0,32,32);
        g.dispose();
        // END - image read
        JToggleButton b = new JToggleButton();
        b.setIcon(new ImageIcon(image));
        b.setSelectedIcon(new ImageIcon(imageSelected));
        b.setBorderPainted(false);
        b.setContentAreaFilled(false);
        JOptionPane.showMessageDialog(null, b);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ImageVanish();
            }
        });
    }
}

关于定位,请参阅@Hovercraft FOE关于他们answer对您之前问题的建议。