4

Is there any easy way to have my jinternalframes be the best of both worlds

  • embedded when I want them to act as part of jdesktoppane
  • also movable and able to handle their own events

enter image description here

Hopefully this picture will help out. Right now I have my code to drag the item to your player and it inserts into next available backpack slot, but I also want to be able to drag it to any of the backpack slots, but as you can see it paints below. Is there any quick and dirty way to remedy this? My JDesktopPane has a panel on top that is where everything is painted to(besides the jinternalframes of course). Sorry for all of the code and no SSCCEE. Just felt it would better to show my logic.

theDesktop.addMouseMotionListener(new MouseMotionListener() {
    @Override
    public void mouseDragged(MouseEvent e) {
        updateMouseCursor(e);
        if (MapGamePanel.draggingItem){
            Point point = e.getPoint();
            MapGamePanel.currentX = point.x;
            MapGamePanel.currentY = point.y;
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        updateMouseCursor(e);
    }
});

theDesktop.addMouseListener(new MouseListener() {
    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1){ //left
            int whiteLeftSpace = (theDesktop.getWidth() - xTiles*32)/2;
            boolean inGamePanel = e.getX() > whiteLeftSpace && e.getX() < (xTiles*32 + whiteLeftSpace) && e.getY() < yTiles*32;
            String globalCoords = localToGlobalCoords((e.getX()-whiteLeftSpace)/32 + "," + e.getY()/32);
            if (inGamePanel){
                //looking for tiles with no mobs or players and loot
                String[] globalCoordsSplit = globalCoords.split(",");
                int globalX = Integer.parseInt(globalCoordsSplit[0]);
                int globalY = Integer.parseInt(globalCoordsSplit[1]);
                if ((!(globalX == characterX && globalY == characterY)) && //not under character
                        ((globalX-1) == characterX || globalX == characterX || (globalX+1) == characterX) && //(+/-) 1 xTile
                        ((globalY-1) == characterY || globalY == characterY || (globalY+1) == characterY)){ //(+/-) 1 yTile
                    HashMap <String, String> dropsToDrag = new HashMap <String, String>();
                    Boolean mobPresent = false;
                    Boolean playerPresent = false;
                    if (MapGamePanel.entityInfoHashTable.containsKey(globalCoords)){ //no mobs/npcs
                        mobPresent = true;
                    }
                    Iterator<Entry<String, String>> it = MapGamePanel.entityInfoHashTable.entrySet().iterator();
                    while (it.hasNext()) {
                        Entry<String, String> pairs = it.next();
                        String key = pairs.getKey();
                        if (!key.contains(",") && !key.contains("-")){
                            String[] values = pairs.getValue().split("\\|");
                            String tempCoords = values[0];
                            if (globalCoords.equals(tempCoords)){ //player on spot
                                playerPresent = true;
                            }
                        } else if (key.contains("-")){
                            String[] splitKey = key.split("-");
                            if (splitKey[0].equals(globalCoords)){
                                dropsToDrag.put(key, pairs.getValue());
                            }
                        }
                    }
                    int smallEntityId = Integer.MAX_VALUE; //2147483647
                    if (!mobPresent && !playerPresent && !dropsToDrag.isEmpty()){
                        Iterator<Entry<String, String>> it2 = dropsToDrag.entrySet().iterator();
                        while (it2.hasNext()) {
                            Entry<String, String> pairs = it2.next();
                            String[] keyWithPK = pairs.getKey().split("-");
                            String tmpCoords = keyWithPK[0];
                            String[] coordsSplit = tmpCoords.split(",");
                            int tempX = Integer.parseInt(coordsSplit[0]);
                            int tempY = Integer.parseInt(coordsSplit[1]);
                            int tmpEntityId = Integer.parseInt(keyWithPK[1]);
                            String[] values = pairs.getValue().split("\\|");
                            String tmpId = values[0];
                            int tmploot_amt = Integer.parseInt(values[1]);
                            String tmploot_filename = values[2];
                            if (tmpEntityId < smallEntityId){
                                smallEntityId = tmpEntityId;
                                MapGamePanel.dragItemXCoord = tempX;
                                MapGamePanel.dragItemYCoord = tempY;
                                MapGamePanel.dragItemEntityId = tmpEntityId;
                                MapGamePanel.dragItemId = tmpId;
                                MapGamePanel.dragItemAmt = tmploot_amt;
                                MapGamePanel.draggingItemFilename = tmploot_filename;
                            }
                        }
                        MapGamePanel.draggingItem = true;
                        Point point = e.getPoint();
                        MapGamePanel.startX = point.x;
                        MapGamePanel.startY = point.y;  
                    }
                }
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3){ //right
            movementBitKeys.keyReleased(87);
            movementBitKeys.keyReleased(68);
            movementBitKeys.keyReleased(83);
            movementBitKeys.keyReleased(65);
            mouseHeld = false;
        }
        if (MapGamePanel.draggingItem){
            int whiteLeftSpace = (theDesktop.getWidth() - xTiles*32)/2;
            String[] globalCoords = localToGlobalCoords((MapGamePanel.currentX-whiteLeftSpace)/32 + "," + MapGamePanel.currentY/32).split(",");
            int globalX = Integer.parseInt(globalCoords[0]);
            int globalY = Integer.parseInt(globalCoords[1]);

            String[] startCoords = localToGlobalCoords((MapGamePanel.startX-whiteLeftSpace)/32 + "," + MapGamePanel.startY/32).split(",");
            int startX = Integer.parseInt(startCoords[0]);
            int startY = Integer.parseInt(startCoords[1]);
            if (globalX == characterX && globalY == characterY){
                    sendToServer("pickupItem|" + startX + "," + startY + "-" + MapGamePanel.dragItemEntityId + "|backpack|-1|" + MapGamePanel.dragItemAmt);
            } else if (((globalX-1) == characterX || globalX == characterX || (globalX+1) == characterX) &&
                    ((globalY-1) == characterY || globalY == characterY || (globalY+1) == characterY)){
                if (!(startX == globalX && startY == globalY)){
                    sendToServer("moveItem|" + startX + "," + startY + "-" + MapGamePanel.dragItemEntityId + "|ground|" + globalX + "," + globalY + "|-1|" + MapGamePanel.dragItemAmt);
                }
            }
            MapGamePanel.draggingItem = false;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}
});

}

