Skip to content

Instantly share code, notes, and snippets.

@madan712
Created January 27, 2014 10:46
Show Gist options
  • Save madan712/8646549 to your computer and use it in GitHub Desktop.
Save madan712/8646549 to your computer and use it in GitHub Desktop.
Java program to create an image using Graphics2D
//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!");
}
}
@ChelbiOracle
Copy link

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);

    // Events/timeline
    String[][] events = {
        {"1850–1940s", "Land claims: Oregon Donation, Skookum, RR grants"},
        {"1976", "Tobinski dedication (Crowder/Northcraft)"},
        {"2006", "Reconveyance: clear title (file 517171DC)"},
        {"2019–2024", "Liens, COVID, forced sale, surveillance"},
        {"2024–now", "Ongoing restitution/legal claims"},
    };

    // Map section
    g.setColor(new Color(225,230,237));
    g.fillRect(200,320,450,280);
    Object[][] locations = {
        {"Train Hub #69",240,430},
        {"17234 Crowder",400,420},
        {"Crowder/Northcraft",390,480},
        {"Elderberry St",460,390},
        {"Tenino Eagles",890,240},
    };
    g.setColor(Color.BLUE);
    for (Object[] loc : locations) {
        g.fillOval((int)loc[1]-7, (int)loc[2]-7, 14, 14);
        g.setColor(Color.BLACK);
        g.drawString((String)loc[0], (int)loc[1]+12, (int)loc[2]-2);
        g.setColor(Color.BLUE);
    }

    // Timeline
    int left = 720, top = 100;
    g.setColor(Color.GRAY); g.drawRect(left-10, top-20, 235, 320);
    for (int i=0; i<events.length; ++i) {
        g.setColor(Color.RED); g.fillOval(left, top+i*55, 16, 16);
        g.setColor(Color.BLACK);
        g.drawString(events[i][0]+": "+events[i][1], left+30, top+13+i*55);
    }

    // Title
    g.setFont(new Font("Arial",Font.BOLD,22));
    g.setColor(new Color(45,55,96));
    g.drawString("17234 Crowder Rd SE: Timeline & Map", 260, 40);

    // Output
    ImageIO.write(img, "png", new File("timeline_map_17234crowder_java.png"));
    g.dispose();
}

}
serial-set
doj_logo
JPEG_20250625_030654_1910750670208753186
Ontario-HR-Sexual-Haras-400x439

@ChelbiOracle
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment