[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();
			}
			
		}
		
	}

}

[Implementation] Pentris – Board

Basic structure for Board class was finished: general GUI is done.

Check the source code posted. The class uses bunch of buttons to simulate what I want.

package gygd.pentris.choi;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 * board class
 * @author Wonjohn Choi
 *
 */
public class Board extends JPanel {
	//array to control each block
	protected JButton[][] blockGrid;

	//mathematical grid
	protected boolean[][] grid;

	static final boolean EMPTY = false;
	static final boolean FILLED = true;

	//width and height of the board
	protected int HEIGHT, WIDTH;

	public Board(int height, int width){
		HEIGHT = height; WIDTH = width;

		grid = new boolean[HEIGHT][WIDTH];
		blockGrid = new JButton[HEIGHT][WIDTH]; //instantiate
		setLayout(new GridLayout(HEIGHT, WIDTH)); //set its layout

		//for each and every block,
		for(int row=0;row<HEIGHT;row++){
			for(int col=0;col
				blockGrid[row][col] = new JButton();//set each block as a button
				blockGrid[row][col].setBorder(new LineBorder(Color.getHSBColor(0, 0,0.8F)));//set its border color
				blockGrid[row][col].setEnabled(false);//disable it
				add(blockGrid[row][col]);//add to the panel
			}
		}

		setSize(WIDTH*30, HEIGHT*30); //set the size (each block has a size length of 30)
	}

	public static void main(String args[]){
		JFrame test = new JFrame();
		Board board = new Board(20, 20);
		test.add(board);
		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		test.setVisible(true);
		test.setSize(board.WIDTH*30, board.HEIGHT*30);
	}

}

Pentris_Board

Pentris_Board

The left image is earned by running above code.

[Design] Pentris

Board.java

This class will extend JPanel and will be used to manage blocks in pentris board. Each block will be made by JButton. Also, this class will receive two integer variables to set the size of the board.

For “Only Tetris” mode, the board size will be 10*20.

For “Tetris+Pentris” or “Pentris” mode, the board size will be 20*20 because it’s much harder.

Piece.java

A model class for every piece.

ModeChooser.java

A class to choose modes (“Only Tetris,” “Tetris + Pentris,” “Only Pentris”)

Pentris.java

A class to manage the whole game.

This class should include Timer, Board.java, TetrisPieces.java, PentrisPieces.java, Level Option

**Other classes/functions will be added in future if necessary

[Requirement] Pentris

Hello I am Wonjohn Choi.

I named the game I am going to develop as Pentris.

As I do not think there was any game similar to this before, I need to explain a bit. This game is a variation of Tetris, the traditional game. But, in Pentris, we use pentominoes (polyomino composed of five blocks) instead of Tetris blocks.

Tetris Image from Flickr

Tetris Image from Flickr

I am going to develop this with Java.

———————————Update——————————————-

Decided to allow user to choose between three moods: only Tetris, Tetris+Pentris, only Pentris

Q_Calc

Hello I am Wonjohn Choi.

Q_Calc.”

By the name, it is easy to guess that the new software is a calculator (yes, it is). But, this calculator is actually an android version of “Equation Calculator“!

Language: Java with Device API version is 8 (Android 2.2)

screenshot of Q_Calc

screenshot of Q_Calc

As I do not have any smartphone, I had to use Android Virtual Machine, which is extremely slow, especially on my laptop computer.

The logic is very similar to the “Equation Calculator” and GUI is very simple (Button, EditText, TextView).

I am going to upload the project files (including source codes) for people who want to learn from this software. Check downloads page.

Any updates will be posted here.

Equation Calculator

Hello I am Wonjohn Choi

Haven’t written any posts for a while. I was very busy for other stuff, but kept making programs when I was bored and when I was … bored.

As I am going to be a senior student in my school, I think time won’t allow me to make any more developer’s posts (that explains how I developed/programed a software), but I might still make softwares and put it to the download page.

Also, I decided to stop putting stupid information like:

  • Project Manager/Leader: Wonjohn Choi
  • Designer: Wonjohn Choi
  • Programmer: Wonjohn Choi
  • QA (Quality assurance): Wonjohn Choi

Unless specified, it is so obvious that who is doing what as I am the only member of G.Y.G.D!

But, still, I will put other general information.

This time I made a graphical interface calculator called “Equation Calculator” using Java, the programming language by Sun.

Different from the “Button Calculator,” this calculator computes an equation. For example, if user puts an equation “((1+2+3)*6/36)^30,” it will output 1. Although design is simpler, this calculator required much more logic than Button Calculator.

screenshot for equation calculator by Wonjohn Choi

screenshot for equation calculator by Wonjohn Choi

GUI: I used three “JTextField”s, three “JLabel”s, and two “JButton”s to represent the Graphic.

Logic: First convert infix equation given by user to postfix equation. Then, use postfix equation to evaluate the answer. If you are not familiar with postfix equation or related algorithm, I recommend you to check http://en.wikipedia.org/wiki/Reverse_Polish_notation . I put some variations on it to allow using ‘^’ (power) and blanks.

Any Updates will be posted here and check downloads page to download the “Equation Calculator.”

[Implementation]+[Maintenance] Elefant Hunt

Screenshot of Elefant Hunt Computer Game

Screenshot of Elefant Hunt Computer Game

Unlike usual, I won’t bother explaining the GUI classes that I used because it’s redundant and repetitive (Check the previous posts to learn GUI stuff!).

Check https://gygd.wordpress.com/downloads/ to download the game.

This page will be kept updated for further changes on the software.

—————————————-First Update: July 5th————————————–

Screenshot of Elefant Hunt Computer Game

Screenshot of Elefant Hunt Computer Game

  • The content of Log changed a lot to give more accurate and organized information to players.
  • “Current Player: Name” label and “Start Turn” button are added.
  • A critical error occurred after playing too many turns was fixed.