EDIT where I mess around with JLayeredPane due to the suggestions.

public static JFrame frame;
public static JLayeredPane theDesktop;
public static MapGamePanel gamePanel; //where all my game tiles draw
    ....//lines removed
theDesktop = new JDesktopPane();
theDesktop.setOpaque(false);
theDesktop.add(backpackFrame, JLayeredPane.DEFAULT_LAYER);
theDesktop.add(gamePanel, JLayeredPane.DEFAULT_LAYER);
theDesktop.add(new JLabel("THIS SHOULD SHOW UP ABOVE THE OTHER CRAP, but does not"), JLayeredPane.DRAG_LAYER);
    ....//lots of lines removed
frame.getContentPane().add(theDesktop);
KisnardOnline
  • 653
  • 4
  • 16
  • 42
  • It looks as though drag and drop would be your best option here. – Hovercraft Full Of Eels Mar 27 '13 at 03:01
  • makes sense... can you elaborate? I have to admit I have no experience with drag and drop code. possibly an example or another post that I may take a read on? thanks for the feedback already, btw. – KisnardOnline Mar 27 '13 at 03:03
  • 1
    I wish I had the time to write an example. It is one of the more complex Swing features, at least for me, and I will refer you to the tutorials and also that you search out examples on this site. That is what I've done in the past for this. – Hovercraft Full Of Eels Mar 27 '13 at 03:09
  • Will do, thanks for the help with where to start :D. I am about to get some rest so if you have time later please feel free to write that example ;) – KisnardOnline Mar 27 '13 at 03:13
  • Not likely going to happen any time soon. Job responsibilities are oppressive right now. – Hovercraft Full Of Eels Mar 27 '13 at 03:15
  • You could 1- use the frames glass pane; 2- use a transparent component that is placed at the top level of the desktop (over all othe other content) – MadProgrammer Mar 27 '13 at 03:23
  • looked at the drag and drop and now I cant sleep, very glad you have other ideas... are there any examples you could share or link me to? so what you are saying is to paint my regular game to where it is and then paint when i drag items on the glass/transparent pane/component? – KisnardOnline Mar 27 '13 at 03:38
  • @MadProgrammer I tried to use JLayeredPane concept and am not having much luck... for now I am only just trying to paint a random JLabel above my game map and cant get it to work.. I have posted the code above... any clue? – KisnardOnline Mar 28 '13 at 01:18
  • Looking at the way you write your code... I seriously recommend that you add comments; otherwise, when you come back at it, even as soon as after a few months, it'll be very hard to figure out what it does (and how). – Radu Murzea Mar 28 '13 at 15:05
  • I appreciate the sentiment, but I have plenty of refactoring to do and I actually remove most comments as the logic is self explanatory to me. Hopefully a lot will be moved to reusable functions, and thats why my variable names are logical(self commenting code). – KisnardOnline Mar 28 '13 at 20:06

