Created
January 27, 2014 10:46
-
-
Save madan712/8646549 to your computer and use it in GitHub Desktop.
Java program to create an image using Graphics2D
This file contains hidden or 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
| //ImageCreator.java | |
| import java.awt.Color; | |
| import java.awt.Font; | |
| import java.awt.Graphics2D; | |
| import java.awt.image.BufferedImage; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import javax.imageio.ImageIO; | |
| public class ImageCreator { | |
| public static void main(String[] args) { | |
| System.out.println("Creating an image!"); | |
| int width = 250; | |
| int height = 250; | |
| //create a BufferedImage for mentioned image types. | |
| BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | |
| //create a graphics2d object which can be used to draw into the buffered image | |
| Graphics2D g2d = buffImg.createGraphics(); | |
| //fill the rectangle with grey color | |
| g2d.setColor(Color.GRAY); | |
| g2d.fillRect(0, 0, width, height); | |
| //draw a string | |
| g2d.setColor(Color.yellow); | |
| g2d.setFont(new Font("TimesRoman", Font.BOLD, 40)); | |
| g2d.drawString("JavaXp.com", 20, 100); | |
| //disposes of this graphics context and releases any system resources that it is using | |
| g2d.dispose(); | |
| //write the image file | |
| File f = new File("C:/Temp/icon.jpg"); | |
| try { | |
| ImageIO.write(buffImg, "jpg", f); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| System.out.println(f.getAbsolutePath()+" created successfully!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import java.awt.;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.;
public class TimelineMapCanvas {
public static void main(String[] args) throws Exception {
int width = 1000, height = 700;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setColor(new Color(245,244,240)); g.fillRect(0,0,width,height);
}



