a GUI application that presents a game based on a 4 by 4 matrix of buttons.One of the buttons (selected at random) "hides" the prize. A status bar at the top of the window shows the number of guesses. When the prize button is pressed, the status bar shows "You got it in x attempts!". While pressing buttons, the distance to the destination (x + y distance) is shown on the button.
this is my assigment and here is my code the problem is I cant find the coordinates of buttons so I cant find distance does anyone have suggestion about this?? thnks to all:)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class PotLuck extends JFrame implements ActionListener
{
ArrayList<JButton> buttons;
private int count;
Random prize;
private int prizeNo;
private JLabel countLabel;
JButton prizeButton;
public PotLuck()
{
count = 0;
prize = new Random();
buttons = new ArrayList<JButton>();
setLayout(new BorderLayout());
countLabel = new JLabel( "Guesses: " + count );
add(countLabel,BorderLayout.NORTH);
JPanel myPanel=new JPanel(new GridLayout(4,4));
add(myPanel,BorderLayout.CENTER);
setBounds( 0 , 0 , 500 , 500 );
setTitle( " PotLuck Game " );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
for( int i = 0; i < 16; i++ )
{
buttons.add( new JButton(" "+ (i+1) + " "));
myPanel.add( buttons.get(i) );
(buttons.get(i)).addActionListener( this );
}
prizeNo = prize.nextInt(16) + 1;
prizeButton = buttons.get(prizeNo-1);
setVisible( true );
}
public void actionPerformed( ActionEvent e)
{
count++;
countLabel.setText("Guesses: " + count);
JButton curButton = (JButton)e.getSource();
if( prizeButton.equals( curButton ))
{
countLabel.setText("You got it in " + count + "attempts.");
for( int i = 0; i < buttons.size() ; i++ )
{
(buttons.get(i)).disable();
(buttons.get(i)).removeActionListener( this );
}
}
}
}