prob in saving canvas to jpg
hi friends, i m tryin to save canvas to jpg n has de code for it but saved jpg file contalinsblack background... i hav attached de code.. can u plz execute it n suggest wat de prob is.....import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
publicclass CanvasClipextends Canvasimplements ActionListener
{
Rectangle clip;
boolean showClip;
public CanvasClip()
{
clip =new Rectangle(50, 150, 350, 300);
showClip =false;
}
publicvoid paint(Graphics g)
{
System.out.println("inside");
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int dia = Math.min(w,h)/4;
g2.setPaint(getBackground());
g2.fillRect(0,0,w,h);
g2.setPaint(Color.blue);
g2.drawRect(w/16, h/16, w*7/8, h*7/8);
g2.setPaint(Color.red);
g2.fillOval(w/8, h/12, w*3/4, h*5/6);
g2.setPaint(Color.green.darker());
g2.fillOval(w/2-dia/2, h/2-dia/2, dia, dia);
g2.setPaint(Color.orange);
g2.drawLine(w/16+1, h/16+1, w*15/16-1, h*15/16-1);
if(showClip)
{
g2.setPaint(Color.magenta);
g2.draw(clip);
}
}
publicvoid actionPerformed(ActionEvent e)
{
Button button = (Button)e.getSource();
String ac = button.getActionCommand();
if(ac.equals("show"))
{
showClip = !showClip;
repaint();
}
if(ac.equals("save"))
save();
}
privatevoid save()
{
int w = clip.width;
int h = clip.height;
BufferedImage img =new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = img.createGraphics();
g2.translate(-clip.x, -clip.y);
paint(g2);
g2.dispose();
String ext ="jpg";// or "png"; "bmp" okay in j2se 1.5
try
{
ImageIO.write(img,"jpg",new File("canvasClip.jpg"));
}
catch(IOException ioe)
{
System.err.println("write error: " + ioe.getMessage());
}
}
private Panel getUIPanel()
{
Button show =new Button("show clip");
Button save =new Button("save");
show.setActionCommand("show");
save.setActionCommand("save");
show.addActionListener(this);
save.addActionListener(this);
Panel panel =new Panel();
panel.add(show);
panel.add(save);
return panel;
}
publicstaticvoid main(String[] args)
{
CanvasClip cc =new CanvasClip();
Frame f =new Frame();
f.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.add(cc);
f.add(cc.getUIPanel(),"South");
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
I beleive u can get de wat prob is... if not ask me to b more clear

