Java - Problema guardando imagenes

 
Vista:

Problema guardando imagenes

Publicado por victor_a (1 intervención) el 13/03/2008 00:57:54
Buenas señores, estoy desarrollando una aplicacion para el procesamiento de imagenes, mi problema esta en que me crea el archivo con la extension que deseo pero este queda vacio (0 bytes), no se esta dibujando nada en el. El codigo que estoy usando para hacerlo es el siguiente, si alguien me puede colaborar le agradezco mucho.

/*******************************/
/*******************************/
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class guardarPanel extends JPanel{

private BufferedImage bufferedImage;

public guardarPanel(){
}

public void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

Graphics2D g2Image = null;
bufferedImage = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage .TYPE_INT_BGR);
g2Image = bufferedImage.createGraphics();
g2Image.setColor(Color.BLACK);
g2Image.fillRect(0,0,this.getWidth(), this.getHeight());
g2Image.setColor(Color.WHITE);
g2Image.drawString( "PRUEBA", this.getWidth() / 2, this.getHeight() / 2 ); //Este es um metodo que llamamos para dibujar en el JPanel
g2.drawImage(bufferedImage,0,0,this);
}

public void guardarGrafica(File nombreArchivo) {
String fileName = nombreArchivo.getName();
String extension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length());

System.out.println(fileName);
System.out.println(extension);

try{
ImageIO.write(bufferedImage, extension, nombreArchivo);
}catch (IOException ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}catch(java.lang.IllegalArgumentException iaExc){
System.out.println(iaExc.getMessage());
iaExc.printStackTrace();
}
}

}

/**********************************/
/**********************************/
import java.io.File;
import javax.swing.JPanel;

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
guardarPanel guardar = new guardarPanel();
this.getContentPane().add(guardar);
File file = new File("C:\pb.png");
guardar.guardarGrafica(file);
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// //GEN-BEGIN:initComponents
private void initComponents() {

getContentPane().setLayout(new java.awt.GridLayout());

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON _CLOSE);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-397)/2, (screenSize.height-343)/2, 397, 343);
}// //GEN-END:initComponents

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables

}
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