Created
November 1, 2020 07:23
-
-
Save tamurashingo/74cb77687377d9b4fcc77ef1ff580c8e to your computer and use it in GitHub Desktop.
Javaで画像として文字を出力する(Kotlinだけど)
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.image.BufferedImage | |
import java.io.File | |
import javax.imageio.ImageIO | |
fun main(args: Array<String>) { | |
val img = BufferedImage(28 * 7, 40, BufferedImage.TYPE_3BYTE_BGR) | |
val g = img.graphics | |
g.color = Color.WHITE | |
g.fillRect(0, 0, 28 * 7, 40) | |
g.color = Color.BLACK | |
g.font = Font("Arial", Font.PLAIN, 28) | |
g.drawString("あいうえお憂鬱", 0, 28) | |
val m = g.fontMetrics | |
println("height:" + m.height) | |
println("width:" + m.stringWidth("あ")) | |
g.color = Color.RED | |
g.drawRect(0, 0, 27, 33) | |
g.color = Color.BLUE | |
g.drawRect(0, 0, 27, 28) | |
g.color = Color.RED | |
g.drawRect(28, 0, 27, 33) | |
g.drawRect(56, 0, 27, 33) | |
g.drawRect(84, 0, 27, 33) | |
g.drawRect(112, 0, 27, 33) | |
g.drawRect(140, 0, 27, 33) | |
g.drawRect(168, 0, 27, 33) | |
g.dispose() | |
ImageIO.write(img, "png", File("test.png")) | |
println("hello world") | |
} |
「え」の右側がギリギリまで行っていたので、「憂鬱ていう字は大丈夫なんだろうか?」と試しに確認してみたやつ。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こんな出力