projet-rendu/TestGraphicWrapper.java

138 lines
3.8 KiB
Java
Raw Normal View History

2022-04-12 10:08:58 +00:00
import java.awt.*;
public class TestGraphicWrapper {
static int width = 128;
static int height = 128;
static GraphicsWrapper screen;
2022-04-14 20:42:57 +00:00
private static void checker(int polarity, double r, double g, double b) {
2022-04-12 10:08:58 +00:00
screen.clearBuffer();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (((i / 8) % 2 == 1) == ((j / 8) % 2 == polarity)) {
2022-04-14 20:42:57 +00:00
screen.setPixel(j, i, r, g, b);
2022-04-12 10:08:58 +00:00
}
}
2022-04-14 20:42:57 +00:00
}
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
static void init() {
screen = new GraphicsWrapper(width, height, 1);
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
static int countNeighbours(int x0, int y0) {
2022-04-12 10:08:58 +00:00
int count = 0;
for (int y = y0 - 1; y <= y0 + 1; y++) {
for (int x = x0 - 1; x <= x0 + 1; x++) {
if ((x != x0) || (y != y0)) {
int _x = x % width;
int _y = y % height;
2022-04-14 20:42:57 +00:00
Color pix = screen.getFrontPixel(_x, _y);
if (pix.getRed() != 0) {
2022-04-12 10:08:58 +00:00
count++;
}
}
2022-04-14 20:42:57 +00:00
}
2022-04-12 10:08:58 +00:00
}
return count;
}
2022-04-14 20:42:57 +00:00
static void evolve() {
2022-04-12 10:08:58 +00:00
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
2022-04-14 20:42:57 +00:00
int c = countNeighbours(x, y);
Color pix = screen.getFrontPixel(x, y);
if (pix.getRed() == 0) {
// System.out.println (x + " " + y + " : dead (" + c + " nbrs) "
// + ((bpix.getRed () == 0) ? "dead" : "alive")
// );
2022-04-12 10:08:58 +00:00
if (c == 3) {
/* born */
2022-04-14 20:42:57 +00:00
screen.setPixel(x, y, 255, 255, 255);
2022-04-12 10:08:58 +00:00
} else {
2022-04-14 20:42:57 +00:00
screen.setPixel(x, y, 0, 0, 0);
2022-04-12 10:08:58 +00:00
}
} else {
2022-04-14 20:42:57 +00:00
// System.out.println (x + " " + y + " : alive (" + c + " nbrs) "
// + ((bpix.getRed() == 0) ? "dead" : "alive"));
2022-04-12 10:08:58 +00:00
if ((c >= 2) && (c <= 3)) {
/* survive */
2022-04-14 20:42:57 +00:00
screen.setPixel(x, y, 255, 255, 255);
2022-04-12 10:08:58 +00:00
} else {
/* die */
2022-04-14 20:42:57 +00:00
screen.setPixel(x, y, 0, 0, 0);
2022-04-12 10:08:58 +00:00
}
}
}
}
}
2022-04-14 20:42:57 +00:00
public static void testChecker() throws Exception {
2022-04-12 10:08:58 +00:00
for (int k = 0; k < 10; k++) {
2022-04-14 20:42:57 +00:00
checker(1, 1.0, 1.0, 1.0);
screen.swapBuffers();
Thread.sleep(100);
checker(0, 1.0, 1.0, 1.0);
screen.swapBuffers();
Thread.sleep(100);
2022-04-12 10:08:58 +00:00
}
}
2022-04-14 20:42:57 +00:00
public static void testConway() throws Exception {
2022-04-12 10:08:58 +00:00
2022-04-14 20:42:57 +00:00
screen.clearBuffer();
// screen.swapBuffers ();
// screen.clearBuffer ();
// screen.swapBuffers ();
/*
* for (int y = 0; y < height; y++) {
* for (int x = 0; x < width; x++) {
* if (Math.random() < 0.3) {
* screen.setPixel (x, y, 255, 255, 255);
* }
* }
* }
*/
screen.setPixel(20, 10, 255, 255, 255);
screen.setPixel(21, 11, 255, 255, 255);
screen.setPixel(19, 12, 255, 255, 255);
screen.setPixel(20, 12, 255, 255, 255);
screen.setPixel(21, 12, 255, 255, 255);
screen.swapBuffers();
Thread.sleep(1000);
2022-04-12 10:08:58 +00:00
for (int k = 0; k < 100; k++) {
2022-04-14 20:42:57 +00:00
evolve();
screen.swapBuffers();
Thread.sleep(30);
2022-04-12 10:08:58 +00:00
}
2022-04-14 20:42:57 +00:00
screen.destroy();
2022-04-12 10:08:58 +00:00
System.exit(0);
}
public static void main(String[] args) {
try {
2022-04-14 20:42:57 +00:00
init();
// testChecker () ;
testConway();
2022-04-12 10:08:58 +00:00
} catch (Exception e) {
2022-04-14 20:42:57 +00:00
System.out.println("EXCEPTION: " + e);
e.printStackTrace();
2022-04-12 10:08:58 +00:00
}
}
}