Java drag image better & Overlapping image detection

廉价感情. 提交于 2019-12-23 22:03:45

问题


I have two questions for you:

  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.

  2. [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

        }
    });

Any feedback is appreciated.

Thanks.


回答1:


I am actually using Graphics to draw an image.

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:

  • 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:

  • 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 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.




回答2:


One type of solution is how I've solved this for a chess board. The board is composed of a grid of JPanels that each can hold one or 0 JLabels (that hold the chess piece with an image). If I click on a JPanel that holds a JLabel, the JLabel is kicked up onto the top level window's glasspane and then dragged with the mouse. When the mouse is released, the MouseListener detects what JPanel of the grid I'm over, the chess engine determines if this is a valid move, and if so, the JLabel is added to the JPanel that the mouse cursor is over. If not, the JLabel goes back to its original JPanel. The JPanels use GridBagLayout, and so if a JLabel is added to it, it is displayed centrally in the JPanel cell.



来源:https://stackoverflow.com/questions/11439139/java-drag-image-better-overlapping-image-detection

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