MouseEvents和Icon/ImageIcon [英] MouseEvents and Icon / ImageIcon

查看:93
本文介绍了MouseEvents和Icon/ImageIcon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请可以从收听MouseEvents图标/ ImageIcon (在没有实现API的任何通知程序),无需监听容器(JPanelJLabel)或通过使用

Please is possible to listening for MouseEvents from Icon / ImageIcon (in API aren't implemented any notifiers), without listening from container (JPanel, JLabel) or by converting events by using SwingUtilities, implements and to add XxxListener to plain vanilla Icon / ImageIcon

编辑

类似代码(@pietblok)之类的东西,但可能无法回答我的问题,我不确定是否创建Graphics Object,BufferedImage和paintIcon是否属于属性

something like as code (@pietblok), but maybe not an answer to my question, I'm not sure if create an Graphics Object, BufferedImage and paintIcon is last of property

(我看到了一些类似的代码,这是SSCCE形式的)

(I saw a few similair code, this is in SSCCE form)

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.WeakHashMap;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestMouseAwareIcon {

    public static class MouseAwareIcon extends MouseAdapter implements Icon {

        private static final long serialVersionUID = 1L;
        private int size = 80, halfSize = 40;
        private final BufferedImage image;
        private Map components = new WeakHashMap();

        public MouseAwareIcon() {
            super();
            image = createImage();
        }

        @Override
        public int getIconHeight() {
            return image.getHeight();
        }

        @Override
        public int getIconWidth() {
            return image.getWidth();
        }

        @Override
        public void mouseClicked(MouseEvent event) {
            Object source = event.getSource();
            if (source instanceof Component) {
                Component component = (Component) source;
                Point paintPoint = (Point) components.get(component);
                if (paintPoint == null) {
                    System.out.println("unknown component");
                } else {
                    Point mousePoint = event.getPoint();
                    int imageX = mousePoint.x - paintPoint.x;
                    int imageY = mousePoint.y - paintPoint.y;
                    if (imageX >= 0 && imageX < this.getIconWidth() && imageY >= 0
                            && imageY < this.getIconHeight()) {
                        int argb = image.getRGB(imageX, imageY);
                        int alpha = (argb << 0) >>> 24;
                        int red = (argb << 8) >>> 24;
                        int green = (argb << 16) >>> 24;
                        int blue = (argb << 24) >>> 24;
                        System.out.println("Color clicked on "
                                + component.getName() + ": " + alpha + ","
                                + red + "," + green + "," + blue);
                        int fillX = halfSize * (imageX / halfSize);
                        int fillY = halfSize * (imageY / halfSize);
                        Graphics2D g2 = image.createGraphics();
                        g2.setColor(new Color(255 - red, 255 - green,
                                255 - blue, alpha));
                        g2.fill3DRect(fillX, fillY, halfSize, halfSize, true);
                        g2.dispose();
                        component.repaint();
                    } else {
                        System.out.println("Clicked outside image area");
                    }
                }
            }
        }

        @Override
        public void paintIcon(Component component, Graphics g, int x, int y) {
            ((Graphics2D) g).drawImage(image, null, x, y);
            if (!components.containsKey(component)) {
                component.addMouseListener(this);
            }
            components.put(component, new Point(x, y));
        }

        private BufferedImage createImage() {
            BufferedImage image1 = new BufferedImage(size, size,
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = image1.createGraphics();
            Color[] colors = new Color[]{Color.BLACK, Color.RED, Color.GREEN,
                Color.BLUE};
            int colorIndex = 0;
            for (int x = 0; x < size; x += halfSize) {
                for (int y = 0; y < size; y += halfSize) {
                    g2.setColor(colors[colorIndex]);
                    g2.fill3DRect(x, y, halfSize, halfSize, true);
                    colorIndex++;
                }
            }
            g2.dispose();
            return image1;
        }
    }

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

            @Override
            public void run() {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel label = new JLabel(new MouseAwareIcon());
                label.setName("label");
                frame.getContentPane().add(label);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });    
    }
}

推荐答案

否,不可能,您必须使用现有的JComponent(例如JPanel或JLabel)或实现临时JComponent来完成您想要的事情.

No, it is not possible, you must use an existing JComponent like JPanel or JLabel or implement an ad-hoc JComponent to do what you want.

这篇关于MouseEvents和Icon/ImageIcon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