2012-05-24 35 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