4

私のjinternalframesを両方の世界で最高にする簡単な方法はありますか?

  • jdesktoppaneの一部として機能させたいときに埋め込まれます
  • また、移動可能で、独自のイベントを処理できます

ここに画像の説明を入力してください

うまくいけば、この写真がお役に立てば幸いです。現在、アイテムをプレーヤーにドラッグするコードがあり、次に使用可能なバックパックスロットに挿入されますが、任意のバックパックスロットにドラッグできるようにしたいのですが、ご覧のとおり、下にペイントされています。これを改善するための迅速で汚い方法はありますか?私のJDesktopPaneの上部には、すべてがペイントされるパネルがあります(もちろん、jinternalframesを除く)。すべてのコードで申し訳ありませんが、SSCCEEはありません。私の論理を示したほうがいいと思っただけです。

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

}

提案のために私がJLayeredPaneをいじり回しているところを編集してください。

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

2 に答える 2

2

DragLabelOnLayeredPaneChessBoard使用する良い例ですJLayeredPane

GlassPaneDemoガラス板上でのドラッグを示しています。

JComponent#setComponentPopupMenu()コンテキストメニューを追加する方法になります。

于 2013-03-27T12:28:37.170 に答える
2

これを解決するために、アイテムが(draggingItem boolean)状態にあるときに常に更新するJLabelを作成しました。ここにいくつかのコードがあります。すべての助けとアイデアをありがとう...

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

mouseReleasedについても同じですが、JLabelを削除します。また、私のスクリーンショットでわかるように、画像はまだありません。テキストだけです。これを非常に高速にコーディングするだけなので、他の人を助けるためにここで答えを提供できます...

ここに画像の説明を入力してください

于 2013-03-28T15:01:50.313 に答える