2 Answers2

2

DragLabelOnLayeredPane and ChessBoard are good examples that use JLayeredPane.

GlassPaneDemo shows dragging on the glass pane.

JComponent#setComponentPopupMenu() would be a way to add a context menu.

Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • I tried to use JLayeredPane concept and am not having much luck... for now I am only just trying to paint a random JLabel above my game map and cant get it to work.. I have posted the code above... any clue? – KisnardOnline Mar 28 '13 at 01:07
  • I think you need to use _different_ numbers for the layers, and `I usually forget to do `setBounds()`. – Catalina Island Mar 28 '13 at 11:56
  • everything should be on the same layer for my game except the drag painting(in this case testing with jlabel). default layer is lower than drag layer(which is highest). so setBounds() is why it isn't working you think? – KisnardOnline Mar 28 '13 at 13:31
  • yes setBounds() was the issue. Thanks a million Catalina. I am marking this as accepted for your benefit and then when I come up with my solution I will post what I did for anybody's, who finds this post, benefit. – KisnardOnline Mar 28 '13 at 14:49
2

In order to solve this I have created a JLabel that I will always update for when an item is in my (draggingItem boolean) state. Here is some code. Thanks everyone for all of the help and ideas...

public void mouseDragged(MouseEvent e) {
    updateMouseCursor(e);
    if (MapGamePanel.draggingItem){
        Point point = e.getPoint();
        MapGamePanel.currentX = point.x;
        MapGamePanel.currentY = point.y;
        dragJLabel.setBounds(MapGamePanel.currentX, MapGamePanel.currentY, 32, 32);
    }
}

public void mousePressed(MouseEvent e) {
    MapGamePanel.startX = point.x;
    MapGamePanel.startY = point.y;
    dragJLabel = new JLabel(MapGamePanel.draggingItemFilename);
    dragJLabel.setBounds(MapGamePanel.startX, MapGamePanel.startY, 32, 32);
    theDesktop.add(dragJLabel, JLayeredPane.DRAG_LAYER);
}

Same thing for mouseReleased, I will just remove the JLabel. Also as you can see in my screenshot there is no image yet, just text, just codes this real fast so I could provide an answer here to help others...

enter image description here

KisnardOnline
  • 653
  • 4
  • 16
  • 42
  • hahaha no, unfortunately, that is about as good as my artistic skill gets... check out my site if you want :) www.kisnardonline.com – KisnardOnline Mar 29 '13 at 13:58