Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java GUI Help

Options
  • 01-11-2009 3:51pm
    #1
    Closed Accounts Posts: 338 ✭✭


    Alri lads.

    Got an assignment from college, think(or at least hope) that Im nearly finished. Having one or two problems and can't figure them out.

    The program is basically a guessing game. Choose one button, then another, if they are a match the you get 1 point, if they dont match, both flip back over and no point. I messed the counter up somehow and them flipping over doesnt work very well now.

    A mate helped me with this but I hate annoying him with more questions, so if anyone can help, thanks.
    /******************************************************/
    /*		Name : Alan McCormac						  */
    /*		Student Number : B00027337					  */
    /*		Group 3										  */
    /*													  */
    /*		This is a guessing game.the user presses	  */
    /*		any one of the ? buttons and is presented	  */
    /*		with an image. The user must then			  */
    /*		try find the match. If the user finds		  */
    /*		the correct image they are given 1 point.	  */
    /*		If they dont find the correct match, 		  */
    /*		the 2 buttons will reset to ? and 			  */
    /*		the user trys to find the match again.		  */
    /******************************************************/
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.* ;
    import java.lang.*;
    
    class guessGame extends JFrame implements ActionListener{
    
    
    	//Creating Variables
    	JPanel gamePanel, bottomPanel;
    
    	//Buttons
    	JButton buttonClub1, buttonDiamond1, buttonSpade1, buttonMinus1, buttonClub2, buttonHeart1, buttonMinus2, buttonPlus1, buttonSpade2, buttonHeart2, buttonDiamond2, buttonPlus2, checkButton, resetButton;
    
    	//Boolean values to check if pressed
    	Boolean button1_pressed, button2_pressed, button3_pressed, button4_pressed, button5_pressed, button6_pressed, button7_pressed, button8_pressed, button9_pressed, button10_pressed, button11_pressed, button12_pressed;
    
    	//Counter to record score
    	private int counter = 0;
    
    	private Boolean buttons_pressed[] = {false,false,false,false,false,false,false,false,false,false,false,false};
    	private JButton buttons[];
    	private int button_count = 12;
    	private int prev_button = 255; // silly number to signify no button
    
    	//Icons
    	private Icon iconSpade, iconClub, iconHeart, iconDiamond, iconPlus, iconMinus;
    
    	public guessGame(){
    
    		super("Guessing Game");
    
    		setup();
    
    		setSize(500, 500);		//Set Size
    		setVisible(true);		//Set Visible so can be seen
    
    
    	}
    
    	/********************************************/
    	/*	This method passes onto the other	 	*/
    	/*	methods to set up the guessing Game		*/
    	/********************************************/
    	public void setup(){
    
    		Container c = getContentPane();
    
    		setUpPanels();
    		setUpButtons();
    		setupIcons();
    		setButtonFont();
    		buttonPanelAdd();
    		setUpListener();
    
    		//Set Content Pane Layout
    		c.add(gamePanel, BorderLayout.CENTER);
    		c.add(bottomPanel, BorderLayout.SOUTH);
    
    	}
    
    	/********************************************/
    	/*	This method sets p the panels and	 	*/
    	/*	makes the game panel a grid layout		*/
    	/********************************************/
    
    	public void setUpPanels(){
    
    		gamePanel = new JPanel();
    		bottomPanel = new JPanel();
    
    		//Set Grid Layout
    		gamePanel.setLayout(new GridLayout(3, 4, 3, 3));
    
    	}
    
    	public void setupIcons() {
    		iconSpade = new ImageIcon("spade.gif");
    		iconClub = new ImageIcon("club.gif");
    		iconHeart = new ImageIcon("heart.gif");
    		iconDiamond = new ImageIcon("diamond.gif");
    		iconPlus = new ImageIcon("plus.gif");
    		iconMinus = new ImageIcon("minus.gif");
    	}
    
    	/********************************************/
    	/*	This method sets up the buttons 	 	*/
    	/*	putting a ? on each one	and setting 	*/
    	/*	up the check and reset button			*/
    	/********************************************/
    
    	public void setUpButtons(){
    
    
    		buttons = new JButton[button_count];
    
    		//Set the text as Question Mark
    		for (int i=0; i < button_count;i++)
    		{
    			buttons[i] = new JButton("?");
    		}
    
    
    		checkButton = new JButton("CHECK");
    
    		resetButton = new JButton("RESET");
    
    	}
    
    	/********************************************/
    	/*	This method adds the font to the 	 	*/
    	/*	buttons. (Times New Roman, BOLD and   	*/
    	/*	size 30)								*/
    	/********************************************/
    
    	public void setButtonFont(){
    
    		Font buttonFont = new Font("Times", Font.BOLD, 30);
    
    		//Set Font
    		for (int i=0;i<button_count;i++) {
    			buttons[i].setFont(buttonFont);
    		}
    
    
    	}
    
    	/********************************************/
    	/*	This method adds the buttons to the 	*/
    	/*	the correct panel.						*/
    	/********************************************/
    
    	public void buttonPanelAdd(){
    
    		//Add to gamePanel
    		for (int i=0; i<button_count; i++) {
    			gamePanel.add(buttons[i]);
    		}
    
    		//Add to bottomPanel
    		bottomPanel.add(checkButton);
    		bottomPanel.add(resetButton);
    
    	}
    
    
    	/********************************************/
    	/*	This method adds the buttons to the  	*/
    	/* 	action listener							*/
    	/********************************************/
    
    	public void setUpListener(){
    
    		//Add Action Listeners
    		for (int i=0; i<button_count; i++) {
    			buttons[i].addActionListener(this);
    		}
    
    		checkButton.addActionListener(this);
    		resetButton.addActionListener(this);
    
    	}
    
    	/********************************************/
    	/*	This is the action listener methoid.	*/
    	/*	If buttonX is pressed then the variable	*/
    	/*	buttonX_pressed is set to true so the 	*/
    	/*	program knows when the match has been 	*/
    	/*	chosen. if the buttonX_pressed and 		*/
    	/*	its match are pressed together, the		*/
    	/*	counter counts the point. If the match	*/
    	/*	is not correct then the 2 buttons are 	*/
    	/*	reset.									*/
    	/*											*/
    	/*	Note : X = number						*/
    	/********************************************/
    
    	public void actionPerformed(ActionEvent e){
    
    		int button_number = 0;
    
    		for (int i=0; i<button_count; i++) {
    			if(e.getSource().equals(buttons[i])){
    				button_number = i;
    			}
    		}
    
    		buttons[button_number].setText("");
    
    		switch (button_number) {
    			case 0:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 6) {         // other club already revealed
    						buttons[button_number].setIcon(iconClub);
    						counter++;
    					} else if (prev_button == 255){ // first of the pair
    						buttons[button_number].setIcon(iconClub);
    					} else {
    						// not a match, reset?
    						buttons[button_number].setIcon(null);
    						buttons[button_number].setText("?");
    						prev_button = button_number;
    					}
    				}
    				break;
    			case 1:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 7) {         // other Diamond already revealed
    						buttons[button_number].setIcon(iconDiamond);
    						counter++;
    					} else if (prev_button == 255){ // first of the pair
    						buttons[button_number].setIcon(iconDiamond);
    					}
    					else {
    						// not a match, reset?
    						buttons[button_number].setIcon(null);
    						buttons[button_number].setText("?");
    				}
    			}
    			button_number = prev_button;
    			break;
    			case 2:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 8) {         // other Spade already revealed
    						buttons[button_number].setIcon(iconSpade);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconSpade);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    			case 3:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 9) {         // other Heart already revealed
    						buttons[button_number].setIcon(iconHeart);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconHeart);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    			break;
    			case 4:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 10) {         // other Minus already revealed
    						buttons[button_number].setIcon(iconMinus);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconMinus);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    			break;
    			case 5:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 11) {         // other Plus already revealed
    						buttons[button_number].setIcon(iconPlus);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconPlus);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    			case 6:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 0) {         // other club already revealed
    						buttons[button_number].setIcon(iconClub);
    						counter++;
    					} else if (prev_button == 255){ // first of the pair
    						buttons[button_number].setIcon(iconClub);
    					} else {
    						// not a match, reset?
    						buttons[button_number].setIcon(null);
    						buttons[button_number].setText("?");
    						prev_button = button_number;
    					}
    				}
    
    				break;
    			case 7:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 1) {         // other Diamond already revealed
    						buttons[button_number].setIcon(iconDiamond);
    						counter++;
    					}
    					else if (prev_button == 255){ // first of the pair
    						buttons[button_number].setIcon(iconDiamond);
    					}
    					else {
    						// not a match, reset?
    						buttons[button_number].setIcon(null);
    						buttons[button_number].setText("?");
    						button_number = prev_button;
    					}
    				}
    
    				break;
    			case 8:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 2) {         // other Spade already revealed
    						buttons[button_number].setIcon(iconSpade);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconSpade);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    			case 9:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 3) {         // other Heart already revealed
    						buttons[button_number].setIcon(iconHeart);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconHeart);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    			case 10:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 4) {         // other Minus already revealed
    						buttons[button_number].setIcon(iconMinus);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconMinus);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    			case 11:
    				if (button_number != prev_button) { // was this button already pressed.
    					if (prev_button == 5) {         // other Plus already revealed
    						buttons[button_number].setIcon(iconPlus);
    						counter++;
    				} else if (prev_button == 255){ // first of the pair
    					buttons[button_number].setIcon(iconPlus);
    				} else {
    					// not a match, reset?
    					buttons[button_number].setIcon(null);
    					buttons[button_number].setText("?");
    					button_number = prev_button;
    				}
    			}
    				break;
    		}
    
    		// record the state for next round...
    		prev_button = button_number;
    
    		if(e.getSource().equals(resetButton)){
    			setUpResetButton();
    			prev_button = 255; // Override the button number
    		}
    		if(e.getSource().equals(checkButton)){
    			JOptionPane.showMessageDialog(null, "Your score is : " + counter);
    			//prev_button = 255; // Override the button number
    		}
    	}
    
    	/********************************************/
    	/*	This method sets up the reset button 	*/
    	/*	When the reset button is pressed the	*/
    	/*	icons are replaced with ? and the 		*/
    	/*	counter is set to 0.					*/
    	/********************************************/
    
    	public void setUpResetButton(){
    
    		//Reset the Button putting Question Mark back
    		for (int i=0; i<button_count; i++){
    			buttons[i].setText("?");
    			buttons[i].setIcon(null);
    		}
    			counter = 0;
    	}
    
    	public static void main(String[]args)
    	{
    		guessGame game = new guessGame();
    	}
    }
    


Advertisement