Add image backgrounds to jframe and jtextarea

Source: Internet
Author: User
Tags scale image

I. Move the background position with scrolling: </p> <p> import java. awt. graphics; </p> <p> import javax. swing. imageIcon; <br/> import javax. swing. JFrame; <br/> import javax. swing. JScrollPane; <br/> import javax. swing. JTextArea; </p> <p>/** <br/> * display an image background in JTextArea (the background moves with the scrolling position) <br/> * @ author Doumi <reposted Please retain the author and source> <br/> * @ blog http://blog.csdn.net/mq612 <br/> */<br/> public class Test extends JFrame {</p> <p> private static final long serialVersionUID = 4785452373598819719L; </p> <p> private JScrollPane sp = null; </p> <p> private JTextArea text = null; </p> <p> private ImageIcon imageIcon = null; </p> <p> public Test () {<br/> super ("JTextArea"); </p> <p> imageIcon = new ImageIcon ("photo.jpg "); <br/> text = new JTextArea () {<br/> private static final long serialVersionUID =-8220994963464909915L; </p> <p >{< br/> setOpaque (false); // set transparency <br/>}</p> <p> protected void paintComponent (Graphics g) {<br/> g. drawImage (imageIcon. getImage (), 0, 0, this); <br/> super. paintComponent (g); <br/>}< br/>}; <br/> sp = new JScrollPane (text); </p> <p> this. getContentPane (). add (sp); </p> <p> this. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); <br/> this. setSize (360,260); <br/> this. setVisible (true); <br/>}</p> <p> public static void main (String args []) {<br/> new Test (); <br/>}</p> <p>Mport java. awt. event. *; <br/> import javax. swing. *; <br/> import java. awt. *; </p> <p> public class BackgroundImage extends JFrame <br/> {<br/> JScrollPane scrollPane; <br/> ImageIcon; <br/> Image image; </p> <p> public BackgroundImage () <br/> {<br/> icon = new ImageIcon ("bgpanel.jpg "); </p> <p> JPanel = new JPanel () <br/> {<br/> protected void paintComponent (Graphics g) <br/>{< br/> // Dispaly image at full size </p> <p> g. drawImage (icon. getImage (), 0, 0, null ); </p> <p> // Scale image to size of component </p> <p> // Dimension d = getSize (); </p> <p> // g. drawImage (icon. getImage (), 0, 0, d. width, d. height, null); </p> <p> // Fix the image position in the scroll pane </p> <p> // Point p = scrollPane. getViewport (). getViewPosition (); </p> <p> // g. drawImage (icon. getImage (), p. x, p. y, null); </p> <p> super. paintComponent (g); <br/>}< br/>}; <br/> panel. setOpaque (false); <br/> panel. setPreferredSize (new Dimension (400,400); <br/> scrollPane = new JScrollPane (panel); <br/> getContentPane (). add (scrollPane); </p> <p> JButton button = new JButton ("Hello"); <br/> panel. add (button); <br/>}</p> <p> public static void main (String [] args) <br/>{< br/> BackgroundImage frame = new BackgroundImage (); <br/> frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); <br/> frame. setSize (300,300); <br/> frame. setLocationRelativeTo (null); <br/> frame. setVisible (true); <br/>}< br/>}

Images are not moved

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.