简体   繁体   English

将鼠标悬停在图像上时可以更改多个图像

[英]Multiple image change on mouse hover over an image

I want to get the solution for a Java program. 我想获得Java程序的解决方案。 I want to change the image in a rectangular box as soon as the mouse cursor hovers over the box. 我想在鼠标指针悬停在矩形框上时更改图像。 The images in the box has to keep on changing (at least 5 different images) till the mouse is on the images and stop changing as the cursor goes out of the box.I want to set the images using only the g.drawImage() , where g is a Graphics2D object. 框中的图像必须不断变化(至少5个不同的图像),直到鼠标停留在图像上,并随着光标移出框而停止变化。我只想使用g.drawImage()设置图像,其中g是Graphics2D对象。

Please help me figure out this problem as i am stuck. 请帮助我找出这个问题,因为我被困住了。

Start with How to Write a Mouse-Motion Listener . 如何编写鼠标运动监听器开始 You will need to determine when the mouse enters your prescribed area, you can use a java.awt.Rectangle to help with this as it has a contains(Point) method. 您将需要确定鼠标何时进入您的指定区域,可以使用java.awt.Rectangle进行帮助,因为它具有contains(Point)方法。

Then, take a look at How to Use Swing Timers , which you can use to trigger the changes in the picture. 然后,看看如何使用Swing计时器 ,您可以使用它来触发图片中的更改。

Basically, as the mouse moves into your Rectangle , you would start the Swing Timer , which would trigger an action event, where by you would update which picture was to be displayed and call repaint . 基本上,当鼠标移至Rectangle ,您将启动Swing Timer ,该Swing Timer将触发一个动作事件,在此您可以更新要显示的图片并调用repaint When the mouse moves out side of the Rectangle you would simply stop the timer. 当鼠标移出Rectangle一侧时,您只需停止计时器。

Use ImageIcon array add set all path in ImageIcon when mouse enter on label, the image changes after 1 second. 当鼠标进入标签时,使用ImageIcon数组在ImageIcon添加设置所有路径,图像在1秒钟后发生变化。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class myImage extends JFrame implements  MouseMotionListener
{
    ImageIcon[] m=new ImageIcon[2]; 
    JLabel l;
    int i,l1;
    public myImage()
    {
        setLayout(null);
        setSize(1000,1000);
        setVisible(true);
        m[0]=new ImageIcon("m.jpg");
        m[1]=new ImageIcon("m1.jpg");
        l=new JLabel();
        l.setBounds(400,0,getWidth(),getHeight());
        add(l);
        l.addMouseMotionListener(this);
    }

    public void mouseMoved(MouseEvent e) 
    {
        if(i<2)
        {
            l.setIcon(m[i]);
            i++;
            try{
                Thread.sleep(1000);
            }
            catch(Exception e1)
            {
            }
        }
        else
        {
            i=0;
        }
    }

    public void mouseDragged(MouseEvent e) 
    {
        System.out.print("Mouse bye");
    }

    public static void main(String args[])
    {
        myImage i1=new myImage();
    }
}

This code may help you some! 此代码可能对您有所帮助!

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class answer {

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

    public answer(){
        JFrame frame=new JFrame();
        frame.getContentPane().add(new rectangle());
        frame.pack();
        frame.setVisible(true);

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

    public class rectangle extends JPanel{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        Image img;
        Timer timer;
        int count=0;
        public rectangle() {

            setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
            setPreferredSize(new Dimension(100,100));
            timer=new Timer(800, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    loadPic(count%5);
                    count++;
                    revalidate();
                    repaint();
                }
            });

            addMouseListener(new MouseAdapter() {

                @Override
                public void mouseEntered(MouseEvent arg0) {
                    count=0;
                    timer.start();
                }

                @Override
                public void mouseExited(MouseEvent arg0) {
                    timer.stop();
                }
            });
        }
        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.drawImage(img, 0, 0, 120, 100,this);
        }

        public void loadPic(int number){

            String address="";

            switch(number){
            case 0: address="img1.jpg";
            break;
            case 1: address="img2.jpg";
            break;
            case 2: address="img3.jpg";
            break;
            case 3: address="img4.jpg";
            break;
            default: address="img5.jpg";
            break;
            }

            try  
            {  
                img = ImageIO.read(getClass().getResourceAsStream(address));

            }  
            catch(Exception e){
                System.out.println("error in loading image");
            }

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM