Watermark JScrollPane : Scrollpane « Swing JFC « Java






Watermark JScrollPane

 
/*
Swing Hacks Tips and Tools for Killer GUIs
By Joshua Marinacci, Chris Adamson
First Edition June 2005  
Series: Hacks
ISBN: 0-596-00907-0
Pages: 542
website: http://www.oreilly.com/catalog/swinghks/
*/

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.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;

public class ScrollPaneWatermark extends JViewport {
  BufferedImage fgimage, bgimage;

  TexturePaint texture;

  public ScrollPaneWatermark() {
    super();
    // setOpaque(false);
  }

  public void setBackgroundTexture(URL url) throws IOException {
    bgimage = ImageIO.read(url);
    Rectangle rect = new Rectangle(0, 0, bgimage.getWidth(null), bgimage.getHeight(null));
    texture = new TexturePaint(bgimage, rect);
  }

  public void setForegroundBadge(URL url) throws IOException {
    fgimage = ImageIO.read(url);
  }

  public void paintComponent(Graphics g) {
    // do the superclass behavior first
    super.paintComponent(g);

    // paint the texture
    if (texture != null) {
      Graphics2D g2 = (Graphics2D) g;
      g2.setPaint(texture);
      g.fillRect(0, 0, getWidth(), getHeight());
    }
  }

  public void paintChildren(Graphics g) {
    super.paintChildren(g);
    if (fgimage != null) {
      g.drawImage(fgimage, getWidth() - fgimage.getWidth(null), 0, null);
    }
  }

  public void setView(JComponent view) {
    view.setOpaque(false);
    super.setView(view);
  }

  public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();

    JTextArea ta = new JTextArea();
    for (int i = 0; i < 1000; i++) {
      ta.append(Integer.toString(i) + "  ");
    }

    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    // ta.setOpaque(false);

    ScrollPaneWatermark watermark = new ScrollPaneWatermark();
    watermark.setBackgroundTexture(new File("background.jpg").toURL());
    watermark.setForegroundBadge(new File("foreground.png").toURL());
    watermark.setView(ta);

    JScrollPane scroll = new JScrollPane();
    scroll.setViewport(watermark);

    frame.getContentPane().add(scroll);
    frame.pack();
    frame.setSize(600, 600);
    frame.setVisible(true);
  }

}

           
         
  








Related examples in the same category

1.Creating a JScrollPane Container
2.Create a scrollable list
3.Controlling the scrollbars in a JScrollPaneControlling the scrollbars in a JScrollPane
4.JViewport: Move and View JViewport: Move and View
5.ScrollPane Sample
6.Scrollpane rulerScrollpane ruler
7.Customized ScrollPaneCustomized ScrollPane
8.ScrollPane with imageScrollPane with image
9.Scrolling ProgrammaticallyScrolling Programmatically
10.A simple JScrollPane for a JList componentA simple JScrollPane for a JList component
11.JScrollPane with row and column headersJScrollPane with row and column headers
12.A simple JScrollPane demonstrationA simple JScrollPane demonstration