Last active
October 2, 2022 22:53
-
-
Save Katharsas/7c3554f520f80c32cdb6130735206ea7 to your computer and use it in GitHub Desktop.
Debug-Number-Texture Generator
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.Font; | |
import java.awt.RenderingHints; | |
import java.awt.image.BufferedImage; | |
import java.io.IOException; | |
import java.nio.file.Paths; | |
import java.util.Random; | |
import javax.imageio.ImageIO; | |
public class Main { | |
public static void main(String[] args) { | |
new Main(); | |
} | |
float scale = 0.20f; | |
int spacingX = 30; | |
int spacingY = 10; | |
int numberCount = 1000; | |
String path = "number-textures"; | |
Random rand = new Random(); | |
/** | |
* Adapted from: https://stackoverflow.com/questions/18800717/convert-text-content-to-image | |
*/ | |
public Main() { | |
Font font = new Font("Arial", Font.PLAIN, 48); | |
int height = 512; | |
int width = height; | |
var img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | |
var g2d = img.createGraphics(); | |
g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); | |
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); | |
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | |
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); | |
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); | |
g2d.setFont(font); | |
var fm = g2d.getFontMetrics(); | |
for (int i = 0; i < numberCount; i++) { | |
int base = 128; | |
var bg = new Color(base + rand.nextInt(128), base + rand.nextInt(128), base + rand.nextInt(128)); | |
g2d.setColor(bg); | |
g2d.fillRect(0, 0, img.getWidth(), img.getHeight()); | |
String numberZeroed = String.format("%3d", i); | |
String numberFormatted = String.format("%03d", i); | |
String text = numberZeroed; | |
int textWidth = fm.stringWidth(text); | |
int textHeight = fm.getHeight(); | |
for (int y = 0; y < height; y += textHeight + spacingY) { | |
for (int x = 0; x < width; x += textWidth + spacingX) { | |
int underscoreOffsetY = 1 + fm.getAscent(); | |
g2d.setColor(Color.BLACK); | |
g2d.drawString(text, x, y + fm.getAscent()); | |
g2d.drawLine(x, y + underscoreOffsetY, x + textWidth, y + underscoreOffsetY); | |
} | |
} | |
try { | |
var imagePath = Paths.get(path).resolve("tex_" + numberFormatted + ".png"); | |
ImageIO.write(img, "png", imagePath.toFile()); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
g2d.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment