Created
December 2, 2017 00:01
-
-
Save dragon0/0c55da7ca3caaa9b32fd29203c8e220c to your computer and use it in GitHub Desktop.
Creates a checkerboard from a text description
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color | |
import java.awt.Graphics2D | |
import java.awt.image.BufferedImage | |
import javax.imageio.ImageIO | |
Red = new Color(255, 0, 0) | |
Green = new Color(0, 255, 0) | |
Blue = new Color(0, 0, 255) | |
Orange = new Color(255, 165, 0) | |
Yellow = new Color(255, 255, 0) | |
Pink = new Color(255, 192, 203) | |
grid = '''B O R O Y | |
O R B G R | |
B O G O Y | |
Y G B Y G | |
R O R B R''' | |
def gridToMatrix(grid) { | |
def rows = [] | |
def sgrid = new Scanner(grid) | |
while(sgrid.hasNextLine()){ | |
def line = sgrid.nextLine() | |
def sline = new Scanner(line) | |
def row = [] | |
rows << row | |
while(sline.hasNext()){ | |
row << sline.next() | |
} | |
} | |
rows | |
} | |
def tileToColor(tile) { | |
switch(tile) { | |
case "R": return Red | |
case "G": return Green | |
case "B": return Blue | |
case "O": return Orange | |
case "Y": return Yellow | |
case "P": return Pink | |
default: | |
throw new IllegalArgumentException("Unknown color: " + tile) | |
} | |
} | |
mat = gridToMatrix(grid) | |
rows = mat.size() | |
cols = mat[0].size() | |
tileWidth = 100 | |
tileHeight = 100 | |
println(mat) | |
width = tileWidth * cols | |
height = tileHeight * rows | |
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) | |
g = bi.createGraphics(); | |
g.setBackground() | |
y = 0 | |
mat.each {row -> | |
x = 0 | |
row.each {tile -> | |
g.setPaint(tileToColor(tile)) | |
printf("Drawing to %d %d\n", x, y) | |
g.fillRect(x, y, tileWidth, tileHeight) | |
x+=tileWidth | |
} | |
y+=tileHeight | |
} | |
ImageIO.write(bi, "PNG", new File("colormaze.png")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment