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:)
public class PotLuck extends JFrame implements ActionListener
private int count;
private int prizeNo;
private JLabel countLabel;
count = 0;
prize = new Random();
buttons = new ArrayList<JButton>();
countLabel = new JLabel( "Guesses: " + count );
JPanel myPanel=new JPanel(new GridLayout(4,4));
setBounds( 0 , 0 , 500 , 500 );
setTitle( " PotLuck Game " );
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)
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)).removeActionListener( this );
What are you saying I dont understand???
I'm not sure what agits... post represents, perhaps a russion advertisement for porn. Whatever it means, I"m sure that it has absolutely nothing to do with your post or your question.
As to your problem, there are probably several ways to solve it. Myself, I'd use a 2 dimensional array of JButtons, 4 by 4 buttons. Before the game starts, you would select a prizeButton and note its row and column coordinates in the array. Then when any button is pressed, check the row and column coordinates in the array and compare this to that of the prize button. If the same, then it is the prize button, and if different, it's easy enough to calculate x and y numbers.