I am having a problem drawing a GenralPath onto a Graphics2D created from a BufferedImage. It works fine on a Graphics2D created from downcasting a Graphics object and displayed on the screen though. Any suggestions? I have posted the code stripped out of the whole program to keep it short below. I was trying to save a jpg file created from a BufferedImage and have to use a GeneralPath to draw the image as it's shape will change based on the arguments provided.
THANKS
import javax.swing.*;
import java.awt.image.*;
import java.awt.geom.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import javax.imageio.plugins.jpeg.*;
public class SaveImage extends JFrame{
BufferedImage newImage;
GeneralPath gp;
GeneralPath gp2;
Graphics g;
Graphics2D g2d;
Graphics newImageG2D;
public Shape sh;
public SaveImage() {
super("Image Save");
setSize(400,400);
setVisible(true);
}
public void paint(Graphics g) {
g2d = (Graphics2D) g;
newImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_BGR);
newImageG2D = newImage.createGraphics();
//Checking to make sure they are both Graphics2D objects
System.out.println(newImageG2D.toString());
System.out.println(g2d.toString());
newImageG2D.drawRoundRect(120,120,100,100,100,100);
newImageG2D.fillRoundRect(120,120,100,100,100,100);
newImageG2D.setColor(Color.red);
Ellipse2D.Double sh = new Ellipse2D.Double( 0,0,1,1 );
gp = new GeneralPath();
gp.moveTo(0,0);
gp.lineTo(100,0);
gp.lineTo(100,50);
gp.curveTo(100,100,100,100,50,100);
gp.lineTo(0,100);
gp.closePath();
//Uncomment this to create the problem
//newImageG2D.draw(gp);
//newImageG2D.fill(gp);
save(newImage);
g2d.drawRoundRect(120,120,100,100,100,100);
g2d.fillRoundRect(120,120,100,100,100,100);
g2d.setColor(Color.red);
gp2 = new GeneralPath();
gp2.moveTo(0,0);
gp2.lineTo(100,0);
gp2.lineTo(100,50);
gp2.curveTo(100,100,100,100,50,100);
gp2.lineTo(0,100);
gp2.closePath();
g2d.draw(gp2);
g2d.fill(gp2);
}
protected void save (BufferedImage bi) {
String filename = "newImage.jpg";
try{
ImageIO.write((RenderedImage)bi, "jpg", new File(filename));
System.out.println("Image Written");
} catch (Exception e) {
System.out.println(e.toString());
}
}
public static void main (String[] args){
SaveImage si = new SaveImage();
}
}