Can i put a jpeg as a background for a panel or frame in swing?? if so what
should I be aware of?
Could I make it the background of my frame and set the panel to opaque?
would this effect and animation?
ak - 27 Dec 2003 20:43 GMT
> Can i put a jpeg as a background for a panel or frame in swing??
yes, you can, not only jpeg - any image.
public class ImagePanel extends JPanel {
Image img;
public void paintComponent(Graphics g) {
Insets insets = getInsets();
if(img != null) {
g.drawImage(img, insets.left, insets.top, null);
}
}
}
> Could I make it the background of my frame and set the panel to opaque?
yes
> would this effect and animation?
for animation you need more then one image.
____________
http://reader.imagero.com the best java image reader.
Evil Monkey - 28 Dec 2003 17:24 GMT
> > Can i put a jpeg as a background for a panel or frame in swing??
> yes, you can, not only jpeg - any image.
[quoted text clipped - 8 lines]
> }
> }
how do I tell it to locate the jpeg?? in the above code?? Do i just put it
in the same file??
thanks
willy - 30 Dec 2003 04:47 GMT
Evil,
To load and draw (the path passed to the ImageIcon ctor will matter, and no
exception will be thrown if it can't find the image, it just won't draw the
image):
final ImageIcon imageIcon = new ImageIcon( "your.gif" );
imagePanel = new JPanel( ){
Image image = imageIcon.getImage();
public void paint( Graphics g ){
super.paint( g );
g.drawImage( image, 0, 0, this );
}
};
Also note that setting the opaqueness will have an effect... so calling the
super may or may not be necessary as it may only paint the background.
WT-
> > > Can i put a jpeg as a background for a panel or frame in swing??
> > yes, you can, not only jpeg - any image.
[quoted text clipped - 13 lines]
>
> thanks
hiwa - 28 Dec 2003 00:47 GMT
> Can i put a jpeg as a background for a panel or frame in swing??
Yes, you can.
> what should I be aware of?
Nothing particular.
> Could I make it the background of my frame and set the panel to opaque?
Yes, you could.
> would this effect and animation?
Animation needs a rapid sequence of redrawing background and
foreground(characters and their movements). Consume enough time and
tinker around Component#paint() method and/or update() method.
Christopher Wong - 28 Dec 2003 17:12 GMT
> Can i put a jpeg as a background for a panel or frame in swing?? if so what
> should I be aware of?
You might want to keep in mind that JPEGs and PNGs in Java eat up a lot of
memory when displayed. Depending on resolution and depth, you could add
megabytes to your app's footprint by throwing up a JPG background. See Bug
Parade's bug ID 4190828:
http://developer.java.sun.com/developer/bugParade/bugs/4190828.html
One workaround is to use GIFs instead.
Chris
ak - 28 Dec 2003 20:43 GMT
> You might want to keep in mind that JPEGs and PNGs in Java eat up a lot of
> memory when displayed. Depending on resolution and depth, you could add
[quoted text clipped - 4 lines]
>
> One workaround is to use GIFs instead.
another workaround is to read _first_ any small jpeg image.
____________
http://reader.imagero.com the best java image reader.