Skip to content

Instantly share code, notes, and snippets.

@murphysean
Created July 29, 2014 05:35
Show Gist options
  • Save murphysean/7d5fbb4d0e45ab748f03 to your computer and use it in GitHub Desktop.
Save murphysean/7d5fbb4d0e45ab748f03 to your computer and use it in GitHub Desktop.
A way to draw on a memory bitmap in Java
import java.awt.Color;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class Drawer{
public static void main(String[] args) throws Exception{
//Create an in memory Image
BufferedImage img = new BufferedImage(100, 10, BufferedImage.TYPE_INT_ARGB);
//Grab the graphics object off the image
Graphics2D graphics = img.createGraphics();
Color color = new Color(50,50,50);
Stroke stroke = new BasicStroke(1f);
//graphics.setStroke(stroke);
graphics.setPaint(color);
graphics.fill(new Rectangle(0,0,100,10));
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment