获取鼠标的检测与动感的外形 [英] Get mouse detection with a dynamic shape

查看:157
本文介绍了获取鼠标的检测与动感的外形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我构建的世界地图。我知道如何有一个方形的点击区域。但我想使它这样我就可以把国家一起,并能点击的国家。现在是pretty明显,我不能使用方点击领域的,因为我有重叠的点击区域。我能做到这一点通过查看每个像素的透明度?即便如此,我不知道该怎么做?

Basically I'm constructing a world map. I know how to have a square click area. But I'd like to make it so I can put the countries together and be able to click on the country. Now it's pretty obvious that I can't use square click areas for that because I'd have overlapping click areas. Could I do it by looking at the transparency of each pixel? Even so I don't know how to do that?

推荐答案

使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/Shape.html#contains%28java.awt.geom.Point2D%29\"><$c$c>Shape.contains(Point2D) - 是这样的:

Use Shape.contains(Point2D) - something like this:

本例使用重叠椭圆显示如何包含(..)方法将准确地识别椭圆形的鼠标点击下降中。但是地图的你是指那种可能会被提出了一些<一的href=\"http://docs.oracle.com/javase/7/docs/api/java/awt/geom/GeneralPath.html\"><$c$c>GeneralPath对象(每个国家)不重叠。

This example uses overlapping ellipses to show how the contains(..) method will accurately identify which ovals the mouse click falls inside. But the kind of map you are referring to will probably be made of a number of GeneralPath objects (one for each country) that do not overlap.

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

这篇关于获取鼠标的检测与动感的外形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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