Skip to content
Snippets Groups Projects
Commit 4252509c authored by Alexandre  DELEFOSSE's avatar Alexandre DELEFOSSE
Browse files

V 0.1

parent f9651d87
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,10 @@ public class Automata {
dimCube = dim;
}
public int getDimCube() {
return dimCube;
}
public int getWidth() {
return grid.length;
}
......@@ -71,9 +75,12 @@ public class Automata {
}
}
}
public void clicked(int x, int y) {
grid[(y-20)/dimCube][(x-20)/dimCube] = 1-grid[(y-20)/dimCube][(x-20)/dimCube];
clone[(y-20)/dimCube][(x-20)/dimCube] = 1-clone[(y-20)/dimCube][(x-20)/dimCube];
public void clicked(int x, int y, int offy, int offx) {
try {
grid[(y-offy)/(dimCube+1)][(x-offx)/(dimCube+1)] = 1-grid[(y-offy)/(dimCube+1)][(x-offx)/(dimCube+1)];
clone[(y-offy)/(dimCube+1)][(x-offx)/(dimCube+1)] = 1-clone[(y-offy)/(dimCube+1)][(x-offx)/(dimCube+1)];
} catch (RuntimeException e) {
java.lang.System.err.println("Mouse Click out of reach");
}
}
}
\ No newline at end of file
......@@ -8,32 +8,52 @@ import javax.swing.*;
@SuppressWarnings("serial")
public class GraphicDisplayer extends JPanel {
Automata myAutomata;
int startX, startY, size;
int startX, startY, _size;
public GraphicDisplayer(Automata d, int _size){
public GraphicDisplayer(Automata d, int size){
super();
myAutomata = d;
startX = 20;
startY = 20;
size = _size;
_size = size;
}
public int getStartX() {
return startX;
}
public void setStartX(int startX) {
this.startX = startX;
}
public int getStartY() {
return startY;
}
public void setStartY(int startY) {
this.startY = startY;
}
public void setDimCube(int dim) {
size = dim;
_size = dim;
}
public int getDimCube() {
return _size;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
this.setBackground(Color.DARK_GRAY);
for(int i=0; i<myAutomata.getWidth(); i++) {
for(int j=0; j<myAutomata.getHeight(); j++) {
if(myAutomata.isLiving(i,j)) {
g.setColor(Color.GRAY);
g.setColor(Color.decode("#FFD557"));
}
else {
g.setColor(Color.WHITE);
g.setColor(Color.GRAY);
}
g.fillRect(startY+size*j, startX+size*i, size, size);
g.fillRect(startY+_size*j+j, startX+_size*i+i, _size, _size);
}
}
}
......
......@@ -15,14 +15,14 @@ public class GraphicalGameOfLife{
static Automata goL;
static GraphicDisplayer space;
static JFrame screen = new JFrame();
static JButton next,init,jump,run,pause, resize, respeed;
static JButton next,init,jump,run,pause, resize, respeed, pasB;
static JPanel buttonPanel;
static Boolean go;
static int speed = 120;
static int speed = 120, pas = 10;
public static void main(String[] arg) {
int dimCube = 5;
int dimCube = 10;
int lDim = Integer.parseInt(JOptionPane.showInputDialog("Number of rows: "));
int cDim = Integer.parseInt(JOptionPane.showInputDialog("Number of columns: "));
goL = new Automata(lDim,cDim);
......@@ -32,14 +32,68 @@ public class GraphicalGameOfLife{
space.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent mouse) {
// TODO Auto-generated method stub
space.requestFocus();
if (mouse.getButton() == MouseEvent.BUTTON1) {
goL.clicked(mouse.getX(), mouse.getY());
goL.clicked(mouse.getX(), mouse.getY(), space.getStartX(), space.getStartY());
space.repaint();
}
}
});
space.addMouseWheelListener(new MouseAdapter() {
public void mouseWheelMoved(MouseWheelEvent e) {
int notches = e.getWheelRotation();
int size = goL.getDimCube();
if (notches < 0) {
goL.setDimCube(size+1);
space.setDimCube(size+1);
space.repaint();
} else {
goL.setDimCube(size-1);
space.setDimCube(size-1);
space.repaint();
}
}
});
space.setFocusable(true);
space.requestFocus();
space.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int x = space.getStartX();
int y = space.getStartY();
if(e.getKeyCode() == KeyEvent.VK_Z)
{
space.setStartX(x-pas);
space.repaint();
}
if(e.getKeyCode() == KeyEvent.VK_S)
{
space.setStartX(x+pas);
space.repaint();
}
if(e.getKeyCode() == KeyEvent.VK_D)
{
space.setStartY(y+pas);
space.repaint();
}
if(e.getKeyCode() == KeyEvent.VK_Q)
{
space.setStartY(y-pas);
space.repaint();
}
if(e.getKeyCode() == KeyEvent.VK_P)
{
speed -= 10;
}
if(e.getKeyCode() == KeyEvent.VK_M)
{
speed += 10;
}
}
});
next = new JButton("Next");
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
......@@ -79,6 +133,14 @@ public class GraphicalGameOfLife{
}
);
pasB = new JButton("pas");
pasB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pas = Integer.parseInt(JOptionPane.showInputDialog("New pas: "));
}
}
);
respeed = new JButton("respeed");
respeed.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
......@@ -101,6 +163,7 @@ public class GraphicalGameOfLife{
if (go.booleanValue()) {
goL.evolve();
space.repaint();
}
else running = false;
}
......@@ -136,6 +199,7 @@ public class GraphicalGameOfLife{
buttonPanel.add(pause);
buttonPanel.add(resize);
buttonPanel.add(respeed);
buttonPanel.add(pasB);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setSize(800,600);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment