[Implementation] Pentris – MoodChooser

ModeChooser is a class generally made for Menu screen. I designed the class so that when I use it, I can give information of buttons and # of buttons that user will choose. Using this class, I can, in return, get information buttons chosen by users.

Also, I made three constructors to allow to choose the dimension of menu screen and to set the title of the menu screen.

Most importantly, I used notify() and wait() method overriden from Object class to efficiently make the main frame Thread till this menu Thread is finished.

//call other thread to work
			try{
				synchronized(out){
					out.notify();
				}
			}catch(Exception exp){
				exp.printStackTrace();
			}

In this class, ‘in’ is a group of Buttons that were received from user and ‘out’ is a group of Buttons chosen by user. Both are implemented using simple arrays.

Now, check the full sourcecode.

package gygd.pentris.choi;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * This class was created for the functionality of displaying a set of buttons and allowing the user to choose one
 * This class was made very generally for future reuse
 * This class uses wait() and notify()
 * @author Wonjohn Choi
 *
 */
public class ModeChooser extends JFrame implements Runnable, ActionListener{
	
	protected JButton[][] in; //given buttons
	public JButton[] out; //chosen buttons
	protected int idx; //index needed to fill the 'out' array
	
	/**
	 * constructor
	 * @param buttons
	 * @param re
	 */
	public ModeChooser(JButton[][] buttons, int n){
		//initialize process
		in = buttons;
		out = new JButton[n]; //n indicates # of expected chosen buttons
		idx = 0;
		
		//set layout
		setLayout(new GridLayout(buttons.length, buttons[0].length));
		
		//for each buttons,
		for(int r=0;r<buttons.length;r++){
			for(int c=0;c<buttons[0].length;c++){
				//if a button does not exit,
				if(buttons[r][c]==null){
					//put a dead button
					buttons[r][c]=new JButton();
					buttons[r][c].setEnabled(false);
				}
				
				//reaction to mouse click
				buttons[r][c].addActionListener(this);
				//add to frame
				add(buttons[r][c]);
			}
		}
		
		//set the best optimized size
		setResizable(false);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * constructor
	 * @param buttons
	 * @param n
	 * @param name
	 */
	public ModeChooser(JButton[][] buttons, int n, String name){
		this(buttons, n);
		setTitle(name);
	}
	
	/**
	 * constuctor that sets the size of chooser
	 * @param buttons
	 * @param re
	 * @param dimen
	 */
	public ModeChooser(JButton[][] buttons, int n, Dimension dimen){
		this(buttons, n); //call standard constructor
		
		//set size of the button chooser
		setSize(dimen);
	}
	
	/**
	 * for thread purpose
	 */
	@Override
	public void run() {
		//start by making it visible
		setVisible(true);
	}

	/**
	 * when clicked by mouse
	 */
	@Override
	public void actionPerformed(ActionEvent event) {
		JButton chosen = (JButton) event.getSource(); //get button
		chosen.setEnabled(false); //kill its reaction
		out[idx] = chosen; //store it to the chosen button list
		idx++; 
		
		//if required buttons are chosen,
		if(out.length == idx){
			//invisible frame
			setVisible(false);
			
			//call other thread to work
			try{
				synchronized(out){
					out.notify();
				}
			}catch(Exception exp){
				exp.printStackTrace();
			}
			
		}
		
	}

}

Advertisements

About Wonjohn Choi
Hello, I am a software developer Wonjohn Choi. I live in Mississauga, Ontario, Canada and study at a school called St. Francis Xavier Secondary School. My core interests include Math, Computer Science, and Engineering. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: