Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GameOfLife
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexandre DELEFOSSE
GameOfLife
Commits
4252509c
Commit
4252509c
authored
Dec 14, 2017
by
Alexandre DELEFOSSE
Browse files
Options
Downloads
Patches
Plain Diff
V 0.1
parent
f9651d87
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Automata.java
+11
-4
11 additions, 4 deletions
Automata.java
GraphicDisplayer.java
+28
-8
28 additions, 8 deletions
GraphicDisplayer.java
GraphicalGameOfLife.java
+69
-5
69 additions, 5 deletions
GraphicalGameOfLife.java
with
108 additions
and
17 deletions
Automata.java
+
11
−
4
View file @
4252509c
...
...
@@ -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
This diff is collapsed.
Click to expand it.
GraphicDisplayer.java
+
28
−
8
View file @
4252509c
...
...
@@ -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
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
GraphicalGameOfLife.java
+
69
−
5
View file @
4252509c
...
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment