Java拖曳图片更好&重叠图像检测 [英] Java drag image better & Overlapping image detection

查看:111
本文介绍了Java拖曳图片更好&重叠图像检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题要问你

  1. [已解决]-在Java中,我能够使用鼠标侦听器在图像周围移动.与其将图像精确地移到我的指针所处的位置,不如单击并向上移动鼠标,而是将图像向上移.不要让图像跳到我的鼠标指针所在的位置.

  1. [SOLVED] - In java, I have the ability to move around an image using the mouse listeners. Instead of moving the image exactly where my pointer is, how do I make it so if I click and move up the mouse, it just moves the image up. Not make the image jump to where my mouse pointer is.

[已解决]-由于我正在构建编辑器,如果我在窗口上可以移动的多个图像,如果有两个重叠的图像,那么如何检测应该实际移动的图像.如果我想将图像移到图像的后面而不是图像的前面,反之亦然.你们在这里做的最好的方法是什么?

[SOLVED] - Since I am building an editor, If I have multiple images on the window that you can move around, if there is two overlapping images, how do I detect which image I should actually move. What happens If I want to move the one behind the image instead of the front one or vice versa. What is the best method that you guys have done here.

与这两个问题相关的一些代码

Some code that relates to both of these questions

addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseDragged(MouseEvent arg0) {
            // TODO Auto-generated method stub
            //use this
            if(curObj != null){

                Point p = arg0.getPoint();

                curObj.pos.x = p.x;
                curObj.pos.y = p.y;

                ......
            }
        }
    });


addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
            //right now find the first one that contains the mouse.
            //later we can make it so they have an option to pick on that overlaps.

            Point p = arg0.getPoint();
            SwingUtilities.convertPoint(Main.gui, p, instance);

                            ....

                            //this is the code to detect which image to use
            curObj = null;
            for(int i = 0; i < objects.size(); i++){
                StepObject obj = objects.get(i);
                if(obj.isDraggable()){
                    if(p.x >= obj.pos.x && p.y >= obj.pos.y &&
                       p.x <= (obj.pos.x + obj.getWidth()) && p.y <= (obj.pos.y + obj.getHeight())){
                        curObj = obj;
                        break;
                    }
                }
            }

            .....

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }
    });

任何反馈都值得赞赏.

谢谢.

推荐答案

我实际上是在使用Graphics绘制图像.

这是一个最早出现在广泛使用的对象绘制程序中的用户界面问题.常见的方法是实现两个基本操作,这些操作使用户可以通过更改其渲染顺序来显示隐藏的对象:

This has been a user interface problem going back to the earliest, widely-available object drawing programs. The common approach is to implement two basic operations that enable the user to expose a hidden object by changing it's rendering order:

  • 后退:以 z 顺序将选择项后退一步.
  • 后退:将所选内容移到 z 顺序的后方.
  • Move backward: move the selection one step back in z-order.
  • Move to back: move the selection to the back of the z-order.

通常包括两个互补操作:

The two complementary operations are usually included:

  • 向前移动:将选择按 z 顺序向前移动一个步骤.
  • 移动到最前面:将选择内容移动到 z 顺序的最前面.
  • Move forward: move the selection one step forward in z-order.
  • Move to front: move the selection to the front of the z-order.

GraphPanel 说明了几种Java命中测试和处理多项选择的技术2D它说明了一种更简单的情况,其中至少有 some 个对象可见.它的渲染顺序是由使用ArrayList的简单List<Node>模型定义的,这对于重新排序并不理想;考虑将LinkedList作为List接口的替代实现.

GraphPanel illustrates several techniques for hit-testing and handling multiple selection in Java 2D. It illustrates the simpler case in which at least some of the object is visible. Its rendering order is defined by a simple List<Node> model using ArrayList, which is not ideal for re-ordering; consider LinkedList as an alternative implementation of the List interface.

这篇关于Java拖曳图片更好&amp;重叠图像检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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