Monday, 8 February 2021

Game of Life in Java

  • For information on the Game of Life, visit this site.
  • The following code is a Java program simulating the Game of Life.
  • Clicking on white cells makes them black and vice-versa.
  • The completed jar can be downloaded from this site.
  • The jar file requires JRE7 to run.
  • This is the code for the Java program:

import java.awt.Color;

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.JTextField;

 

public class mainjarlife {

 

       public static void main(String[] args){

              final JButton arr[]=new JButton[2500];

              final JFrame frm=new JFrame("Life");

              final JTextField set=new JTextField();

              set.setSize(490,20);

              set.setLocation(10,510);

              frm.add(set);

              frm.setSize(520, 609);

              frm.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-frm.getSize().width/2,Toolkit.getDefaultToolkit().getScreenSize().height/2-frm.getSize().height/2);

              frm.setLayout(null);

              frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              frm.setResizable(false);

              JButton ok=new JButton(">>");

              ok.setSize(70,20);

              ok.setLocation(430,540);

              ok.addActionListener(new ActionListener() {

 

                     @Override

                     public void actionPerformed(ActionEvent arg0) {

                           try {

                                  Thread.sleep(100);

                           } catch (InterruptedException e) {

                                  // TODO Auto-generated catch block

                                  e.printStackTrace();

                           }

                           for(int i=0;i<2500;i++) {

                                  if (arr[i].getLocation().x==0) {continue;}

                                  if (arr[i].getLocation().y==0) {continue;}

                                  if (arr[i].getLocation().x==490) {continue;}

                                  if (arr[i].getLocation().y==490) {continue;}

                                  arr[i].setName("");

                                  int n=0;

                                  if (arr[i+1].getBackground()==Color.black) {n++;}

                                  if (arr[i-1].getBackground()==Color.black) {n++;}

                                  if (arr[i+51].getBackground()==Color.black) {n++;}

                                  if (arr[i-51].getBackground()==Color.black) {n++;}

                                  if (arr[i+50].getBackground()==Color.black) {n++;}

                                  if (arr[i-50].getBackground()==Color.black) {n++;}

                                  if (arr[i+49].getBackground()==Color.black) {n++;}

                                  if (arr[i-49].getBackground()==Color.black) {n++;}

                                  if (n==2) {continue;}

                                  if (n!=2 && n!=3) {arr[i].setName("d");}

                                  if (n==3) {arr[i].setName("l");}

                           }

                           for(int i=0;i<2500;i++) {

                                  if(arr[i].getName().equals("d")) {arr[i].setBackground(Color.white);}

                                  if(arr[i].getName().equals("l")) {arr[i].setBackground(Color.black);}

                           }

                     }

              });

              frm.add(ok);

              final JButton clr=new JButton("Clear");

              clr.setSize(70,20);

              clr.setLocation(350,540);

              clr.addActionListener(new ActionListener() {

                     @Override

                     public void actionPerformed(ActionEvent arg0) {

                           for (int i=0;i<2500;i++) {

                                  arr[i].setBackground(Color.white);

                                  arr[i].setName("");

                           }

 

                     }

              });

              frm.add(clr);

              JButton gc=new JButton("GetByte");

              gc.setSize(90,20);

              gc.setLocation(250,540);

              gc.addActionListener(new ActionListener() {

 

                     @Override

                     public void actionPerformed(ActionEvent arg0) {

                           String x="";

                           for (int i=0;i<2500;i++) {

                                  if (arr[i].getBackground()==Color.black) {

                                         x=x+i+"'";

                                  }

                           }

                           set.setText(x);

                     }

              });

              frm.add(gc);

              JButton sc=new JButton("SetByte");

              sc.setSize(90,20);

              sc.setLocation(150,540);

              sc.addActionListener(new ActionListener() {

 

                     @Override

                     public void actionPerformed(ActionEvent arg0) {

                           clr.doClick();

                           String code=JOptionPane.showInputDialog("Enter byte code");

                           for (int i=0;i<code.split("'").length;i++) {

                                  arr[Integer.parseInt(code.split("'")[i])].setBackground(Color.black);;

                           }

                     }

              });

              frm.add(sc);

              for (int i=0;i<2500;i++) {

                     arr[i]=new JButton("");

                     arr[i].setName("");

                     arr[i].setSize(10, 10);

                     arr[i].setLocation(i%50*10, (int)Math.floor(i/50)*10);

                     arr[i].setVisible(true);

                     arr[i].setBackground(Color.white);

                     final int num=i;

                     arr[i].addActionListener(new ActionListener() {

 

                           @Override

                           public void actionPerformed(ActionEvent arg0) {

                                  if (arr[num].getBackground()==Color.white) {arr[num].setBackground(Color.black);}

                                  else {arr[num].setBackground(Color.white);}

                           }

                     });

                     frm.add(arr[i]);

              }

              frm.setVisible(true);

 

       }

 

}

 


  • Run this code in your IDE

No comments:

Post a Comment

Algorithmic Pixel Art: The Beauty of Aliasing

Discussed  here More images in  Google drive Some outputs: Forward to anyone who says aliasing is ugly.