Forum Stats

  • 3,728,222 Users
  • 2,245,576 Discussions
  • 7,853,386 Comments

Discussions

How can I use TextArea with Background Picture ?

843804
843804 Member Posts: 45,751
edited November 2004 in Swing
I want design TextArea with Background Picture


JTextArea j= new JTextArea();
j.setBackground("abc.jpg"); // I known java dosen't support this method

but I want did it. Just like MSN or ICQ can change its skin .

How can I do ??

Comments

  • 843804
    843804 Member Posts: 45,751
    Extend your own JTextArea :)
  • 843804
    843804 Member Posts: 45,751
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    import javax.swing.border.*;
    
    public class CentredBackgroundBorder implements Border {
        private final BufferedImage image;
    
        public CentredBackgroundBorder(BufferedImage image) {
            this.image = image;
        }
    
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            x += (width-image.getWidth())/2;
            y += (height-image.getHeight())/2;
            ((Graphics2D) g).drawRenderedImage(image, AffineTransform.getTranslateInstance(x,y));
        }
    
        public Insets getBorderInsets(Component c) {
            return new Insets(0,0,0,0);
        }
    
        public boolean isBorderOpaque() {
            return true;
        }
    
        //demo
        public static void main(String[] args) throws IOException {
            JFrame.setDefaultLookAndFeelDecorated(true);
            JTextArea area = new JTextArea();
            area.setForeground(Color.GREEN);
            area.setOpaque(false);
            area.read(new FileReader("CentredBackgroundBorder.java"), null);
            String url = "http://today.java.net/jag/bio/JagHeadshot.jpg";
            JScrollPane sp = new JScrollPane(area);
            sp.getViewport().setOpaque(false);
            sp.setViewportBorder(new CentredBackgroundBorder(ImageIO.read(new URL(url))));
    
            JFrame f = new JFrame("BackgroundBorderExample");
            f.getContentPane().add(sp);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(600,400);
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }
  • 843804
    843804 Member Posts: 45,751
    Thank you for your help !!^^
This discussion has been closed.