Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3117

Spreading an image to the

From "jmtrg" <jmtrg@THRWHITE.remove-dii-this>
Subject Spreading an image to the
Message-ID <ea7cdb8b-c83d-4dd3-b1b6-80eaf8ce0fa0@k39g2000hsf.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
Date 2011-04-27 15:43 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Hello

I found a java code allowing to display an image in a JPanel in 2
ways : either by centering the image or by creating a mosaic picture
of this image ;
I would rather be interested in having this image displayed once and
spreaded to the width and the hight of the JPanel (and this, each time
the JPanel size changes)

anyone has an idea ?

thanks in advance for your suggestions

Jean-Marie


public class JPanelImageBg extends JPanel
{
    private int mode;
    private TexturePaint texture;
    private BufferedImage bufferedImage;

    public static final int CENTER = 0;
    public static final int MOSAIC = 1;

    public JPanelImageBg( String fileName, int mode )
    {
        super();
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        this.mode = mode;
        this.bufferedImage =
this.toBufferedImage(Toolkit.getDefaultToolkit().getImage(fileName));
        this.texture = new TexturePaint(bufferedImage,new Rectangle(0,
0, bufferedImage.getWidth(), bufferedImage.getHeight()));
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        switch( mode )
        {    case CENTER :
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(texture);
                g2d.fillRect(0, 0, getWidth(), getHeight() );
                break;
            case MOSAIC :
                g.setColor(this.getBackground());
                g.fillRect(0,0,getWidth(), getHeight() );
             g.drawImage(bufferedImage,(getWidth()-
bufferedImage.getWidth())/2,(getHeight()-bufferedImage.getHeight())/
2,null);
                break;
            default :
                super.paintComponents(g);
        }
    }
    private BufferedImage toBufferedImage(Image image)
    {    image = new ImageIcon(image).getImage();

        BufferedImage bufferedImage = new
BufferedImage( image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
        Graphics g = bufferedImage.createGraphics();

        g.setColor(Color.white);
        g.fillRect(0, 0, image.getWidth(null),
        image.getHeight(null));
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bufferedImage;
    }

}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Spreading an image to the "jmtrg" <jmtrg@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Spreading an image to "Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
    Re: Spreading an image to "jmtrg" <jmtrg@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Spreading an image to "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

csiph-web