2012-05-24 37 views
0

林使遊戲那裏有一個方法稱爲移動(INT X1,INT Y1,INT X2,INT Y2),其移動從所述第一按鈕(X1,Y1)和第二個按鈕的地方它(X2,Y2的片)我怎麼能傳遞一個JButton我點擊到第二的JButton我點擊的一個組成部分?

如何我寫的方法,當我按第一,他等待第二應用移動的GUI? PS(按鈕的圖標應該改變,他的動作完成)

這是我寫到目前爲止:

package eg.edu.guc.dvonn.gui; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import java.awt.Image; 
import java.awt.TextArea; 
import java.awt.TextField; 

import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 



import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

import eg.edu.guc.dvonn.engine.Board; 


public class FirstWindow extends JFrame implements ActionListener, MouseListener { 
    JButton Startbutton; 
    JPanel welcomePanel; 
    JPanel SecondPanel; 
    JPanel StandardPanel; 
    JPanel custPanel; 
    JPanel panelFill; 
    JButton Standard; 
    JButton fill; 
    JButton put; 
    JButton clear; 
    JButton cust; 
    JButton beginGame; 
    JButton clickedButton = null; 
    JLabel label; 
    JLabel put2; 
    TextField rows; 
    TextField col; 
    Board b; 
    JButton[][] button; 
    int r1; 
    int cc; 

    Icon red = new ImageIcon(getClass().getResource("index.jpg")); 
    Icon Black=new ImageIcon(getClass().getResource("black.jpg")); 
    Icon white= new ImageIcon(getClass().getResource("images.jpg")); 



     public FirstWindow(){ 

      setSize(800,600); 
      setVisible(true); 
      setLayout(new BorderLayout()); 

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       welcomePanel = new JPanel(); 
       welcomePanel.setLayout(new BorderLayout()); 
       label = new JLabel("Welcome to Dvonn"); 
       welcomePanel.add(label, BorderLayout.NORTH); 
       welcomePanel.setVisible(true); 

      label.setForeground(Color.RED); 
      label.setFont(new Font("Serif", Font.PLAIN, 40)); 


      Startbutton = new JButton ("Start"); 
      Startbutton.setBackground(Color.BLACK); 
      Startbutton.setForeground(Color.RED); 
      Startbutton.setFont(new Font("Times New Roman", Font.PLAIN, 40)); 
      Startbutton.setPreferredSize(new Dimension(120,40)); 

      welcomePanel.add(Startbutton, BorderLayout.CENTER); 

      add(welcomePanel); 

      Startbutton.addActionListener(this); 


      SecondPanel = new JPanel(); 
      SecondPanel.setVisible(false); 
      SecondPanel.setSize(400,400); 
       rows = new TextField(); 
       col= new TextField(); 
       SecondPanel.add(rows); 
       SecondPanel.add(col); 

       fill = new JButton("Fill random"); 
       fill.addActionListener(this); 
       fill.setBackground(Color.LIGHT_GRAY); 
       fill.setForeground(Color.BLUE); 

      /*  
       put = new JButton("Put"); 
       put.setBackground(Color.LIGHT_GRAY); 
       put.setForeground(Color.BLUE); 
      */ 
       put2 = new JLabel("Place Pieaces Manually on the Board or Choose Fill Random"); 
       put2.setBackground(Color.LIGHT_GRAY); 
       put2.setForeground(Color.BLUE); 

       clear = new JButton("Clear Board"); 
       clear.addActionListener(this); 
       clear.setBackground(Color.LIGHT_GRAY); 
       clear.setForeground(Color.BLUE); 

       beginGame = new JButton("Start The Game"); 
       beginGame.addActionListener(this); 
       beginGame.setBackground(Color.LIGHT_GRAY); 
       beginGame.setForeground(Color.BLUE); 
       beginGame.setVisible(false); 

       Standard = new JButton("Standardised board"); 
       Standard.setBackground(Color.LIGHT_GRAY); 
       Standard.setForeground(Color.BLUE); 
       Standard.addActionListener(this); 
       Standard.addMouseListener(this); 


       cust = new JButton("customized"); 
       cust.setBackground(Color.LIGHT_GRAY); 
       cust.setForeground(Color.BLUE); 
       cust.addActionListener(this); 
       cust.addMouseListener(this); 

       panelFill = new JPanel(); 

       panelFill.add(fill); 
     // panelFill.add(put2); 
       panelFill.add(clear); 
       panelFill.add(beginGame); 
       SecondPanel.add(Standard); 
       SecondPanel.add(cust); 



       this.r1 =5; 
       this.cc = 11; 
       b = new Board(r1,cc); 
       button = new JButton [r1][cc]; 
       StandardPanel = new JPanel(); 
       StandardPanel.setBackground(Color.WHITE); 
       StandardPanel.setLayout(new GridLayout(r1, cc)); 
       for (int i = 0; i <= r1 - 1; i++) { 
        for (int j = 0; j <= cc - 1; j++) { 
         button[i][j] = new JButton("Place"); 
         button[i][j].addActionListener(this); 
        } 
       } 
       for (int i = 0; i <= r1 - 1; i++) { 
        for (int j = 0; j <= cc - 1; j++) { 

         button[i][j].setLayout(new FlowLayout()); 
         button[i][j].setBackground(Color.WHITE); 


         StandardPanel.add(button[i][j], i, j); 



        } 
       } 



     } 

