lunes, 13 de abril de 2009

Mostrar imágenes en Java

Dentro del método paint de un panel se puede insertar el código para que se cargue una imagen almacenada en un archivo, para que sea mostrada dentro de dicho panel.

Para incluir una imagen se debe llamar al método drawImage de la clase Graphics. A este método se le pasan cuatro parámetros: El primero de ellos es la referencia al objeto Image o BufferedImage que contiene la imagen a mostrar. El segundo y tercer parámetro indican la posición en la que se mostrará.

Para crear el objeto de la clase Image o BufferedImage se puede utilizar el método read de la clase ImageIO, al que se le para la referencia a un objeto File con la ruta al archivo que contiene la imagen.

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

        BufferedImage img = null;
        int posx=0, posy=0;
        try
        {
            img = ImageIO.read(new File("src/recursos/ejemplo.png"));
        }
        catch (IOException e)
        {
            //Control de excepción si no se encuentra el archivo
        }
        g.drawImage(img, posx, posy, null);
    }
En este ejemplo se mostraría la imagen "ejemplo.png" que se encuentra en la carpeta "src/recursos" del proyecto. Se indica la posición 0,0 por lo que aparecerá en la esquina superior izquierda del panel.

23 comentarios:

  1. muy wueno tu post... yo cacho que nadie te alaba los post en este webborld

    ResponderEliminar
  2. pero para varias imagenes como lo hacemos?

    ResponderEliminar
  3. hola yo tambien tengo la duda de como hacerlo para varias imagenes que tengo en una carpeta

    ResponderEliminar
  4. hola yo tambien tengo la duda de como hacerlo para varias imagenes que tengo en una carpeta

    ResponderEliminar
  5. McAfee loginis made with high-innovation and is one of the most progressive applications. Be that as it may, each advanced stage be it little or enormous faces specialized issues. In the event that you face any sort of issue in McAfee programming attempt to connect with the specialists. These senior experts will propose you the exact answer for any McAfee login page
    mistake without taking a lot of time. Apply as needs be the arrangement and you will dispose of the inquiry. The accessibility is 24*7 for the clients.

    ResponderEliminar
  6. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  7. Now your HP Tango printer setup is complete guide. You can now print high-quality pages. With HP Tango printer you can print using the voice command from Alexa and google assistant. You just need to do HP Tango Printer setup with the help of this blog.

    ResponderEliminar
  8. If you have any doubt for printer setup, driver download, wireless setup, usb setup and troubleshooting follow this link123hp.com/setup 4155

    ResponderEliminar
  9. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  10. It is a valid link to activate www.amazon.com mytv code or Read More in your device. You can watch Amazon prime video on amazon mytv, just you need to enter a 6 digit amazon registration code on your device.

    ResponderEliminar
  11. https://losmonstruosdetony.blogspot.com/2019/04/weird-animals.html
    https://losremediosinformaticos.blogspot.com/2009/04/mostrar-imagenes-en-java.html?showComment=1625737227369#c1469843154765761738
    http://lotharf.blogspot.com/2008/01/software-fr-windows-updates.html
    http://loveactually-blog.blogspot.com/2013/01/digi-scrap-love.html
    http://love-aesthetics.blogspot.com/2011/06/video-quick-hair-routine.html
    http://www.lovedesignbdos.com/2016/03/stay-tuned.html?showComment=1625737234731#c8381687392134747928

    ResponderEliminar
  12. FuboTv is one of the leading streaming television services specializing in sports like the NFL, NBA, MLS, and global soccer. FuboTV connect code will be helpful to use FuboTV with ease. Fubo TV started as a soccer streaming service, it became an all sports service and then a virtual multichannel video programming global distributor. fubo.tv/connect code

    ResponderEliminar

 

Contador de visitas