Results 1 to 3 of 3
  1. #1
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Talking button coordinates

    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:)
    Java Code:
    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. #2
    jacline's Avatar
    jacline is offline Member
    Join Date
    Mar 2009
    Location
    Turkey
    Posts
    13
    Rep Power
    0

    Default

    What are you saying I dont understand???

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. Replies: 2
    Last Post: 04-01-2009, 09:37 AM
  2. Replies: 0
    Last Post: 04-16-2008, 11:55 PM
  3. Java3D: Clicking and getting coordinates?
    By seabhcan in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2008, 03:46 PM
  4. Object locations via grid coordinates HELP.
    By deadman_uk in forum New To Java
    Replies: 4
    Last Post: 11-18-2007, 09:32 PM
  5. Arc2D.Double coordinates
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-07-2007, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •