java – 使用动态形状获取鼠标检测

基本上我正在构建一张世界地图.我知道如何有一个方形点击区域.但我想做到这一点,我可以将这些国家放在一起,并能够点击该国家.现在很明显我不能使用方形点击区域因为我有重叠的点击区域.我可以通过查看每个像素的透明度来做到这一点吗?即便如此,我不知道该怎么做?

解决方法:

使用Shape.contains(Point2D) – 这样的事情:

此示例使用重叠椭圆来显示contains(..)方法将如何准确识别鼠标单击所在的椭圆.但是你所指的那种地图可能是由一些不重叠的GeneralPath对象(每个国家一个)构成的.

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import java.util.*;
import javax.imageio.ImageIO;

public class ShapeContainment {

    List<Ellipse2D> shapes = new ArrayList<Ellipse2D>();
    int w = 400;
    int h = 100;
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Point2D mouse = new Point2D.Double(0, 0);
    JLabel l;

    ShapeContainment() {
        Random r = new Random();
        for (int ii = 0; ii < 10; ii++) {
            int x = r.nextInt(w / 2);
            int y = r.nextInt(h / 2);
            int wE = r.nextInt(w - x);
            int hE = r.nextInt(h - y);
            Ellipse2D ellipse = new Ellipse2D.Double(x, y, wE, hE);
            shapes.add(ellipse);
        }
        l = new JLabel(new ImageIcon(img));
        MouseAdapter listener = new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent me) {
                mouse = me.getPoint();
                drawImage();
            }
        };
        l.addMouseListener(listener);
        drawImage();

        JOptionPane.showMessageDialog(null, l);
    }

    public void drawImage() {
        Graphics2D g = img.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);

        RenderingHints hints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHints(hints);

        Color bg = new Color(0, 128, 0, 60);
        Color inside = new Color(0, 0, 255, 120);
        Color outside = new Color(255, 0, 0, 120);
        g.setStroke(new BasicStroke(4));
        for (Ellipse2D shape : shapes) {
            g.setColor(bg);
            g.fill(shape);
            if (shape.contains(mouse)) {
                g.setColor(inside);
            } else {
                g.setColor(outside);
            }
            g.draw(shape);
        }
        g.setColor(Color.RED);
        int x = (int) mouse.getX();
        int y = (int) mouse.getY();
        g.setStroke(new BasicStroke(2));
        int s = 3;
        g.drawLine(x-s, y, x+s, y);
        g.drawLine(x, y-s, x, y+s);
        l.setIcon(new ImageIcon(img));

        g.dispose();

        try {
            ImageIO.write(
                    img,
                    "png",
                    new File(System.currentTimeMillis() + ".png"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Runnable run = new Runnable() {

            @Override
            public void run() {
                new ShapeContainment();
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(run);
    }
}
上一篇:在Java中截取屏幕截图(机器人,代码工作,但没有外部全屏应用程序)


下一篇:java – 为什么我的线不画?