Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Shell Root en 15 Mayo 2013, 22:24 pm



Título: Imagen en JFrame
Publicado por: Shell Root en 15 Mayo 2013, 22:24 pm
Como hago para ponerle una imagen de fondo a un JFrame? He visto algunos ejemplos pero usan un código muy extenso...


Título: Re: Imagen en JFrame
Publicado por: visualfree en 16 Mayo 2013, 23:10 pm
Código:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GuiTestBed extends JPanel
{
    private static final long serialVersionUID = 1L;

    public static void main(String args[])
    {
        JFrame frame = new JFrame("GUI Test Bed 2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(765,503);
        frame.getContentPane().setBackground(Color.BLUE);
       
        String path = "direccion de tu imagen";
       
        JLabel myLabel = new JLabel();
       
        try
        {
            URL url = new URL(path);
            BufferedImage image = ImageIO.read(url);
            myLabel = new JLabel(new ImageIcon(image));
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
               
       
        frame.getContentPane().add(myLabel);
        //frame.getContentPane().add(new GuiTestBed());
        frame.setVisible(true);
    }
   
    public void paint(Graphics g)
    {
        g.setColor(Color.WHITE);
        g.setFont(new Font("Comic Sans Ms", Font.BOLD, 14));
        g.drawString("Timer: 1:04:32" , 550, 260);
        g.drawString("Edu XP : 22223" , 550, 280);
    }
}





Título: Re: Imagen en JFrame
Publicado por: YairMon en 19 Mayo 2013, 02:02 am
Mira, yo uso una imagen del tamaño del frame.

Por ejemplo hago un dibujito en tuxPaint (Es como el "paint" de window$) y lo guardo al tamaño que necesito.

Entonces:


Código
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3.  
  4. import javax.swing.ImageIcon;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7.  
  8.  
  9. public class ImagenFondo extends JPanel{
  10. // imagen de fondo
  11. private ImageIcon fondo;
  12.  
  13. // frame donde va este panel
  14. JFrame frame = new JFrame("Imagen de fondo");
  15.  
  16. public ImagenFondo(){
  17. // cerrar por defecto, tamanio, visibilidad...
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. frame.setSize(400,400);
  20. frame.setVisible(true);
  21. frame.add(this); // agrego este mismo panel
  22.  
  23. // creo el fondo con la ruta de la imagen
  24. fondo = new ImageIcon("img_fondo-PC.png");
  25.  
  26. // coloco la imagen y le envio las graficas y la posicion
  27. fondo.paintIcon(null, getGraphics(), 0, 0);
  28.  
  29.  
  30. }
  31.  
  32. public void paint(Graphics g){
  33. //pongo un color fondo (no sirve para nada si esta la imagen sobre el)
  34. g.setColor(Color.black);
  35. g.fillRect(0, 0, 400, 400);
  36. //cuando no sale el fondo lo coloco aqui (fondo.paintIcon(null, g, 0, 0);)
  37.  
  38.  
  39. }
  40.  
  41. public static void main(String[] args){
  42. new ImagenFondo();
  43. }
  44. }
  45.  

PD: la imgagen que tengo "img_fondo-PC.png" mide 400x400 y la tengo dentro del proyecto fuera de la carpetas "src" y "bin"