The following is a snippet from a larger program, where the goal is to draw a red circle onto an image.
The resources I am using to accomplish this are from the following to sites
Create a BufferedImage from an Image
and
This is what I have
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class main {
public static void main(String[] args) throws IOException {
Image img = new ImageIcon("colorado.jpg").getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 100, 100));
g2d.drawImage(img, 0,0,null);
g2d.dispose();
ImageIO.write(bi, "jpg", new File("new.jpg"));
}
}
However when the code is ran the outputting image created is an exact copy of the input image with no alterations.
Painting through software is similar to painting on a canvas in the real world. If you paint something, then paint over it, it will paint over what was painted first. The order in which you do things is very important.
So, in your original code, you'd have to paint the image and the ellipse...
g2d.drawImage(img, 0,0,null);
g2d.fill(new Ellipse2D.Float(0, 0, 100, 100));
Now, having said that. There is an easier solution. Instead of using ImageIcon
, which has issues. You can just use ImageIO.read
to load the image. The immediate benefit is, you get a BufferedImage
//Image img = new ImageIcon("colorado.jpg").getImage();
//BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
BufferedImage bi = ImageIO.read(new File("colorado.jpg"));
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 100, 100));
g2d.dispose();
ImageIO.write(bi, "jpg", new File("new.jpg"));
Also, take a look at Reading/Loading an Image