     public FirstWindow(int row,int cols){ 

      setSize(800,600); 
      setVisible(true); 
      setLayout(new BorderLayout()); 

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


       fill = new JButton("fill random"); 
       fill.addActionListener(this); 
       fill.setBackground(Color.LIGHT_GRAY); 
       fill.setForeground(Color.BLUE); 

      /* 
       put = new JButton("put"); 
       put.setBackground(Color.LIGHT_GRAY); 
       put.setForeground(Color.BLUE); 
      */ 
       put2 = new JLabel("Place Pieaces Manually on the Board or Choose Fill Random"); 
       put2.setBackground(Color.LIGHT_GRAY); 
       put2.setForeground(Color.BLUE); 

       clear = new JButton("Clear Board"); 
       clear.addActionListener(this); 
       clear.setBackground(Color.LIGHT_GRAY); 
       clear.setForeground(Color.BLUE); 

       Standard = new JButton("Standardised board"); 
       Standard.setBackground(Color.LIGHT_GRAY); 
       Standard.setForeground(Color.BLUE); 
       Standard.addActionListener(this); 
       Standard.addMouseListener(this); 

       cust = new JButton("customized"); 
       cust.setBackground(Color.LIGHT_GRAY); 
       cust.setForeground(Color.BLUE); 
       cust.addActionListener(this); 
       cust.addMouseListener(this); 

       panelFill = new JPanel(); 
       panelFill.add(fill); 
     // panelFill.add(put2); 
       panelFill.add(clear); 





      this.r1 = row; 
      this.cc = cols; 



       b = new Board(r1,cc); 


       button = new JButton [r1][cc]; 

       custPanel = new JPanel(); 
       custPanel.setBackground(Color.WHITE); 
       custPanel.setLayout(new GridLayout(r1, cc)); 

       for (int i = 0; i <= r1 - 1; i++) { 
        for (int j = 0; j <= cc - 1; j++) { 
         button[i][j] = new JButton("Place"); 
         button[i][j].addActionListener(this); 
        } 
       } 
       for (int i = 0; i <= r1 - 1; i++) { 
        for (int j = 0; j <= cc - 1; j++) { 

         button[i][j].setLayout(new FlowLayout()); 
         button[i][j].setBackground(Color.WHITE); 
         custPanel.add(button[i][j], i, j); 

        } 
       } 
       add(custPanel); 
       add(panelFill,BorderLayout.SOUTH); 


     } 
     public boolean checkBoard() { // checks if the board is full or not to start the game and the move process 
     // boolean fullBoard = false; 
      for (int i = 0; i < this.r1; i++) { 
       for (int j = 0; j < this.cc; j++) { 
        if(button[i][j].getIcon() == null) { 
         return true; 
        } 
       } 

      } 
      return false; 
     } 

     public static void main (String [] args){ 
      FirstWindow window = new FirstWindow(); 



     } 
     @Override 
     public void actionPerformed(ActionEvent e) { 

      if (e.getSource()==Startbutton){ 

       this.remove(welcomePanel); 
       this.add(SecondPanel); 
       SecondPanel.setVisible(true); 
       //this.add(SecondPanel); 

       JLabel label1 = new JLabel("dvonn"); 
       label1.setForeground(Color.RED); 
       label1.setFont(new Font("Serif", Font.PLAIN, 25)); 
       SecondPanel.add(label1); 

       } 

       if(e.getSource()== cust){ 
        String rowText = rows.getText(); 
        String colText = col.getText(); 

        r1 = Integer.parseInt(rowText); 
        cc = Integer.parseInt(colText); 
        this.setVisible(false); 

       FirstWindow custom = new FirstWindow(r1, cc); 




       } 
         if(e.getSource()==Standard){ //places the standard panel 
         SecondPanel.setVisible(false); // set the second panel invisible 
         this.add(StandardPanel); //to add the panel with the buttons 
         StandardPanel.setVisible(true); 

         add(put2, BorderLayout.NORTH); 
         add(panelFill,BorderLayout.SOUTH); 
         add(StandardPanel,BorderLayout.CENTER); 

        } 

       if (e.getSource() == fill) { //randomly fills the board 
         b.fillRandom(); 

        for (int i = 0; i <=r1-1; i++) { 
        for (int j = 0; j <=cc-1; j++) { 
         if (b.getColor(i, j)==0){ 
          button[i][j].setIcon(red); 

         } else if (b.getColor(i, j) == 1) { 
          button[i][j].setIcon(white); 
         } else if (b.getColor(i, j) == 2) { 
          button[i][j].setIcon(Black); 
         } 
        } 
        } 

        beginGame.setVisible(true); 

        } 


         if (e.getActionCommand().equals("Place")) { 

         // u press on the button to manually place a piece 
          for(int i=0; i< this.r1; i++){ 

          for (int j = 0; j < cc; j++) { 
           if(e.getSource() == button[i][j]){ 
           b.put(i, j); 
           if (b.getColor(i, j) == 0) { 
            button[i][j].setText(""); 
            button[i][j].setIcon(red); 
            if (checkBoard()) { 
            beginGame.setVisible(true); 
            } 
            break; 
           } else { 
           if (b.getColor(i, j) == 1) { 
            button[i][j].setText(""); 
            button[i][j].setIcon(white); 
            if (checkBoard()) { 
             beginGame.setVisible(true); 
            } 
            break; 
           } else { 
            button[i][j].setText(""); 
            button[i][j].setIcon(Black); 
            if (checkBoard()) { 
             beginGame.setVisible(true); 
            } 
            break; 

          } 
         } 
        } 
        } 
         } 
         } 

       if (e.getSource() == clear) { 

        for (int i = 0; i <= r1 - 1; i++) { 
         for (int j = 0; j <= cc - 1; j++) { 
         button[i][j].setIcon(null); 
         if (button[i][j].getText().equals("")) { 
          button[i][j].setText("Place"); 
         } 
         } 
        } 

       } 

       if (e.getSource() == beginGame) { //begins the move part 
        b.currentPhase = 2; 
        panelFill.setVisible(false); 
        remove(panelFill); 

       } 


      } 

