2 Replies - 1177 Views - Last Post: 31 January 2010 - 06:20 PM Rate Topic: -----

#1 cheryl92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 10

Stop the random in the array

Posted 31 January 2010 - 06:06 PM

/* Hi, I desperately need help for this. I don't know how to stop the random in the array after the timer is up and the user is supposed to click on the right button. It is impossible to match as the images are always at random. Any help will be deeply appreciated. Thanks */

package AYBE.ui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.ImageIcon;

import java.awt.Font;
import java.awt.Point;
import java.awt.Color;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

import AYBE.entity.QuestionGame1P1;

import java.awt.event.KeyEvent;

public class Game1P1L1 extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton jButtonCup1 = null;
	private JButton jButtonCup2 = null;
	private JButton jButtonCup3 = null;
	private MainFrame myFrame = null;
	private JButton jButtonShow = null;
	private JButton jButtonstart = null;
	private ArrayList<QuestionGame1P1> aList; // @jve:decl-index=0:
	private String[] bList = new String[3];
	private Timer test; // @jve:decl-index=0:
	private int time = 10;

	/**
	 * A This is the default constructor
	 */
	public Game1P1L1(MainFrame f) {
		super();
		myFrame = f;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1273, 760);
		this.setLayout(null);
		this.setBackground(Color.white);
		this.add(getJButtonCup1(), null);
		this.add(getJButtonCup2(), null);
		this.add(getJButtonCup3(), null);
		this.add(getJButtonShow(), null);
		this.add(getJButtonstart(), null);
	}

	/**
	 * This method initializes jButtonCup1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup1() {
		if (jButtonCup1 == null) {
			jButtonCup1 = new JButton();
			jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup1.setSize(new Dimension(147, 210));
			jButtonCup1.setLocation(new Point(350, 100));
			jButtonCup1.setBorder(null);
			jButtonCup1.setVisible(true);
			jButtonCup1.setName("/AYBE/ui/images/Cup.png");
			jButtonCup1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
							bList[0])));
					matching();
				}
			});
		}
		return jButtonCup1;
	}

	/**
	 * This method initializes jButtonCup2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup2() {
		if (jButtonCup2 == null) {
			jButtonCup2 = new JButton();
			jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup2.setSize(new Dimension(147, 210));
			jButtonCup2.setLocation(new Point(650, 100));
			jButtonCup2.setBorder(null);
			jButtonCup2.setName("/AYBE/ui/images/Cup.png");
			jButtonCup2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
							bList[1])));
					matching();
				}
			});
		}
		return jButtonCup2;
	}

	/**
	 * This method initializes jButtonCup3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup3() {
		if (jButtonCup3 == null) {
			jButtonCup3 = new JButton();
			jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup3.setSize(new Dimension(147, 210));
			jButtonCup3.setLocation(new Point(950, 100));
			jButtonCup3.setMnemonic(KeyEvent.VK_UNDEFINED);
			jButtonCup3.setBorder(null);
			jButtonCup3.setName("/AYBE/ui/images/CupAndCoin.png");
			jButtonCup3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
							bList[2])));
					matching();
				}
			});
		}
		return jButtonCup3;
	}

	/**
	 * This method initializes jButtonCoin
	 * 
	 * @return javax.swing.JButton
	 */
	public void setUpCupImageAfter() {
		jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
		jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
		jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
	}

	private JButton getJButtonShow() {
		if (jButtonShow == null) {
			jButtonShow = new JButton();
			jButtonShow.setText("Click to show!");
			jButtonShow.setBackground(Color.black);
			jButtonShow.setForeground(new Color(255, 255, 153));
			jButtonShow.setBounds(new Rectangle(55, 136, 157, 23));
			jButtonShow.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
			jButtonShow.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					timerForShow();
					jButtonShow.setEnabled(false);
					jButtonShow.setVisible(false);
					jButtonstart.setEnabled(true);
					jButtonstart.setVisible(true);

				}
			});
		}
		return jButtonShow;
	}

	public void timerForShow() {
		int delay = 200; // milliseconds

		ActionListener taskPerformer = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				aList = QuestionGame1P1.getImageList();
				jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
						aList.get(0).getCupImage())));
				jButtonCup1.setBorder(null);
				jButtonCup1.setBackground(Color.white);
				jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
						aList.get(1).getCupImage())));
				jButtonCup2.setBorder(null);
				jButtonCup2.setBackground(Color.white);
				jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
						aList.get(2).getCupImage())));
				jButtonCup3.setBorder(null);
				jButtonCup3.setBackground(Color.white);
				time--;

				if (time == 0) {
					test.stop();
					setUpCupImageAfter();
					for (int i = 0; i < bList.length; i++) {
						bList[i] = aList.get(i).getCupImage();
					}
				}

			}
		};
		test = new Timer(delay, taskPerformer);

		test.start(); // start the timer

	}

	public void matching() {
		if (jButtonCup1.isSelected()) {

			if (jButtonCup1.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

		else if (jButtonCup2.isSelected()) {

			if (jButtonCup2.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

		else {
			if (jButtonCup3.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

	}

	private JButton getJButtonstart() {
		if (jButtonstart == null) {
			jButtonstart = new JButton();
			jButtonstart.setForeground(new Color(255, 255, 153));
			jButtonstart.setBackground(Color.black);
			jButtonstart.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
			jButtonstart.setText("Click to start!");
			jButtonstart.setEnabled(false);
			jButtonstart.setBounds(new Rectangle(55, 136, 157, 23));
			jButtonstart.setVisible(false);
			jButtonstart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					// start timer
					// start game

				}
			});
		}
		return jButtonstart;
	}
}


[quote name='cheryl92' post='910978' date='31 Jan, 2010 - 05:04 PM']
package AYBE.ui;

import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.ImageIcon;

import java.awt.Font;
import java.awt.Point;
import java.awt.Color;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

import AYBE.entity.QuestionGame1P1;

import java.awt.event.KeyEvent;

public class Game1P1L1 extends JPanel {

	private static final long serialVersionUID = 1L;
	private JButton jButtonCup1 = null;
	private JButton jButtonCup2 = null;
	private JButton jButtonCup3 = null;
	private MainFrame myFrame = null;
	private JButton jButtonShow = null;
	private JButton jButtonstart = null;
	private ArrayList<QuestionGame1P1> aList; // @jve:decl-index=0:
	private String[] bList = new String[3];
	private Timer test; // @jve:decl-index=0:
	private int time = 10;

	/**
	 * A This is the default constructor
	 */
	public Game1P1L1(MainFrame f) {
		super();
		myFrame = f;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1273, 760);
		this.setLayout(null);
		this.setBackground(Color.white);
		this.add(getJButtonCup1(), null);
		this.add(getJButtonCup2(), null);
		this.add(getJButtonCup3(), null);
		this.add(getJButtonShow(), null);
		this.add(getJButtonstart(), null);
	}

	/**
	 * This method initializes jButtonCup1
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup1() {
		if (jButtonCup1 == null) {
			jButtonCup1 = new JButton();
			jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup1.setSize(new Dimension(147, 210));
			jButtonCup1.setLocation(new Point(350, 100));
			jButtonCup1.setBorder(null);
			jButtonCup1.setVisible(true);
			jButtonCup1.setName("/AYBE/ui/images/Cup.png");
			jButtonCup1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
							bList[0])));
					matching();
				}
			});
		}
		return jButtonCup1;
	}

	/**
	 * This method initializes jButtonCup2
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup2() {
		if (jButtonCup2 == null) {
			jButtonCup2 = new JButton();
			jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup2.setSize(new Dimension(147, 210));
			jButtonCup2.setLocation(new Point(650, 100));
			jButtonCup2.setBorder(null);
			jButtonCup2.setName("/AYBE/ui/images/Cup.png");
			jButtonCup2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
							bList[1])));
					matching();
				}
			});
		}
		return jButtonCup2;
	}

	/**
	 * This method initializes jButtonCup3
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCup3() {
		if (jButtonCup3 == null) {
			jButtonCup3 = new JButton();
			jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
					"/AYBE/ui/images/cup.jpg")));
			jButtonCup3.setSize(new Dimension(147, 210));
			jButtonCup3.setLocation(new Point(950, 100));
			jButtonCup3.setMnemonic(KeyEvent.VK_UNDEFINED);
			jButtonCup3.setBorder(null);
			jButtonCup3.setName("/AYBE/ui/images/CupAndCoin.png");
			jButtonCup3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
							bList[2])));
					matching();
				}
			});
		}
		return jButtonCup3;
	}

	/**
	 * This method initializes jButtonCoin
	 * 
	 * @return javax.swing.JButton
	 */
	public void setUpCupImageAfter() {
		jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
		jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
		jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
				"/AYBE/ui/images/cup.jpg")));
	}

	private JButton getJButtonShow() {
		if (jButtonShow == null) {
			jButtonShow = new JButton();
			jButtonShow.setText("Click to show!");
			jButtonShow.setBackground(Color.black);
			jButtonShow.setForeground(new Color(255, 255, 153));
			jButtonShow.setBounds(new Rectangle(55, 136, 157, 23));
			jButtonShow.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
			jButtonShow.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					timerForShow();
					jButtonShow.setEnabled(false);
					jButtonShow.setVisible(false);
					jButtonstart.setEnabled(true);
					jButtonstart.setVisible(true);

				}
			});
		}
		return jButtonShow;
	}

	public void timerForShow() {
		int delay = 200; // milliseconds

		ActionListener taskPerformer = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				aList = QuestionGame1P1.getImageList();
				jButtonCup1.setIcon(new ImageIcon(getClass().getResource(
						aList.get(0).getCupImage())));
				jButtonCup1.setBorder(null);
				jButtonCup1.setBackground(Color.white);
				jButtonCup2.setIcon(new ImageIcon(getClass().getResource(
						aList.get(1).getCupImage())));
				jButtonCup2.setBorder(null);
				jButtonCup2.setBackground(Color.white);
				jButtonCup3.setIcon(new ImageIcon(getClass().getResource(
						aList.get(2).getCupImage())));
				jButtonCup3.setBorder(null);
				jButtonCup3.setBackground(Color.white);
				time--;

				if (time == 0) {
					test.stop();
					setUpCupImageAfter();
					for (int i = 0; i < bList.length; i++) {
						bList[i] = aList.get(i).getCupImage();
					}
				}

			}
		};
		test = new Timer(delay, taskPerformer);

		test.start(); // start the timer

	}

	public void matching() {
		if (jButtonCup1.isSelected()) {

			if (jButtonCup1.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

		else if (jButtonCup2.isSelected()) {

			if (jButtonCup2.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

		else {
			if (jButtonCup3.getName() == ("/AYBE/ui/images/CupAndCoin.png"))
				System.out.print("S");
			else
				System.out.print("f");
		}

	}

	private JButton getJButtonstart() {
		if (jButtonstart == null) {
			jButtonstart = new JButton();
			jButtonstart.setForeground(new Color(255, 255, 153));
			jButtonstart.setBackground(Color.black);
			jButtonstart.setFont(new Font("Comic Sans MS", Font.BOLD, 18));
			jButtonstart.setText("Click to start!");
			jButtonstart.setEnabled(false);
			jButtonstart.setBounds(new Rectangle(55, 136, 157, 23));
			jButtonstart.setVisible(false);
			jButtonstart.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {

					// start timer
					// start game

				}
			});
		}
		return jButtonstart;
	}
}


This post has been edited by cheryl92: 31 January 2010 - 06:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Stop the random in the array

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Stop the random in the array

Posted 31 January 2010 - 06:06 PM

Please, :code:
Also, please include a clear description of your problem(s) in the body of your post instead of the title where it gets truncated. Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#3 cheryl92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 10

Re: Stop the random in the array

Posted 31 January 2010 - 06:20 PM

Sorry. I changed it already. Thanks.

This post has been edited by cheryl92: 31 January 2010 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1