How Java Swing adds a background picture and scales it according to the size of the window

Source: Internet
Author: User

Some time ago, when I was using Java Swing as a client, have to add a background picture on the main panel needs, so they find some data on the internet, some netizens said with JLabel to do, by setting its Icon property to achieve, but personal feeling this practice is very hack, hehe, And this approach is easy to bring to the top of the content is obscured and so on, so the individual prefers to use an inherited JPanel class to implement the method, in fact, I feel it and the Web layer concept is somewhat similar to it, just want to add this "layer" to the bottom of the panel, as the bottom of the, OK, To see how to achieve it:

Import Javax.swing.JPanel;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Toolkit;
Import Java.awt.Image;  /** * Picture panel, the form needs to add a background image with * @author waitatlee@163.com */public class Customimgpanel extends jpanel{private int width
    = 0;
    private int height = 0;
    Private String Imgpath = ""; /** * * @param _width integral type, window width * @param _height integral type, window height * @param the URL of the _imgpath picture, available relative path * * p
        Ublic 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, picture URL, available relative
        * * 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);
        Picture background Image image = Toolkit.getdefaulttoolkit (). GetImage (GetClass (). GetResource (Imgpath));
    G.drawimage (image, 0, 0,width,height, this);
 }
}

Above is the code that defines the class of Customimgpanel, and here's how to use it:

Double panelwidth = Toolkit.getdefaulttoolkit (). Getscreensize (). getwidth ();
Double panelheight = Toolkit.getdefaulttoolkit (). Getscreensize (). GetHeight ()-25-25-20;//(two 25 is the height of the title bar of two windows inside and outside, 20 is the height of the bottom update progress bar)
Imgpanel = new Customimgpanel (Panelwidth,panelheight, ". /form/images/mainbg.jpg ");
This.mainPanel.add (IMGPANEL,-1)//parameter-1 is to keep this background picture panel at the bottom of all panels, equivalent to the Z-index property in the Web

Because my window default is maximized, I take the height and width of the screen, this everyone according to their own situation to adjust slightly, OK, initialization time set, that when the user changes the size of the window if the picture size does not change will be difficult to see, this will lead to the boss's face is also very difficult to see, Oh, so need to monitor the size of a parent window changes, and according to the situation to adjust the size of the picture, so that it appears more intelligent, hey, there is code has the truth:

/**
     * Listens to the Resize event of the outermost window and adjusts the size of the background picture 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 outer jframe listening method, when the size of the event trigger, in order to avoid overlapping background panel or other unnecessary exceptions, so first call the Remove method to remove, then set to null empty once, and finally rebuilt and added to the 1 layer. That's it.


Authentic real estate of good tea, if you actually like tea, be sure to go to see: http://tea0760.taobao.com

More wonderful technical sharing please visit: http://www.heyswan.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.