     public void mouseClicked(MouseEvent e) { //move mouselistener 

      if (b.getCurrentPhase() == 2) { 

      if (clickedButton == null) { 

        clickedButton = button[e.getX()][e.getY()]; 
       } 
       else { 
        // second button, so do the 'move' 
        int x1 = clickedButton.getX(); 
        int y1 = clickedButton.getY(); 
        int x2 = e.getX(); 
        int y2 = e.getY(); 
        b.move(x1, y1, x2, y2); 
        if (button[x1][y1].getIcon() == red) { 
         button[x2][y2].setIcon(red); 
         button[x1][y1].setIcon(null); 
        } 
        if (button[x1][y1].getIcon() == white) { 
         button[x2][y2].setIcon(white); 
         button[x1][y1].setIcon(null); 
        }if (button[x1][y1].getIcon() == Black) { 
         button[x2][y2].setIcon(Black); 
         button[x1][y1].setIcon(null); 
        } 
        // clear the clickedButton so that we know the next click is a first click 
        clickedButton = null; 
       } 
      } 


     } 

     @Override 
     public void mouseEntered(MouseEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mouseExited(MouseEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mousePressed(MouseEvent e) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void mouseReleased(MouseEvent e) { 
      // TODO Auto-generated method stub 

     } 


}  
+1

可以顯示更多的代碼?該方法採取了2鼠標事件嚇跑了我。 –

+0

LOOL我做出這樣的方法,我只是想將明白的問題 – Tharwat7

回答

1

你應該有保存的第一個按鈕的引用的全局變量點擊該按鈕,然後在單擊第二個按鈕時使用該按鈕。例如...

JButton clickedButton = null; 

public void mouseClicked(MouseEvent e){ 
    if (clickedButton == null){ 
     // first button clicked - so remember the button 
     clickedButton = (JButton)e.getSource(); 
    } 
    else { 
     // second button, so do the 'move' 
     JButton secondButton = (JButton)e.getSource(); 
     doMove(clickedButton,secondButton); 

     // clear the clickedButton so that we know the next click is a first click 
     clickedButton = null; 
    } 
} 

在這段代碼中,我們有一個JButton全局變量。當這個變量是null,我們知道用戶還沒有點擊任何東西。當變量有一個值時,我們知道用戶已經點擊了第一個按鈕,這是這個變量的值。

所以,在觸發事件mouseClicked()clickedButton變量的值,我們知道moveclickedButton到剛剛點擊的按鈕(第二個按鈕)。


請注意,我們正在鑄造e.getSource()JButton沒有檢查,它實際上是一個JButton。我沒有在代碼中包含這個上面,因爲這將是複雜的,但你應該檢查它首先做這些類型轉換,這樣的事情之前...

Object sourceObject = e.getSource(); 
if (sourceObject instanceof JButton){ 
    JButton button = (JButton)sourceObject; 
    // other processing code 
} 

這將阻止它失敗如果事件在不是JButton的對象上觸發。


我希望這有點合理 - 讓我知道你是否需要解釋得更好。

+0

這是一個的MouseListener或一個ActionListener代碼?因爲如果用戶點擊不在JButton上的某個地方並且投射失敗會怎麼樣? –

+0

非常感謝生病嘗試,現在 – Tharwat7

+0

它是一種'MouseListener',因爲它接受一個'MouseEvent',但是我只是選擇了,因爲目前的用戶代碼。我同意你應該在轉換之前檢查Object類型是'JButton',但是我沒有包含它,因爲它可能會使代碼複雜化。我編輯了我的答案,在類型檢查演員時加入了一節。 – wattostudios