2016-11-18 36 views
0
public class buttonActionStart implements ActionListener {  // overwritten ActionListener 

     public void actionPerformed (ActionEvent e){ 
     JFrame window2 = new JFrame("Main Game"); //create empty frame 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     window2.setSize(screenSize.width,screenSize.height); 
     window2.add(new JPanel(){ 

     public void paintComponent(Graphics graph){ 
      super.paintComponent(graph); 

      Board board = new Board(); 

      graph.drawString(board.sqaures[1].getName(),440,560); 
      graph.drawString(board.sqaures[2].getName(),335,560); 
      graph.drawString(board.sqaures[3].getName(),225,560); 
      graph.drawString(board.sqaures[4].getName(),145,560); 
      graph.drawString(board.sqaures[5].getName(),30,560); 
      graph.drawString(board.sqaures[6].getName(),25,460); 
      graph.drawString(board.sqaures[7].getName(),25,360); 
      graph.drawString(board.sqaures[8].getName(),35,260); 
      graph.drawString(board.sqaures[9].getName(),25,160); 
      graph.drawString(board.sqaures[10].getName(),25,60); 
      graph.drawString(board.sqaures[11].getName(),140,60); 
      graph.drawString(board.sqaures[12].getName(),240,60); 
      graph.drawString(board.sqaures[13].getName(),340,60); 
      graph.drawString(board.sqaures[14].getName(),440,60); 
      graph.drawString(board.sqaures[15].getName(),530,60); 
      graph.drawString(board.sqaures[16].getName(),550,160); 
      graph.drawString(board.sqaures[17].getName(),545,260); 
      graph.drawString(board.sqaures[18].getName(),540,360); 
      graph.drawString(board.sqaures[19].getName(),550,460); 

      for (int i = 0; i < 6; i++) 
       graph.drawRect(10 + (i*100),10,100,100); 
      for (int j = 0; j < 4; j++) 
       graph.drawRect(10,110 + (j*100),100,100); 
      for (int k = 0; k < 4; k++) 
       graph.drawRect(510,110 + (k*100),100,100); 
      for (int l = 0; l < 6; l++) 
       graph.drawRect(10 + (l*100),510,100,100); 

      InputStream resource = UI.class.getResourceAsStream("player1.png"); 
      Image player1Image = null; 
      try{ 
       player1Image = ImageIO.read(resource); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 

      resource = UI.class.getResourceAsStream("player2.png"); 
      Image player2Image = null; 
      try{ 
       player2Image = ImageIO.read(resource); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 

      resource = UI.class.getResourceAsStream("player3.png"); 
      Image player3Image = null; 
      try{ 
       player3Image = ImageIO.read(resource); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 

      resource = UI.class.getResourceAsStream("player4.png"); 
      Image player4Image = null; 
      try{ 
       player4Image = ImageIO.read(resource); 
      }catch (IOException e){ 
       e.printStackTrace(); 
      } 

      graph.drawImage(player1Image, 511, 511, null); 
      graph.drawImage(player2Image, 585, 510, null); 
      graph.drawImage(player3Image, 511, 585, null); 
      graph.drawImage(player4Image, 585, 585, null); 
      } 
     }); 
     window2.setVisible(true); 
    } 
} 

我正在開發一種棋盤遊戲,這四個圖像將作爲玩家圖標。我希望玩家手動點擊分配給他們的圖像,然後點擊屏幕上的其他位置,圖像將「更新」到該位置。如何將mouselistener添加到使用graph.drawimage()插入的圖像?

「廣場」和板」將是我自定義的類。

我有另外一個窗口,這將有一個‘開始’按鈕,觸發上述圖形要在新窗口中繪製。

我發現很多人建議使用的JLabel添加圖片和添加的JLabel到JFrame,但我發現,圖像將覆蓋我的圖形

+2

對於[本尊]名稱:停止閱讀「paintComponent」中的圖像 - 您的體驗將是極端緩慢的動作。閱讀一次,然後根據需要多次塗刷它們,但每次組件需要痛苦時都停止閱讀! –

+0

對不起,你能解釋一下嗎?我不完全明白你的意思。你的意思是閱讀paintComponent()中的圖像是不好的方法嗎?我想進一步問,在將JLabel直接添加到JFrame之前,是否需要將JLabel添加到JPanel? –

+0

'你的意思是閱讀paintComponent()中的圖像是不好的方法嗎?' - 是的。繪畫方法僅用於繪畫,沒有別的。應該在您的類的構造函數中讀取圖像,以便在創建類時只讀取一次。所以不要創建Board類。 – camickr

回答

0

兩種選擇:

  1. d o按照建議使用JLabel。通過說「圖像將覆蓋我的圖形」,您可能意味着圖像的透明部分不會顯示背景。如果是這樣,請致電JLabel.setOpaque(false)
  2. 將MouseListener添加到您爲其實施paintComponent()方法的組件(可能是JFrame實現)。只要用戶點擊組件,您就會獲得鼠標事件。在偵聽器實現中,您必須手動檢查座標是否落入其中一個圖像的區域。

一般來說,對於遊戲,我更喜歡選項2:您使用空白畫布並將您的遊戲場景繪製到畫布中。用戶交互然後由通用遊戲邏輯對象管理,該對象根據座標/按下的鍵/等委託給諸如「玩家」,「NPC」等的遊戲對象。

+0

你是什麼意思的透明部分?我的意思是,我的形象很小(25 * 25就像一個小圖標)。你的意思是JLabel中的空閒位置? –

+0

我認爲你的圖像具有透明像素。如果沒有,並且使用JLabel時不僅僅覆蓋圖像區域,那麼LayoutManager會將JLabel的大小設置爲錯誤。在這種情況下,請查看Java Swing佈局以及如何使用LayoutManagers,以便JLabel不會調整大於其首選大小。 如果您不知道這意味着什麼,請使用JLabel爲您的版本創建一個最小示例,併爲該示例發佈另一個SO問題。 –

+0

默認情況下,JLabel不透明。 – camickr