Help with .paint (g) and JTable

0

I have a big doubt. I want to use the .paint method to be able to save the whole window in an image, the problem is that the information that is inside of it is not painted, I explain:

What I want is for an image to be created as (it is done screenshot):

And what I get with the following code is the following:

How can I save the information in the table as well? The table is named tbcarrito

    // EXTRA: generar una imagen con la factura:
public void GenerarImagenFactura(){
    File fichero = new File(FacturaCajeroGen.getFactura_ID() + ".jpg");
    String formato = "jpg";

    // Creamos la imagen para dibujar en ella.
    BufferedImage imagen = new BufferedImage(this.getContentPane().getWidth(),
                    this.getContentPane().getHeight(), BufferedImage.TYPE_INT_RGB);

    // Hacemos el dibujo
    Graphics g = imagen.getGraphics();
    this.getContentPane().paint(g);

    // Escribimos la imagen en el archivo.
    try {
        ImageIO.write(imagen, formato, fichero);
    } catch (IOException e) {
        System.out.println("Error de escritura");
    }
}

Full Code:

public class GUI_Factura extends javax.swing.JFrame {

public static ControlMySQL DatosFacturaGen;
public static ResultSet aux;
Cliente ClienteCajeroGen;
Factura FacturaCajeroGen;
Personal CajeroGen;

public GUI_Factura(ControlMySQL DatosCajero, Cliente ClienteCajero, Factura FacturaCajero, Personal Cajero) {
    initComponents();
    DatosFacturaGen = DatosCajero;
    ClienteCajeroGen = ClienteCajero;
    FacturaCajeroGen = FacturaCajero;
    CajeroGen = Cajero;
    GenerarFactura();
}

// HERE VA initComponents ();

/**
 * @param args the command line arguments
 */
public void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI_Factura.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI_Factura.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI_Factura.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI_Factura.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GUI_Factura(DatosFacturaGen, ClienteCajeroGen, FacturaCajeroGen, CajeroGen).setVisible(true);
        }
    });
}

public void GenerarFactura(){
    // Información de la Empresa:
    txtEmpresaNombre.setText("MegaMaxi el Condado");
    txtEmpresaCajero.setText(CajeroGen.getNombre() + " " + CajeroGen.getApellido());

    // Información de la Factura:
    txtFacturaID.setText(FacturaCajeroGen.getFactura_ID());
    txtFacturaFecha.setText(GenerarFecha());

    // Datos del Cliente:
    txtClienteNombre.setText(ClienteCajeroGen.getNombre() + " " + ClienteCajeroGen.getApellido());
    txtClienteCI.setText(ClienteCajeroGen.getCI());
    txtClienteDireccion.setText(ClienteCajeroGen.getDireccion());
    txtClienteTelefono.setText(ClienteCajeroGen.getNumero());

    // Generar el Carrito de Compras
    GenerarCarrito();

    // Generar Valores a Pagar
    txtSubtotal.setText(String.valueOf(FacturaCajeroGen.getSubtotal()));
    txtIVA.setText(String.valueOf(FacturaCajeroGen.getIVA()));
    txtDescuento.setText(String.valueOf(FacturaCajeroGen.getDescuento()));
    txtTotal.setText(String.valueOf(FacturaCajeroGen.getTotal()));

    // Generar Imagen de la Factura
    GenerarImagenFactura();
}

public String GenerarFecha(){
    Date date = new Date();
    DateFormat hourdateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
    return hourdateFormat.format(date);
}

public void GenerarCarrito(){
    DefaultTableModel model = (DefaultTableModel) tbCarrito.getModel();
    model.setRowCount(0);
    try {
        aux = DatosFacturaGen.consultarFacturaProductos(FacturaCajeroGen.getFactura_ID());
        while (aux.next())
        {
            model.addRow(new Object[]
            {aux.getString(1),aux.getString(2),aux.getString(3),
             aux.getString(4),aux.getString(5),aux.getString(6)});
        }
    } catch (SQLException ex) {
    }
}

// EXTRA: generar una imagen con la factura:
public void GenerarImagenFactura(){
    File fichero = new File(FacturaCajeroGen.getFactura_ID() + ".jpg");
    String formato = "jpg";

    // Creamos la imagen para dibujar en ella.
    BufferedImage imagen = new BufferedImage(this.getContentPane().getWidth(),
                    this.getContentPane().getHeight(), BufferedImage.TYPE_INT_RGB);

    // Hacemos el dibujo
    Graphics g = imagen.getGraphics();
    this.getContentPane().paint(g);

    // Escribimos la imagen en el archivo.
    try {
        ImageIO.write(imagen, formato, fichero);
    } catch (IOException e) {
        System.out.println("Error de escritura");
    }
}

// Variables declaration - do not modify                     
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblClienteCI;
private javax.swing.JLabel lblClienteDatos;
private javax.swing.JLabel lblClienteDireccion;
private javax.swing.JLabel lblClienteNombre;
private javax.swing.JLabel lblClienteTelefono;
private javax.swing.JLabel lblDescuento;
private javax.swing.JLabel lblEmpresaCajero;
private javax.swing.JLabel lblEmpresaInformacion;
private javax.swing.JLabel lblEmpresaNombre;
private javax.swing.JLabel lblFacturaFecha;
private javax.swing.JLabel lblFacturaID;
private javax.swing.JLabel lblFacturaInformacion;
private javax.swing.JLabel lblFacturaTitulo;
private javax.swing.JLabel lblIVA;
private javax.swing.JLabel lblSubtotal;
private javax.swing.JLabel lblTotal;
private javax.swing.JPanel panCarrito;
private javax.swing.JPanel panClienteDatos;
private javax.swing.JPanel panEmpresaInformacion;
private javax.swing.JPanel panFacturaInformacion;
private javax.swing.JTable tbCarrito;
private javax.swing.JTextField txtClienteCI;
private javax.swing.JTextField txtClienteDireccion;
private javax.swing.JTextField txtClienteNombre;
private javax.swing.JTextField txtClienteTelefono;
private javax.swing.JTextField txtDescuento;
private javax.swing.JTextField txtEmpresaCajero;
private javax.swing.JTextField txtEmpresaNombre;
private javax.swing.JTextField txtFacturaFecha;
private javax.swing.JTextField txtFacturaID;
private javax.swing.JTextField txtIVA;
private javax.swing.JTextField txtSubtotal;
private javax.swing.JTextField txtTotal;
// End of variables declaration                   

}

    
asked by Christian Galarza 15.08.2018 в 01:21
source

0 answers