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


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

Spreading an image to the

Started by"jmtrg" <jmtrg@THRWHITE.remove-dii-this>
First post2011-04-27 15:43 +0000
Last post2011-04-27 15:43 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  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

#3117 — Spreading an image to the

From"jmtrg" <jmtrg@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectSpreading an image to the
Message-ID<ea7cdb8b-c83d-4dd3-b1b6-80eaf8ce0fa0@k39g2000hsf.googlegroups.com>
  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

[toc] | [next] | [standalone]


#3119 — Re: Spreading an image to

From"Michael Dunn" <michael.dunn@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Spreading an image to
Message-ID<47aae6dd$1@dnews.tpgi.com.au>
In reply to#3117
  To: comp.lang.java.gui

<jmtrg@hotmail.fr> wrote in message 
news:ea7cdb8b-c83d-4dd3-b1b6-80eaf8ce0fa0@k39g2000hsf.googlegroups.com...
> 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)
<code snipped>

g.drawImage(..)

there are many drawImage methods in the Graphics class.
have a look at the docs, you may find exactly what you're after.

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3121 — Re: Spreading an image to

From"jmtrg" <jmtrg@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Spreading an image to
Message-ID<e320b092-c2bb-41fa-acd6-5775e52f0d26@v4g2000hsf.googlegroups.com>
In reply to#3119
  To: comp.lang.java.gui
there is no doubt about that ;
but if someone has a more accurate answer,
that would help me : perhaps using AffineTransform
is the solution...

On 7 f=E9v, 12:08, "Michael Dunn" <m_od...@yahoo.com> wrote:
> <jm...@hotmail.fr> wrote in message
>
> news:ea7cdb8b-c83d-4dd3-b1b6-80eaf8ce0fa0@k39g2000hsf.googlegroups.com...>=
 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)
>
> <code snipped>
>
> g.drawImage(..)
>
> there are many drawImage methods in the Graphics class.
> have a look at the docs, you may find exactly what you're after.

---
 * 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

[toc] | [prev] | [next] | [standalone]


#3125 — Re: Spreading an image to

From"tar" <tar@THRWHITE.remove-dii-this>
Date2011-04-27 15:43 +0000
SubjectRe: Spreading an image to
Message-ID<ymifxw41umx.fsf@blackcat.isi.edu>
In reply to#3117
  To: comp.lang.java.gui
jmtrg@hotmail.fr writes:

> 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 ;

Is it just me, or is the code for CENTER and MOSAIC reversed?

 > 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 )
 >     {
... snip
 >     }
 > 
 >     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);
 >         }
 >     }

... snip

-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web