Hello,

The following program displays two images (ImageIcon type) inside the JTextArea.

Is there a way I can make both of these images transparent sothat what I type on them is visible?

Thanks in advance!

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;

public class Panel extends JFrame
  {
      private BufferedImage image2;
      private ImageIcon image,image1;
      private Timer timer;
      private int count = 0, total = 0;
      private JTextArea area;
      private JScrollPane pane;
      private JTextField field;
      private JButton button;

      public  Panel ()
      {
            setLayout (new FlowLayout());
            image = new ImageIcon ("icon1.jpg");
            image1 = new ImageIcon ("icon2.jpg");
            field = new JTextField (10);
            button = new JButton ("Connect");
            area = new JTextArea (20, 20)
             {
                {
                    setOpaque(true);
                    setBackground (Color.GREEN);
                }

                public void paint(Graphics g)
                {
                    super.paintComponent (g);
                    g.drawImage (image1.getImage(), 0, 0, this);
                    g.drawImage (image.getImage(), 0, 0, this);

                }
            };

            button.addActionListener(

            new ActionListener()
               {
                   public void actionPerformed (ActionEvent e)
                    {
                        area.setText (field.getText());
                    }
               }
            );


            area.setLineWrap(true);
            pane = new JScrollPane(area);
            add(pane);
            add(field);
            add(button);
            setSize(500, 500);
            setVisible (true);
       }



       public static void main (String args[])
       {
          new Panel();

       }

  }

In your paint method, draw your images with an alpha (opacity) less than 1.0
Something like:

    float alpha = 0.5;
    composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    Graphics2D g2D = (Graphics2D) g;
    g2D.setComposite(composite);
    g2D.drawImage(...
This article has been dead for over six months. Start a new discussion instead.