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

[5684 byte] By [philihopa] at [2007-10-2 22:49:37]
# 1

Hi philihop,

Firstly, thanks for your code and it helps me do my job.

Please read the following words if you haven't solved this problem.

Canvas paint method use Graphics object 'g' to draw something in the canvas. Its default value is black when you output to JPG image. That is the reason why it prints black background on jpg image. So please just add

int width = getWidth();

int height = getHeight();

g.setColor(Color.white);

g.fillRect(0, 0, width, height);

This 4 lines code draw the whole display area in Color.white. And problem is solved.

Byron

Message was edited by: Byron on 29/01/2007

byronzh

byronzha at 2007-7-14 6:03:55 > top of Java-index,Security,Cryptography...