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;
}
}
Michael Dunn - 07 Feb 2008 11:08 GMT
> Hello
>
[quoted text clipped - 4 lines]
> 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.
jmtrg@hotmail.fr - 07 Feb 2008 15:14 GMT
there is no doubt about that ;
but if someone has a more accurate answer,
that would help me : perhaps using AffineTransform
is the solution...
> <jm...@hotmail.fr> wrote in message
>
[quoted text clipped - 13 lines]
> there are many drawImage methods in the Graphics class.
> have a look at the docs, you may find exactly what you're after.
Knute Johnson - 07 Feb 2008 16:06 GMT
> there is no doubt about that ;
> but if someone has a more accurate answer,
[quoted text clipped - 17 lines]
>> there are many drawImage methods in the Graphics class.
>> have a look at the docs, you may find exactly what you're after.
No it isn't. Look at Graphics.drawImage() and you will see the answer.

Signature
Knute Johnson
email s/nospam/knute/
------->>>>>>http://www.NewsDem
jmtrg@hotmail.fr - 07 Feb 2008 17:27 GMT
thanks
it was simple just taking into account the dimension of JPanel
drawImage(bufferedImage,jpanel.getX(),jpanel.getY(),jpanel.getWidth(),jpanel.getHeight(),null);
> jm...@hotmail.fr wrote:
> > there is no doubt about that ;
[quoted text clipped - 29 lines]
> Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
> ------->>>>>>http://www.NewsDem
Lew - 08 Feb 2008 01:17 GMT
> thanks
A: Because it makes the conversation harder to read.
Q: Why is it bad?
A: Posting your response above the text to which you are responding.
Q: What is top-posting?

Signature
Lew
Thomas A. Russ - 07 Feb 2008 18:36 GMT
> 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
> {
[quoted text clipped - 7 lines]
> public JPanelImageBg( String fileName, int mode )
> {
... snip
> }
>
[quoted text clipped - 18 lines]
> }
> }
... snip

Signature
Thomas A. Russ, USC/Information Sciences Institute