4

有沒有簡單的方法來有我jinternalframes是兩全其美JInternalFrames聽的JDesktopPane事件

  • 嵌入式的時候我希望他們充當的JDesktopPane的一部分
  • 還可以移動,並且能夠處理自己的事件

enter image description here

希望這幅畫將助陣。現在我有我的代碼將項目拖動到您的播放器,並將其插入到下一個可用的揹包插槽中,但我也希望能夠將其拖到任何揹包插槽中,但是您可以在下面看到它。有沒有任何快速和骯髒的方法來解決這個問題?我的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); 
+0

看起來好像拖放將是你最好的選擇。 – 2013-03-27 03:01:39

+0

有意義...你能詳細說明一下嗎?我不得不承認我沒有使用拖放代碼的經驗。可能是我可能讀過的一個示例或另一篇文章?感謝您的反饋,順便說一句。 – KisnardOnline 2013-03-27 03:03:51

+1

我希望我有時間寫一個例子。這是Swing更復雜的功能之一,至少對我而言,我會向您推薦教程,並且還會在本網站上搜索示例。這就是我過去爲此所做的。 – 2013-03-27 03:09:14

回答

2

DragLabelOnLayeredPaneChessBoard是使用JLayeredPane很好的例子。

GlassPaneDemo顯示拖動在玻璃板上。

JComponent#setComponentPopupMenu()將是一種添加上下文菜單的方式。

+0

我試圖使用JLayeredPane的概念,並沒有太多的運氣......現在我只是試圖繪製一個隨機JLabel以上我的遊戲地圖,並不能得到它的工作..我已經公佈上面的代碼...任何線索? – KisnardOnline 2013-03-28 01:07:40

+0

我認爲您需要爲圖層使用_different_數字,並且'我通常會忘記執行'setBounds()'。 – 2013-03-28 11:56:12

+0

除了拖畫(在這種情況下,使用jlabel進行測試)之外,一切應該在我的遊戲的同一圖層上。默認圖層低於拖動圖層(最高)。所以setBounds()是爲什麼它不工作,你認爲? – KisnardOnline 2013-03-28 13:31:11

2

爲了解決這個問題,我創建了一個JLabel,當物品處於我的(draggingItem布爾)狀態時,我總是會更新它。這是一些代碼。感謝大家所有的幫助和想法......爲的mouseReleased

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

同樣的事情,我將只是刪除的JLabel。此外,你可以在我的截圖看有沒有照片呢,只是文本,只是這個代碼實快,所以我可以在這裏提供的答案,幫助他人......

enter image description here

+0

+1看起來不錯!這是背景中的一個'TexturePaint'嗎? – 2013-03-29 12:11:04

+0

哈哈哈不,不幸的是,這和我的藝術技能一樣好...查看我的網站,如果你想:) www.kisnardonline.com – KisnardOnline 2013-03-29 13:58:50