Java - Guardar la imagen ¿qué está mal en este codigo?

 
Vista:

Guardar la imagen ¿qué está mal en este codigo?

Publicado por rbp_toledo (12 intervenciones) el 26/07/2006 13:22:20
Hola, veréis, tengo este applet en el que cargo una imagen y pinto sobre ella un óvalo. La muestro por pantalla y a la vez la guardo en un archivo jpg.

El problema es que al mostrarla por pantalla no me muestra la imagen original, me muestra un cuadro negro (del mismo tamaño que la imagen, eso sí). Lo del óvalo lo hace bien, pero en el archivo de salida (jpg) sólo me vuelve a salir el cuadro negro, sin óvalo.

¿Qué hago mal? Ahí va el código, y muchas gracias!:

public class nuevo extends Applet{

private BufferedImage bi;
/** Creates a new instance of nuevo */
public nuevo() {
setBackground (Color.blue);

Image img = getToolkit().getImage("planoFacil.jpg");
try{
MediaTracker tracker = new MediaTracker (this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}catch (Exception e){}

int iw = img.getWidth(this);
int ih = img.getHeight(this);
// bi = new BufferedImage (iw, ih, BufferedImage.TYPE_INT_RGB);
bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_BGR);
Graphics2D big = bi.createGraphics();
big.setColor(Color.blue);
big.drawOval(5,5, 90, 50);
big.drawImage(bi, 0, 0, this);

try{
FileOutputStream fos = new FileOutputStream("out.jpg");
JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(fos);
jpeg.encode(bi);
fos.close();
}catch (Exception e){}
}

public void paint (Graphics g){
Graphics2D g2 = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
int bw = bi.getWidth(this);
int hi = bi.getHeight(this);

g2.drawImage (bi, null, 0, 0);

}

public static void main (String s[]){
WindowListener l = new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
};
};
Frame f = new Frame("Modificado");
f.addWindowListener(l);
f.add("Center", new nuevo());
f.pack();
// f.setSize(new Dimension (800,600));
f.show();
}

}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder