0

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 );
			}
				
		}		
		
	}
}
2
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by jacline
0

You are using a GridLayout, so it seems to me that it's fairly easy to get the coordinates. The width of a button is the width of the panel divided by the number of buttons per row (4). The height of a button is the height of the panel divided by the number of buttons per column (4). So if the panel is 480 x 480 (subtracting a bit from 500 x 500, but get an exact value of the panel's width and height), each button is 120 x 120. So the upper-left coordinates of the button at location (2, 3) would be (120 * 2, 120 * 3) = (240, 360) relative to the upper-left coordinate of the panel that holds them. The lower right coordinates can be obtained by adding 120 to the upper left:

(240 + 120, 360 + 120) = (360, 480).

So the button at (2,3) is bounded by (240, 360), (360, 480)

All measurements are relative to the panel, not the JFrame.

0

Actually, you can do the above, but it's probably better to get the info directly from the button itself.

Point p = prizeButton.getLocation();
        System.out.println (p.x);
        System.out.println (p.y);
        System.out.println (prizeButton.getWidth());
        System.out.println (prizeButton.getHeight());
0

But there is also JLabel here how can I find the size of panel:S

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.