Foros del Web » Programación para mayores de 30 ;) » Java »

hacer paneles y hacerlos imagen

Estas en el tema de hacer paneles y hacerlos imagen en el foro de Java en Foros del Web. tengo el siguiente codigo @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Javascript : Ver original Panel bottomPanel = new Panel ( ) ; texto = new TextField ( "TextField" ...
  #1 (permalink)  
Antiguo 26/01/2011, 15:16
Avatar de valdo_kof  
Fecha de Ingreso: noviembre-2009
Ubicación: San Juan del Rio, Qro
Mensajes: 192
Antigüedad: 14 años, 5 meses
Puntos: 16
hacer paneles y hacerlos imagen

tengo el siguiente codigo

Código Javascript:
Ver original
  1. Panel bottomPanel = new Panel();
  2. texto= new TextField("TextField");
  3. boton= new Button("Button");
  4. check= new Checkbox("Checkbox");
  5. bottomPanel.add(texto);
  6. bottomPanel.add(boton);
  7. bottomPanel.add(check);

con eso se supone q se crea un panel con una caja de texto, un boton y un checkbox, tambien quisiera saber como puedo agregar mas artefactos o labels o cajas de texto en una determinada area de mi panel y ya despues como se podria hacer q el panel con todo su contenido se guardara en una imagen digamos en c:/?

si alguien puede ayudarme muchas gracias
  #2 (permalink)  
Antiguo 27/01/2011, 02:36
Avatar de chuidiang
Colaborador
 
Fecha de Ingreso: octubre-2004
Mensajes: 3.774
Antigüedad: 19 años, 6 meses
Puntos: 454
Respuesta: hacer paneles y hacerlos imagen

Hola:

Si con "añadir a una determinada area" te refieres a controlar la posición donde salen dentro del panel, echa un ojo al tema de layouts http://chuwiki.chuidiang.org/index.p...Uso_de_Layouts

En cuanto a guardarlo como una imagen, aquí tienes algo parecido
Código java:
Ver original
  1. // creas una imagen
  2. BufferedImage imagen = new BufferedImage(panelBottom.getWidth(),
  3.    panelBottom.getHeight(), BufferedImage.TYPE_INT_RGB);
  4.  
  5. // le dices al panel que se dibuje sobre la imagen
  6. bottomPanel.paint(imagen.getGraphics());
  7.  
  8. // y salvas la imagen
  9. ImageIO.write(imagen, "jpg", new File("foto.jpg"));

Más detalles aquí http://chuwiki.chuidiang.org/index.p...gen_en_fichero

Se bueno.
__________________
Apuntes Java
Wiki de Programación
  #3 (permalink)  
Antiguo 27/01/2011, 08:01
Avatar de valdo_kof  
Fecha de Ingreso: noviembre-2009
Ubicación: San Juan del Rio, Qro
Mensajes: 192
Antigüedad: 14 años, 5 meses
Puntos: 16
Respuesta: hacer paneles y hacerlos imagen

chuidiang gracias por responder

Añadi el codigo q me recomendaste y si me guarda la imagen pero solo es un cuadro totalmente negro no muestra ni el boton, ni el check no se que este haciendo mal si me pueden ayudar gracias de antemano

asi me queda el codigo

Código java:
Ver original
  1. import java.awt.Button;
  2. import java.awt.Checkbox;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Panel;
  6. import java.awt.TextField;
  7. import java.awt.geom.AffineTransform;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import javax.imageio.ImageIO;
  12.  
  13. public class Main {
  14.     private static TextField texto;
  15.     private static Button boton;
  16.     private static Checkbox check;
  17.  
  18.     public static void main(String[] args) throws IOException {
  19.               // Crear panel
  20. Panel bottomPanel = new Panel();
  21. bottomPanel.setSize(200, 200);
  22. // Crear artefactos
  23. texto= new TextField("TextField");
  24. boton= new Button("Button");
  25. check= new Checkbox("Checkbox");
  26.  
  27. // Agregarlos al panel
  28. bottomPanel.add(texto);
  29. bottomPanel.add(boton);
  30. bottomPanel.add(check);
  31. BufferedImage imagen = new BufferedImage(bottomPanel.getWidth(),   bottomPanel.getHeight(), BufferedImage.TYPE_INT_RGB);
  32. // le dices al panel que se dibuje sobre la imagen
  33. bottomPanel.paint(imagen.getGraphics());
  34. // y salvas la imagen
  35. ImageIO.write(imagen, "jpg", new File("c:/panel.jpg"));
  36.     }
  37. }
  #4 (permalink)  
Antiguo 27/01/2011, 09:23
Avatar de chuidiang
Colaborador
 
Fecha de Ingreso: octubre-2004
Mensajes: 3.774
Antigüedad: 19 años, 6 meses
Puntos: 454
Respuesta: hacer paneles y hacerlos imagen

No sé, he copiado tu ejemplo y efectivamente sale un cuadro negro. He copiado el del enlace y funciona bien. Las únicas diferencias que veo son:
- En el enlace la ventana está visible cuando se quiere salvar.
- En el enlace se usan componentes swing en vez de awt.

Intenta ir pasando de un ejemplo al otro poco a poco a ver cuándo deja/empieza a funcionar.

Se bueno.
__________________
Apuntes Java
Wiki de Programación
  #5 (permalink)  
Antiguo 02/02/2011, 15:04
Avatar de valdo_kof  
Fecha de Ingreso: noviembre-2009
Ubicación: San Juan del Rio, Qro
Mensajes: 192
Antigüedad: 14 años, 5 meses
Puntos: 16
Respuesta: hacer paneles y hacerlos imagen

chuidiang
intente hacer lo q me recomendaste pero no pude e intente hacerlo de otra forma.

mira he echo esto hasta el momento con este codigo me guarda una imagen con las separaciones que necesito para una etiqueta pero no me guarda lo q le añado al panel

si cambas de JPanel a JFrame se puede ver lo que quiero por separado, la imagen con las diviciones q necesito para mi etiqueta y en pantalla arroja los datos como los necesito distribuidos

no se si me expliq pero ya ejecutado se ve

te dejo el codigo a ver si me puedes decir q estoy haciendo mal

Código Java:
Ver original
  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.geom.Rectangle2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. import java.io.FileOutputStream;
  9. import javax.imageio.ImageIO;
  10. import net.sourceforge.barbecue.Barcode;
  11. import net.sourceforge.barbecue.BarcodeException;
  12. import net.sourceforge.barbecue.BarcodeFactory;
  13. import net.sourceforge.barbecue.BarcodeImageHandler;
  14. import net.sourceforge.barbecue.output.OutputException;
  15.  
  16. public class Ventana extends JFrame        
  17.  {    
  18.      public Ventana() throws BarcodeException, FileNotFoundException, OutputException, IOException
  19.      {
  20.         super ();
  21.          this.setLayout(new GridBagLayout());
  22.          this.setSize(300, 295);
  23.          //this.setSize(1470, 1350);
  24.          this.setBackground(Color.white);
  25.          GridBagConstraints constraints = new GridBagConstraints();
  26.        Barcode barcode = BarcodeFactory.createCode39("N14U-93110-00",false);
  27. barcode.setDrawingText(false);
  28.  barcode.setBarHeight(45);
  29.  barcode.setBarWidth(1);
  30.  FileOutputStream fos = new FileOutputStream("c:/N14U-93110-00.jpeg");
  31.   BarcodeImageHandler.writeJPEG(barcode, fos);
  32.    ImageIcon icon = new ImageIcon("c:/N14U-93110-00.jpeg");
  33. Barcode barcode_q = BarcodeFactory.createCode39("Q8",false);
  34. barcode_q.setDrawingText(false);
  35.  barcode_q.setBarHeight(45);
  36.  barcode_q.setBarWidth(1);
  37.  FileOutputStream fos_q = new FileOutputStream("c:/q8.jpeg");
  38.   BarcodeImageHandler.writeJPEG(barcode_q, fos_q);
  39.    ImageIcon icon_q = new ImageIcon("c:/q8.jpeg");
  40.  
  41.    Barcode barcode_l = BarcodeFactory.createCode39("L1111",false);
  42. barcode_l.setDrawingText(false);
  43.  barcode_l.setBarHeight(45);
  44.  barcode_l.setBarWidth(1);
  45.  FileOutputStream fos_l= new FileOutputStream("c:/l1111.jpeg");
  46.  BarcodeImageHandler.writeJPEG(barcode_l, fos_l);
  47.  ImageIcon icon_l = new ImageIcon("c:/l1111.jpeg");
  48.  JLabel country=new JLabel("HARADA INDUSTRY OF AMERICA");
  49.  country.setFont(new java.awt.Font("Arial", 1, 10));
  50.  JLabel item=new JLabel("ITEM No.");
  51.  item.setFont(new java.awt.Font("Arial", 1, 8));
  52.  JLabel item_comp=new JLabel("(N)");
  53.  item_comp.setFont(new java.awt.Font("Arial", 1, 8));
  54.  JLabel item_no=new JLabel("14U-93110-00");
  55.  item_no.setFont(new java.awt.Font("Arial", 1, 12));
  56.  JLabel image=new JLabel();
  57.  image.setIcon(icon);
  58.  JLabel espacio=new JLabel(" ");
  59.  JLabel qty=new JLabel("8");
  60.  qty.setFont(new java.awt.Font("Arial", 1, 12));
  61.  JLabel Quantity=new JLabel("Quantity");
  62.  Quantity.setFont(new java.awt.Font("Arial", 1, 8));
  63.  JLabel q=new JLabel("(Q)");
  64.  q.setFont(new java.awt.Font("Arial", 1, 8));
  65.  JLabel image_q=new JLabel();
  66.  image_q.setIcon(icon_q);
  67.  JLabel l=new JLabel("1111");
  68.  l.setFont(new java.awt.Font("Arial", 1, 12));
  69.  JLabel lot=new JLabel("Lot No.");
  70.  lot.setFont(new java.awt.Font("Arial", 1, 8));
  71.  JLabel lo=new JLabel("(L)");
  72.  lo.setFont(new java.awt.Font("Arial", 1, 8));
  73.  JLabel image_l=new JLabel();
  74.  image_l.setIcon(icon_l);
  75.  JLabel c=new JLabel("28200 9E000");
  76.  c.setFont(new java.awt.Font("Arial", 1, 12));
  77.  JLabel cus=new JLabel("Customer No.");
  78.  cus.setFont(new java.awt.Font("Arial", 1, 8));
  79.  JLabel country2=new JLabel("ASSEMBLED IN MEXICO.");
  80.  country2.setFont(new java.awt.Font("Arial", 1, 10));
  81.  Rectangle2D.Double border = new Rectangle2D.Double(0, 0, 243.84, 243.84);
  82.  
  83.          constraints.gridx = 1;
  84.          constraints.gridy = 0;
  85.          constraints.gridwidth = 1;
  86.          constraints.gridheight = 1;
  87.         constraints.weightx=0;
  88.         constraints.weightx=0;
  89.          this.add (country, constraints);
  90.  
  91.          constraints.gridx = 0;
  92.          constraints.gridy = 1;
  93.          constraints.gridwidth = 1;
  94.          constraints.gridheight = 1;
  95.          this.add (item, constraints);
  96.  
  97.      constraints.gridx = 0;
  98.          constraints.gridy = 2;
  99.          constraints.gridwidth = 1;
  100.          constraints.gridheight = 1;
  101.          this.add (item_comp, constraints);
  102.  
  103.          constraints.gridx = 1;
  104.          constraints.gridy = 2;
  105.          constraints.gridwidth = 1;
  106.          constraints.gridheight = 1;
  107.          this.add (item_no, constraints);
  108.  
  109.      constraints.gridx = 1;
  110.          constraints.gridy = 3;
  111.          constraints.gridwidth = 1;
  112.          constraints.gridheight = 1;
  113.          this.add (image, constraints);
  114.  
  115.          constraints.gridx = 0;
  116.          constraints.gridy = 4;
  117.          constraints.gridwidth = 1;
  118.          constraints.gridheight = 1;
  119.          this.add (espacio, constraints);
  120.  
  121.          constraints.gridx = 0;
  122.          constraints.gridy = 5;
  123.          constraints.gridwidth = 1;
  124.          constraints.gridheight = 1;
  125.          this.add (Quantity, constraints);
  126.  
  127.          constraints.gridx = 0;
  128.          constraints.gridy = 6;
  129.          constraints.gridwidth = 1;
  130.          constraints.gridheight = 1;
  131.          this.add (q, constraints);
  132.  
  133.          constraints.gridx = 1;
  134.          constraints.gridy = 6;
  135.          constraints.gridwidth = 1;
  136.          constraints.gridheight = 1;
  137.          this.add (qty, constraints);
  138.  
  139.          constraints.gridx = 1;
  140.          constraints.gridy = 7;
  141.          constraints.gridwidth = 1;
  142.          constraints.gridheight = 1;
  143.          this.add (image_q, constraints);
  144.  
  145.          constraints.gridx = 0;
  146.          constraints.gridy = 8;
  147.          constraints.gridwidth = 1;
  148.          constraints.gridheight = 1;
  149.          this.add (espacio, constraints);
  150.  
  151.          constraints.gridx = 0;
  152.          constraints.gridy = 9;
  153.          constraints.gridwidth = 1;
  154.          constraints.gridheight = 1;
  155.          this.add (lot, constraints);
  156.  
  157.          constraints.gridx = 0;
  158.          constraints.gridy = 10;
  159.          constraints.gridwidth = 1;
  160.          constraints.gridheight = 1;
  161.          this.add (lo, constraints);
  162.  
  163.          constraints.gridx = 1;
  164.          constraints.gridy = 10;
  165.          constraints.gridwidth = 1;
  166.          constraints.gridheight = 1;
  167.          this.add (l, constraints);
  168.  
  169.          constraints.gridx = 1;
  170.          constraints.gridy = 11;
  171.          constraints.gridwidth = 1;
  172.          constraints.gridheight = 1;
  173.          this.add (image_l, constraints);
  174.  
  175.          constraints.gridx = 0;
  176.          constraints.gridy = 12;
  177.          constraints.gridwidth = 1;
  178.          constraints.gridheight = 1;
  179.          this.add (espacio, constraints);
  180.  
  181.          constraints.gridx = 0;
  182.          constraints.gridy = 13;
  183.          constraints.gridwidth = 1;
  184.          constraints.gridheight = 1;
  185.          this.add (cus, constraints);
  186.  
  187.          constraints.gridx = 1;
  188.          constraints.gridy = 13;
  189.          constraints.gridwidth = 1;
  190.          constraints.gridheight = 1;
  191.          this.add (c, constraints);
  192.  
  193.          constraints.gridx = 1;
  194.          constraints.gridy = 14;
  195.          constraints.gridwidth = 1;
  196.          constraints.gridheight = 1;
  197.          this.add (country2, constraints);
  198.          
  199.           BufferedImage imagen = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
  200.          Graphics g = imagen.getGraphics();
  201.          g.drawRect(1, 1, 296, 108);
  202.          g.drawRect(1,109, 296, 72);
  203.           g.drawRect(1,181, 296, 72);
  204.           g.drawRect(1,253, 296, 38);
  205.           g.setXORMode(Color.black);
  206.           this.paint(g);
  207.      
  208.           ImageIO.write(imagen,"jpg",new File("c:/prueba.jpg"));
  209.           //ImageIO.write(imagen,"jpg",new File("c:/prueba.jpg"));
  210.      }
  211.      public static void main(String args[]) throws BarcodeException, FileNotFoundException, OutputException, IOException{
  212.      Ventana obj= new Ventana();
  213.      obj.setVisible(true);
  214.      }
  215. }
  #6 (permalink)  
Antiguo 02/02/2011, 15:08
Avatar de chuidiang
Colaborador
 
Fecha de Ingreso: octubre-2004
Mensajes: 3.774
Antigüedad: 19 años, 6 meses
Puntos: 454
Respuesta: hacer paneles y hacerlos imagen

Hola:

Solo por probar y descartar, intenta meterlo todo en un JFrame y saca el JFrame al fichero, con getContentPane().paint(graphics).

Se bueno.
__________________
Apuntes Java
Wiki de Programación
  #7 (permalink)  
Antiguo 02/02/2011, 15:29
Avatar de valdo_kof  
Fecha de Ingreso: noviembre-2009
Ubicación: San Juan del Rio, Qro
Mensajes: 192
Antigüedad: 14 años, 5 meses
Puntos: 16
Respuesta: hacer paneles y hacerlos imagen

me aparece lo mismo, no cambia en nada

tengo una duda

en la clase pongo public class Ventana extends JFrame esto es lo mismo q hacer un frame o lo hago desde el inicio click derecho new Jframe

Etiquetas: paneles
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:48.