Several methods of setting background picture in Java

Source: Internet
Author: User



The first method is to load the picture through JLabel.

Create a ImageIcon object to get the background picture.

ImageIcon img = new ImageIcon ("Image/img1.png");

Creates a JLabel object that loads the image img into the JLabel object.

JLabel JL_BG = new JLabel (IMG); Background

Then, set the location and size of the JL_BG.

This.jl_bg.setBounds (0, 0, 600, 400); Set the position and size, first setlayout (null).

Place the JLabel object JL_BG into the layered panel.

This.getlayeredpane (). Add (JL_BG, New Integer (Integer.min_value));((JPanel) This.getcontentpane (). Setopaque (False ); Set Transparent

As in the code above, Getlayeredpane () Gets a layered panel that places the background image at the bottom and can be used as a background image. We know that there is also a ContentPane container in the JFrame, so the bottom of the placement is obscured by contentpane, so you need to add a second line of code to make this transparent. Note that it needs to be cast.


The second method is to draw the background picture through paint.

This method is also a more common method, first you need to create a BufferedImage object and initialize it to null.

BufferedImage BG = null;

The background image is then loaded.

try {bg = imageio.read (New File ("Image/img1.png"));} catch (Exception e) {e.printstacktrace ();}

Finally, after overriding the Paint method, you can draw this background picture by using the DrawImage method.

public void Paint (Graphics g) {g.drawimage (bg, 0, 0, NULL);

It is best to use double buffering, otherwise there may be a splash screen god horse problem. About double buffering, written in front of it.

The end~


Several methods of setting background picture in Java

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.