Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

jpg as background?

Thread view: 
Evil Monkey - 27 Dec 2003 19:57 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.