Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package edu.ntnu.stud.chaosgame.view;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas;
/**
* This class converts the state of a ChaosCanvas to a WritableImage.
*/
public class ChaosCanvasToImageConverter {
/**
* The image to be created.
*/
private WritableImage image;
/**
* Convert the canvas to a writable image.
*
* @param chaosCanvas the canvas to work upon.
*/
public ChaosCanvasToImageConverter(ChaosCanvas chaosCanvas) {
int width = chaosCanvas.getWidth();
int height = chaosCanvas.getHeight();
image = new WritableImage(width, height);
PixelWriter pixelWriter = image.getPixelWriter();
int[][] canvasArray = chaosCanvas.getCanvasArray();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (canvasArray[i][j] == 1) {
pixelWriter.setColor(j, i, Color.BLACK);
} else {
pixelWriter.setColor(j, i, Color.WHITE);
}
}
}
}
/**
* Get the image.
*
* @return the image.
*/
public WritableImage getImage() {
return image;
}
}