Skip to content

Instantly share code, notes, and snippets.

@Geolykt
Created April 2, 2022 19:37
Show Gist options
  • Save Geolykt/28211b8140ef6a8e600fdcdf15642aaa to your computer and use it in GitHub Desktop.
Save Geolykt/28211b8140ef6a8e600fdcdf15642aaa to your computer and use it in GitHub Desktop.
Tool to aid the placement of structures on sites such as r/place. Syntax: `java CoordinatesProcessor.java <inputFile> <leftX> <upperY>`
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class CoordinatesProcessor {
private static final int gridMargin = 1;
private static final int gridZoom = 40;
private static final int pixelSize = gridZoom + gridMargin * 2;
private static final int writeOffset = 2;
public static void main(String[] args) throws Exception {
BufferedImage image = ImageIO.read(new File(args[0]));
int xb = Integer.valueOf(args[1]);
int yb = Integer.valueOf(args[2]);
BufferedImage out = new BufferedImage(image.getWidth() * pixelSize, image.getHeight() * pixelSize, image.getType());
Font font = Font.decode("SansSerif");
Graphics2D g2d = out.createGraphics();
for (int x = 0; x < image.getWidth(); x++) {
for (int y = 0; y < image.getHeight(); y++) {
if (image.getColorModel().getAlpha(image.getRaster().getDataElements(x, y, null)) > 127) {
g2d.setPaint(Color.GREEN);
g2d.fillRect(x * pixelSize, y * pixelSize, pixelSize, pixelSize);
int colorRGB = image.getRGB(x, y);
Color color = new Color(colorRGB);
g2d.setPaint(color);
g2d.fillRect(x * pixelSize + gridMargin, y * pixelSize + gridMargin, gridZoom, gridZoom);
if (color.getBlue() < 25 && color.getRed() < 25 && color.getGreen() < 25) {
g2d.setPaint(Color.WHITE);
} else {
g2d.setPaint(Color.BLACK);
}
GlyphVector xString = font.createGlyphVector(g2d.getFontRenderContext(), Integer.toString(xb + x));
g2d.drawGlyphVector(xString, x * pixelSize + gridMargin + writeOffset, y * pixelSize + gridMargin + writeOffset + (int) xString.getLogicalBounds().getHeight());
GlyphVector yString = font.createGlyphVector(g2d.getFontRenderContext(), Integer.toString(yb + y));
g2d.drawGlyphVector(yString, x * pixelSize + gridMargin + writeOffset, y * pixelSize + gridMargin + 2 * writeOffset + (int) xString.getLogicalBounds().getHeight() + (int) yString.getLogicalBounds().getHeight());
} else {
g2d.setPaint(new Color(0, 0, 0, 0));
g2d.fillRect(x * pixelSize, y * pixelSize, pixelSize, pixelSize);
}
}
}
ImageIO.write(out, "png", new File("output.png"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment