Merge pull request #1 from krosset/master

Wrote a Hello World
cokolwiek
Merge pull request #2 from krosset/master

added a .gitignore
Merge branch 'master' of https://github.com/martinuz/colorgameoflife
package colorgameoflife;

public class Colorgameoflife {

    public static void main(String[] args) {

          game_engine GameEngine = new game_engine();
          graph_engine GraphEngine = new graph_engine();
          GameEngine.seed_map();
          GraphEngine.Engine = GameEngine;
          GraphEngine.redraw();
        }
}
package colorgameoflife;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
class Rectangle2
{
 Rectangle r;
 Color c;
}
public class graph_cell extends JPanel
{
int w = 10;
int h = 10;
int x = 0;
int y = 0;
private List<Rectangle2> squares = new ArrayList<Rectangle2>();
public void addSquare(int x, int y, int color_value)
{
      Rectangle2 rect = new Rectangle2();
      rect.r = new Rectangle(x,y,w,h);
      rect.c = new Color(255, color_value, color_value);
      squares.add(rect);

 }

 @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      for (Rectangle2 rect : squares)
      {
         g2.setColor(rect.c);
         g2.fill(rect.r);

      }

}
}
/*
Chwilowo nie robi to nic, ale może kiedyś będzie
 */
package colorgameoflife;
import javax.swing.*;
import java.awt.*;
public class graph_lines extends JPanel
{
    @Override
    protected void paintComponent(Graphics g)
    {
        g.setColor(Color.gray);
        for(int i = 1; i<=100; i++)
        {
        g.drawLine(10*i, 10, 10*i, 1000);
        }
        for(int i = 1; i<=100; i++)
        {
        g.drawLine(10, 10*i, 1000, 10*i);
        }
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colorgameoflife;
import java.util.Random;
/**
 *
 * @author martinuz
 */
public class game_engine
{
int[][] mapa = new int[100][100];
public void seed_map()
{
 Random generator = new Random(123);
 for(int x = 45; x<=55; x++)
 {
     for(int y = 45; y<=55; y++)
 {
     mapa[x][y]= generator.nextInt()%255;
     if(mapa[x][y]<0)
     {
     mapa[x][y] = 0;
     }

 }

 }
}

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colorgameoflife;
public class graph_engine
{
    game_engine Engine;
    public void redraw()
    {
    graph_window new_window = new graph_window(Engine);

    }

}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colorgameoflife;
import javax.swing.*;
public class graph_window extends JFrame {
     public graph_window(game_engine Engine) {
        setSize(1020, 1020);
        setTitle("Color Game of Life");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        graph_cell squares = new graph_cell();
        getContentPane().add(squares);
        for(int x = 0; x<100; x++)
         {
         for(int y = 0; y<100; y++)
         {

        if(Engine.mapa[x][y]>0)
        {
        squares.addSquare(10*x, 10*y, Engine.mapa[x][y]);
        }

        }

     }

}
}
deleted: nowy
delete Colorgameoflife.java
delete game_engine.java
delete graph_cell.java
delete graph_engine.java
delete graph_lines.java
delete graph_window.java
package colorgameoflife;
import java.util.*;

public class ColorGameOfLife {

    public static void main(String[] args) {
          int seed = 12;
          Timer newTimer = new Timer( );
          GameTimer newGameTimer = new GameTimer();
          GameEngine newGameEngine = new GameEngine();
          GraphEngine newGraphEngine = new GraphEngine();
          newGameEngine.seed_map(seed);
          newGraphEngine.Engine = newGameEngine;
          newGraphEngine.redraw();
          newGameTimer.Engine = newGameEngine;
          newGameTimer.GraphEngine = newGraphEngine;
          //newTimer.schedule(newGameTimer, 500);

        }
}
package colorgameoflife;
import java.util.*;
public class GameTimer extends TimerTask
{
    GameEngine Engine = new GameEngine();
    GraphEngine GraphEngine = new GraphEngine();
    public void run()
    {
        Engine.step_ahead();
        GraphEngine.redraw();
    }
}
package colorgameoflife;
public class GraphEngine
{
    GameEngine Engine;
    public void redraw()
    {
        GraphWindow new_window = new GraphWindow(Engine);
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colorgameoflife;
import javax.swing.*;
public class GraphWindow extends JFrame
{
     public GraphWindow(GameEngine Engine)
     {
        setSize(1020, 1020);
        setTitle("Color Game of Life");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);
        GraphCell squares = new GraphCell();
        getContentPane().add(squares);
        for(int x = 0; x<100; x++)
         {
            for(int y = 0; y<100; y++)
            {
                if(Engine.map[x][y]>0)
                {
                    squares.addSquare(10*x, 10*y, Engine.map[x][y]);
                }
            }
        }
    }
}
package colorgameoflife;
import java.util.Random;
public class GameEngine
{
    int[][] map= new int[100][100];
    public void seed_map(int seed)
    {
        Random generator = new Random(seed);
        for(int x = 45; x<=55; x++)
        {
            for(int y = 45; y<=55; y++)
            {
                map[x][y]= generator.nextInt()%255;
                if(map[x][y]<0)
                {
                    map[x][y] = 0;
                }

            }

        }
    }
    public void step_ahead()
    {
        int [][] map_temp = new int[100][100];
        int state = 0;
        for(int x = 45; x<=55; x++)
        {
            for(int y = 45; y<=55; y++)
            {
                if((map[x+1][y+1]>0)&&(x+1<map_temp.length)&&(y+1<map_temp.length))
                {
                    state++;
                }
                else if((map[x+1][y-1]>0)&&(x+1<map_temp.length)&&(y-1>0))
                {
                    state++;
                }
                else if((map[x-1][y+1]>0)&&(x-1>0)&&(y+1<map_temp.length))
                {
                    state++;
                }
                else if((map[x-1][y-1]>0)&&(x-1>0)&&(y-1>0))
                {
                    state++;
                }
                else if((map[x][y+1]>0)&&(y+1<map_temp.length))
                {
                    state++;
                }
                else if((map[x][y-1]>0)&&(y-1>0))
                {
                    state++;
                }
                else if((map[x+1][y+1]>0)&&(x+1<map_temp.length))
                {
                    state++;
                }
                else if((map[x-1][y+1]>0)&&(y-1>0))
                {
                    state++;
                }

                if(state == 3)
                {
                    map_temp[x][y] = map[x][y];
                }
                else if (state == 2)
                {
                    map_temp[x][y] = 255;
                }
            }
            map= map_temp;
        }
    }
}
package colorgameoflife;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
class Rectangle2
{
    Rectangle r;
    Color c;
}
public class GraphCell extends JPanel
{
    int w = 10;
    int h = 10;
    int x = 0;
    int y = 0;
    private List<Rectangle2> squares = new ArrayList<Rectangle2>();
    public void addSquare(int x, int y, int color_value)
    {
        Rectangle2 rect = new Rectangle2();
        rect.r = new Rectangle(x,y,w,h);
        rect.c = new Color(255, color_value, color_value);
        squares.add(rect);
     }
     @Override
     protected void paintComponent(Graphics g) {
     super.paintComponent(g);
     Graphics2D g2 = (Graphics2D) g;
     for (Rectangle2 rect : squares)
     {
        g2.setColor(rect.c);
        g2.fill(rect.r);

     }
}
}
/*
Chwilowo nie robi to nic, ale może kiedyś będzie
 */
package colorgameoflife;
import javax.swing.*;
import java.awt.*;
public class GraphLines extends JPanel
{
	    @Override
    protected void paintComponent(Graphics g)
    {
        g.setColor(Color.gray);
        for(int i = 1; i<=100; i++)
        {
            g.drawLine(10*i, 10, 10*i, 1000);
        }
        for(int i = 1; i<=100; i++)
        {
            g.drawLine(10, 10*i, 1000, 10*i);
        }
    }
}
Poprawione bledy wskazane przez Krzyska + animacja :)
Reup
Usuwanie starego pliku / zastepuje go GraphCellPanel
Dodana obsluga myszy
mini rewrite of GameEngine
AStyled GameEngine
GameEngine rewrite ctd
AStyled GraphWindow
formatting changes in GraphWindow
reflected Engine changes in Window
fixed syntax error (ctor)