Results 1 to 3 of 3
Thread: button coordinates
- 04-05-2009, 08:02 PM #1
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 ); } } } }
- 04-05-2009, 08:49 PM #2
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.
Similar Threads
-
A Polygon class which accepts floating point coordinates
By soorena in forum New To JavaReplies: 2Last Post: 04-01-2009, 08:37 AM -
Conversion between polar and rectangular coordinates
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:55 PM -
Java3D: Clicking and getting coordinates?
By seabhcan in forum Advanced JavaReplies: 0Last Post: 01-11-2008, 02:46 PM -
Object locations via grid coordinates HELP.
By deadman_uk in forum New To JavaReplies: 4Last Post: 11-18-2007, 08:32 PM -
Arc2D.Double coordinates
By alley in forum Java 2DReplies: 2Last Post: 11-07-2007, 10:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks