The procedure is as follows:
Override the paintComponent (Graphics g) method of JViewport. Convert Graphics to Graphics2D, use setPaint (Paint paint) method and fiilRect () method,
Use TexturePaint to fill the JViewport and tile the watermark (background ).
/**
* @ Param args
* JTextArea can have background images through JScrollPane and JViewport, which can be completed by tiled TexturePaint,
* To implement the background, you only need to rewrite part of the paintComponent (Graphics g) code.
* Steps: 1. Set texture = new TexturePaint (Image img, Rectangle rect );
* 2. override the setView (JComponent view) method and set opaque (false) of the view component first ). view. setOpaque (false); the view component does not draw its own background, thus highlighting the JViewport background.
* 2. Use super. paintComponent (g) in paintComponent (Graphics g );
* 3. Use Graphics2D to setPaint (texture); then fillRect background;
*/
Import java. awt. Graphics;
Import java. awt. Graphics2D;
Import java. awt. Rectangle;
Import java. awt. TexturePaint;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. IOException;
Import java.net. MalformedURLException;
Import java.net. URL;
Import javax. imageio. ImageIO;
Import javax. swing. JComponent;
Import javax. swing. JFrame;
Import javax. swing. JScrollPane;
Import javax. swing. JTextArea;
Import javax. swing. JViewport;
Import mai. util. FileUtil;
Public class ScrollPaneWatermark extends JViewport {
BufferedImage fgImg, bgImg;
TexturePaint texture;
// Set The View background
Public void setBackgroundTexture (URL url) throws IOException {
BgImg = ImageIO. read (url );
Rectangle rect = new Rectangle (0, 0, bgImg. getWidth (), bgImg. getHeight ());
Texture = new TexturePaint (bgImg, rect );
}
// Set the front decoration
Public void setForegroundBadge (URL url) throws IOException {
FgImg = ImageIO. read (url );
}
// Use TexturePaint to draw a component
Public void paintComponent (Graphics g ){
Super. paintComponent (g );
If (texture! = Null ){
Graphics2D g2 = (Graphics2D) g;
G2.setPaint (texture );
G2.fillRect (0, 0, getWidth (), getHeight ());
}
}
// Set The View component to opaque = false;
Public void setView (JComponent view ){
View. setOpaque (false );
Super. setView (view );
}
Public static void main (String [] args) throws MalformedURLException, Exception {
// TODO Auto-generated method stub
JFrame jf = new JFrame ("watermark ScrollPane ");
JTextArea jta = new JTextArea ();
Jta. setText (FileUtil. fileToString (new File ("f:/storm_log.txt"). toURI (). toURL ()));
Jta. setLineWrap (true );
Jta. setWrapStyleWord (true );
ScrollPaneWatermark watermark = new ScrollPaneWatermark ();
Watermark. setBackgroundTexture (new File ("G:/PHOTO/webpage material/background material/bj2.gif"). toURI (). toURL ());
Watermark. setView (jta );
JScrollPane jsp tutorial = new JScrollPane ();
Jsp. setViewport (watermark );
Jf. getContentPane (). add (jsp );
Jf. pack ();
Jf. setVisible (true );
}
}
I first thought of directly rewriting the paintComponent (Graphics g) method of JTextArea, and directly drawImage the image. Later I found that the effect was not very good.
2. Later I found that basically JTextArea will first put it into JScrollPane and then present it. JScrollPane will display the scroll area by rendering the window Viewport.
So here we can rewrite JViewport to achieve the effect of partial background with Windows.