欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

给有滚动条的文本区域加水印,滚动条水印,public class

来源: javaer 分享于  点击 19357 次 点评:260

给有滚动条的文本区域加水印,滚动条水印,public class


public class ScrollPaneWatermark extends JViewport {       BufferedImage fgimage, bgimage;       TexturePaint texture;       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("Scroll Pane Watermark Hack");        JTextArea ta = new JTextArea();        ta.setText(fileToString(new File("alice.txt")));        ta.setLineWrap(true);        ta.setWrapStyleWord(true);        ScrollPaneWatermark watermark = new ScrollPaneWatermark();        watermark.setBackgroundTexture(new File("clouds.jpg").toURL());        watermark.setForegroundBadge(new File("flyingsaucer.png").toURL());        watermark.setView(ta);        JScrollPane scroll = new JScrollPane();        scroll.setViewport(watermark);        frame.getContentPane().add(scroll);        frame.pack();        frame.setSize(600,600);        frame.show(); }//该片段来自于http://byrx.net
相关栏目:

用户点评