当我点击图像时如何使图像消失?

发布于 2024-12-05 08:27:16 字数 2067 浏览 0 评论 0原文

我在 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();
        }
    });
}
}

I have an image inside the JOptionPane and I want it to disappear whenever I point the mouse cursor and click into it.

Is there something to do about the position?

Thanks...

Here's the code :

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();
        }
    });
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬シ愛 2024-12-12 08:27:16

(已编辑:)
我在您的 JComponent 上放置了一个 Keylistener,然后查看 MouseEvent 是否在您的图像上,如果是这种情况,我停止计时器并将图像的颜色设置为黑色

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 字段

(Edited:)
I put a Keylistener on your JComponent, then I look if the MouseEvent is on your Image and if its the case, I stop the timer and put the color of the image to Black

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();
            }

        }
    }
}

Edit: look at the fields imageW and imageH

遮了一弯 2024-12-12 08:27:16

一种方法是使用 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 关于其 回答您之前的问题。

One approach is to use a 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();
            }
        });
    }
}

For the positioning, see @Hovercraft FOE's advice on their answer to your earlier question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文