Created
August 22, 2011 15:58
-
-
Save zentrope/1162743 to your computer and use it in GitHub Desktop.
PNG on-the-fly image generation
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.io.File | |
import javax.imageio.ImageIO | |
import java.awt.image.BufferedImage | |
import java.awt.geom._ | |
import java.awt.Color | |
import java.awt.Font | |
import java.awt.Graphics2D | |
import java.util.Date | |
object Screen { | |
def make(clickX: Int, clickY: Int, message: String): BufferedImage = { | |
val width = 800 | |
val height = 480 | |
val image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) | |
val graphics = image.createGraphics() | |
graphics.setPaint(Color.darkGray) | |
graphics.fill(new Rectangle2D.Float(0, 0, width, height)) | |
graphics.setPaint(Color.black) | |
graphics.fill(new Rectangle2D.Float(10, 10, width - 20, height - 20)) | |
graphics.setPaint(Color.orange) | |
graphics.draw(new Line2D.Float(clickX - 10, clickY, clickX + 10, clickY)) | |
graphics.draw(new Line2D.Float(clickX, clickY - 10, clickX, clickY + 10)) | |
graphics.setPaint(Color.white) | |
graphics.setFont(new Font("San-Serif", Font.BOLD, 20)) | |
graphics.drawString(message, (width / 2) - 100, height / 2) | |
graphics.drawString(new Date().toString, 20, 440) | |
graphics.dispose() | |
image | |
} | |
} | |
val message = if (args.size > 0) args(0) else "This is a test." | |
val clickX = 670 | |
val clickY = 45 | |
ImageIO.write(Screen.make(clickX, clickY, message), "PNG", new File("test.png")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment