How do I create a JPanel with two images where only a part of the one below is shown on mouse over?

三世轮回 提交于 2019-12-19 11:49:09

问题


I am trying to create a button panel where the button that was clicked becomes 'differently colored'; i.e show the background image. p.s I only need this approach(with 2 images), and not anything else. Thanks !

Eg:

public class TestPane extends JPanel {      

        private BufferedImage   imgUnclicked;
        private BufferedImage   imgClicked;
        private Point           mousePoint;

        public TestPane() {
            try {
                imgUnclicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Uncolored.png"));
                imgClicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Colored.png"));
            } catch (IOException ex) {
                Logger.getLogger(Spotlight.class.getName()).log(Level.SEVERE, null, ex);
            }

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    mousePoint = e.getPoint();
                    repaint();
                }
            });
        }
    }
    @Override
    protected void paintComponent(Graphics g) {
        //Draw imgClicked
        //Draw imgUnclicked with some rectangular area around mouse click subtracted
    }
}

回答1:


No need to reinvent the wheel. Instead use a JToggleButton (appropriately configured). A button will react to both mouse and keyboard input.

import java.awt.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

class ChangeImageOnClick {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://i.stack.imgur.com/gJmeJ.png");
        final Image img1 = ImageIO.read(url1);
        URL url2 = new URL("http://i.stack.imgur.com/wCF8S.png");
        final Image img2 = ImageIO.read(url2);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JToggleButton btn = new JToggleButton("Click me!");
                btn.setIcon(new ImageIcon(img1));
                btn.setSelectedIcon(new ImageIcon(img2));

                btn.setContentAreaFilled(false);
                btn.setBorderPainted(false);

                JOptionPane.showMessageDialog(null, btn);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}



回答2:


A different idea. Basically, load an image into a JLabel, set the Layout of the label and add two opaque components to it.

By using a simple MouseListener, you can either make the component invisible or transparent based on your needs...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class HideAndShow {

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

    public HideAndShow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Haibane_Miho___Take_2_by_garrbage.png"));
                JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 200, Image.SCALE_SMOOTH)));
                add(label);

                label.setLayout(new GridLayout(2, 1));

                JPanel top = new JPanel();
                top.add(new JLabel("Top"));
                JPanel bottom = new JPanel();
                bottom.add(new JLabel("Bottom"));

                MouseAdapter ma = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        ((JPanel)e.getComponent()).setOpaque(false);
                        repaint();
                    }
                };

                top.addMouseListener(ma);
                bottom.addMouseListener(ma);

                label.add(top);
                label.add(bottom);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}



回答3:


Add an ActionListener to your button and call setIcon with the imgClicked.

Something like this:

    JButton btn = new JButton();
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btn.setIcon(imgClicked);
        }
    });


来源:https://stackoverflow.com/questions/18654930/how-do-i-create-a-jpanel-with-two-images-where-only-a-part-of-the-one-below-is-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!