Showing posts with label swing. Show all posts
Showing posts with label swing. Show all posts

09 August 2011

Conway's Game of Life in Java!!

I recently met with Conway's Game of Life problem given as a programming assignment by one of the famous MNCs to clear level 1 in interview process. It was so mouth watering that I want to develop an UI for it and publish it in my blog. Spent 3 hours and came up with this.

Quick gist about the game from wikipedia
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

Following is the screenshot and an applet for the same. The Code is free to share and distribute. Visit my github link for this game for the bleeding edge copy.







Cheers!!
Braga

10 April 2010

TRIE data structure Part 6 : A Sample UI




Lets have a look at a sample application in Swing. We load the TRIE data structure using a dictionary word list. And using the application we can perform a search. This application is a very much prototypical just to check the insert() and search() operations.

Input file : words.txt

Sample Application


There are two possible outcomes to our search criteria, one true and another false. The following is the dialog shown when a word "article" is entered.

And when a word something like "dasdsa" is entered the following dialog is shown.


The Java code for the Trie Loader,

package dsa.tries;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class TrieLoader {
 
 public static Trie trieDSA;

 public static void main(String[] args) {
  TrieLoader trieLoader = new TrieLoader();
  trieLoader.load();
  new TrieTestFrame();
 }
 
 public void load(){
  trieDSA = new Trie();
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(new File("src/properties/words.txt")));
   String eachLine = null;
   while((eachLine=br.readLine())!=null){
    trieDSA.insert(eachLine);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally{
   if(br!=null){
    try {
     br.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

The TrieTestFrame,

package dsa.tries;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TrieTestFrame extends JFrame {

 private static final long serialVersionUID = 1L;
 
 private JPanel basePanel = new JPanel();
 private JTextField textField = new JTextField(20);
 private JButton button = new JButton("Check");

 public TrieTestFrame(){
  designUI();
 }
 
 private void designUI(){
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setTitle("TRIE Test");
  
  button.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    if(TrieLoader.trieDSA.search(textField.getText())){
     JOptionPane.showMessageDialog(basePanel, "The word you entered exist!!");
    }else{
     JOptionPane.showMessageDialog(basePanel, "The word you entered does not exist!!","Error",JOptionPane.ERROR_MESSAGE);
    }
   }
  });
  
  basePanel.add(textField);
  basePanel.add(button);
  
  add(basePanel);
  
  this.setSize(400,100);
  
  Toolkit tk = Toolkit.getDefaultToolkit();
     Dimension screenSize = tk.getScreenSize();
     int screenHeight = screenSize.height;
     int screenWidth = screenSize.width;
     setLocation(screenWidth / 2 - 200, screenHeight / 2 - 50);
  
  this.setVisible(true);
 }
}

For the Node and Trie class please click here.

Cheers,
Bragaadeesh.

05 February 2008

SWT / Java2D setTransform() problems!!!!

I will post one of the problems that i faced while i was in the design phase and was really confused about the strange features of RCP. That time, i wasnt knowing the solution and only few may have come across one. Eventually, i found the answer.



I may not know how many have you have come across this bug or error or impotentness..
Its between Java2d and swt..

Here is the snippet of java2d version

public void paint(Graphics g){
AffineTransform transform = new AffineTransform();
transform.scale(10,10);//any scale value am specifying
transform.translate(100,100);//any translation value

((Graphics2D)g).setStroke(new BasicStroke(0.0001f));
((Graphics2D)g).setTransform(transform);

g.draw( );//say a general path
}


Here is the swt version for the same
public void paintControl(PaintEvent e){
Transform transform = new Transform(e.getDevice());
transform.scale(10,10);//any scale value am specifying
transform.translate(100,100);//any translation value

e.gc.setLineWidth(1);//This is the least am able to specify
e.gc.draw(path);//Here path is org.eclipse.swt.graphics.Path
}


Both code looks almost the same. The basic problem here is this. In the former case, say i am giving a state boundary for the path and i am scaling the same. It is working just fine. (State boundary is the boundary of say Florida which might have latitude and longitude values like -83.45645631, 22.45646413)

But not so in SWT. In swt, the value of path variables are rounded off or casted to int and then the scale is applied which is a serious problem!!!!. Whereas, i need a mechanism where the scale is first applied and then it is casted to int. Moerover, the setLineWidth() method is taking a value of minimum 1 which when scaled to say 10 times, the thickness also is proportionately increasing.

This is killing me for the past one week and i cannot make any progress in this issue. If someone knows the internals or how to overcome this issue, kindly reply!!!!

Thanks in advance.

Braga




The solution to the above problem was simple : There is no solution.

Yes, At the time i was trying to create the application in RCP, it wont support Vector graphic features, which means you will not be able to draw images on the screen with double precision values. This was a huge setback for me. I thought of designing my full application in RCP and had a thorough understanding of the beautiful eclipse framework until i saw an IEEE paper presented by some college grads claiming the cons of RCP being its lack supportive towards Eclipse's RCP.