How to add a background image in Java swing and scale the image according to the window size

Source: Internet
Author: User

Some time ago, when I was using Java swing as a client, I needed to add background images to a main panel. So I found some materials on the Internet for research, some netizens say that jlabel is used for implementation by setting its icon attribute. However, I personally feel that this method is very good, and it is easy to cause problems such as the content above being hidden, therefore, I personally prefer to use a method that inherits the jpanel class. In fact, I think it is somewhat similar to the layer concept in the web, you just need to add this "layer" to the bottom of the panel and use it as the bottom, so you can see how to implement it:

Import javax. swing. jpanel; import Java. AWT. graphics; import Java. AWT. graphics2d; import Java. AWT. toolkit; import Java. AWT. image;/*** image panel, the form needs to be added with the background image * @ author waitatlee@163.com */public class customimgpanel extends jpanel {private int width = 0; private int Height = 0; private string imgpath = "";/***** @ Param _ width integer, window width * @ Param _ height integer, window height * @ Param _ imgpath URL of the image, available relative path */Public customimgpanel (INT _ width, int _ height, string _ imgpath) {width = _ width; Height = _ height; imgpath = _ imgpath; setsize (width, height); setvisible (true);}/***** @ Param _ width floating point type, window width * @ Param _ height floating point type, window height * @ Param _ imgpath string, the image URL, which can be relative to */Public customimgpanel (double _ width, double _ height, string _ imgpath) {width = (INT) _ width; Height = (INT) _ height; imgpath = _ imgpath; setsize (width, height); setvisible (true) ;}@ override public void paintcomponent (Graphics GS) {graphics2d G = (graphics2d) GS; super. paintcomponent (g); // draws the background image = toolkit. getdefatooltoolkit (). getimage (getclass (). getresource (imgpath); G. drawimage (image, 0, 0, width, height, this );}}

The code above defines the customimgpanel class. The following describes how to use it:

Double panelwidth = toolkit. getdefatooltoolkit (). getscreensize (). getwidth (); double panelheight = toolkit. getdefatooltoolkit (). getscreensize (). getheight ()-25-25-20; // (two 25 are the height of the title bar of the internal and external windows, and 20 are the height of the update progress bar at the bottom) imgpanel = new customimgpanel (panelwidth, panelheight ,".. /form/images/mainbg.jpg "); this. mainpanel. add (imgpanel,-1); // The parameter-1 is used to keep the background image panel at the bottom of all panels, which is equivalent to the Z-index attribute in the web.

Because my window is maximized by default, the height and width I take are the screen height and width, which can be adjusted as needed, set it during initialization. It will be difficult for the user to change the window size if the image size does not change. This will make the boss look ugly, too, so you need to listen to the size change of a parent window and adjust the image size based on the situation to make it more intelligent. Hey hey, some code has the truth:

/*** Listen to the resize event of the outermost window, and adjust the size of the background image according to the new window size * @ Param EVT */private void formcomponentresized (Java. AWT. event. componentevent EVT) {// todo add your handling code here: Try {This. mainpanel. remove (imgpanel);} catch (exception e) {} imgpanel = NULL; dimension newsize = EVT. getcomponent (). getsize (); imgpanel = new customimgpanel (newsize. getwidth (), newsize. getheight ()-70 ,".. /form/images/mainbg.jpg "); this. mainpanel. add (imgpanel,-1 );}

This is the listening method of the outer jframe. When a size change event is triggered, to avoid overlap of the background panel or other unnecessary exceptions, the remove method is called first, set it to null again, and then regenerate and add it to layer-1. This is a success.

Authentic Real Estate good tea, if you really like tea, be sure to go to see: http://tea0760.taobao.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.