Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

draw transparent componet on bufferedimage?

Thread view: 
aksel.schmidt - 13 Dec 2007 12:27 GMT
i have a component living in the glasspane, partly transparent to
allow see through to the background

now i want to take a 'snapshot' of the transparent component to make
som animations in the glasspane with (slide transition etc.) - but i
cannot make the transparency information appear on the buffered image
- i use:

   public static BufferedImage createImage(JComponent component) {
       Dimension d = component.getSize();
       if (d.width == 0) {
           d = component.getPreferredSize();
           component.setSize(d);
       }

       GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsDevice gs = ge.getDefaultScreenDevice();
       GraphicsConfiguration gc = gs.getDefaultConfiguration();

       BufferedImage image = gc.createCompatibleImage(d.width,
d.height, Transparency.TRANSLUCENT);
       Graphics2D g2d = image.createGraphics();
       component.paint(g2d);
       g2d.dispose();
       return image;
   }

how do i paint a transparent component on an image?

thanks!

Aksel,
denmark
Knute Johnson - 13 Dec 2007 19:54 GMT
> i have a component living in the glasspane, partly transparent to
> allow see through to the background
[quoted text clipped - 30 lines]
> Aksel,
> denmark

Aksel:

It's not really clear what you want to do.  An image can have
transparent pixels.  Is the component.paint() that you are referencing
above draw transparent pixels onto the component?  It is also possible
to draw an image using an AlphaComposite with less than 100% to create a
transparent look.  We either need more code or a better explanation.

Signature

Knute Johnson
email s/nospam/knute/

aksel.schmidt - 14 Dec 2007 04:29 GMT
> We either need more code or a better explanation.
>
> --
>
> Knute Johnson
> email s/nospam/knute

The component i want to 'snapshot' is made of a jpanel with a alpha
value background (half see through) - with other components on it
(more alpha values here) - so it is not simply transparent/not
transparent pixels on the component
/aksel
Knute Johnson - 14 Dec 2007 05:01 GMT
>> We either need more code or a better explanation.
>>
[quoted text clipped - 8 lines]
> transparent pixels on the component
> /aksel

I'm still not sure what you are trying to do Aksel.  But try this and
let me know if that is the direction you are trying to go.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test5 extends JPanel {
    public test5() {
        setLayout(new FlowLayout());

        JLabel l = new JLabel("Label");
        l.setOpaque(false);
        add(l);

        JTextField tf = new JTextField("TextField");
        tf.setOpaque(false);
        add(tf);

        JButton b = new JButton("Image");
        b.setOpaque(false);  // i think this is overridden by laf
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                BufferedImage bi = new
BufferedImage(getWidth(),getHeight(),
                 BufferedImage.TYPE_INT_BGR);
                Graphics g = bi.createGraphics();
                test5.this.paint(g);
                try {
                    ImageIO.write(bi,"JPEG",new File("test.jpg"));
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        });
        add(b);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(new Color(0,0,255,50));
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(Color.BLACK);
        g.drawLine(0,0,getWidth(),getHeight());
        g.drawLine(getWidth(),0,0,getHeight());
        g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                test5 t5 = new test5();
                f.add(t5);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/knute/

aksel.schmidt - 14 Dec 2007 05:15 GMT
thanks for the effort
this code explains my problem:
when i press the button, i want to copy a panel (living in the
glasspane on the left side) into an image and be able to draw this
image on the right side - completely with alpha values etc.
what it does right now is just removing the alpha values...

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class CreateImageTest {
   public static void main(String[] args) {
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       JPanel panel = new JPanel();
       final JPanel panel1 = new JPanel();
       final JPanel panel2 = new JPanel();

       JButton b = new JButton("copy");
       final JLabel label2 = new JLabel();
       label2.setOpaque(false);
       b.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               Image c = createImage(panel1);
               label2.setIcon(new ImageIcon(c));
               panel2.repaint();
           }
       });
       panel1.add(b);
       panel1.add(new JLabel("haj"));
       panel1.add(new MyAplhaComponent());
       panel1.setOpaque(false);

       Dimension preferedSize = new Dimension(100, 100);
       panel1.setPreferredSize(preferedSize);

       panel2.add(label2);
       panel2.setPreferredSize(preferedSize);
       panel2.setOpaque(false);
       panel2.setBackground(Color.BLUE);

       panel.add(panel1);
       panel.add(panel2);
       panel.setBackground(Color.GREEN);
       panel.setOpaque(false);
       frame.setGlassPane(panel);
       frame.getGlassPane().setVisible(true);
       JPanel backgroundPanel = new JPanel();
       JTextArea textArea = new JTextArea("iufpf w fw ffjoifopfree ef
\n" + ", joiewfeeffjepfie wfeifoewfjepf e\n"
               + " huep heufhfewfeefehufehfueoie\n" + "w
eifehfwefefheo");
       textArea.setPreferredSize(new Dimension(200, 200));
       backgroundPanel.add(textArea);
       frame.getContentPane().add(backgroundPanel,
BorderLayout.CENTER);
       frame.pack();
       frame.setVisible(true);
   }

   public static BufferedImage createImage(JComponent component) {
       Dimension d = component.getSize();
       if (d.width == 0) {
           d = component.getPreferredSize();
           component.setSize(d);
       }

//        GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
//        GraphicsDevice gs = ge.getDefaultScreenDevice();
//        GraphicsConfiguration gc = gs.getDefaultConfiguration();
//
//        BufferedImage image = gc.createCompatibleImage(d.width,
d.height, Transparency.TRANSLUCENT);
//        Graphics2D g2d = image.createGraphics();
//        component.paint(g2d);
//        g2d.dispose();

       BufferedImage bi = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_BGR);
       Graphics g = bi.createGraphics();
       component.paint(g);
       return bi;
   }

   public static class MyAplhaComponent extends JComponent {
       {
           setPreferredSize(new Dimension(40, 40));
       }

       @Override
       protected void paintComponent(Graphics g) {
           Graphics2D g2d = (Graphics2D) g;
           g.setColor(new Color(255, 0, 0, 140));
           g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
           g.drawLine(0, 0, 24, 25);
       }
   }
}

/aksel
Andrew Thompson - 14 Dec 2007 07:02 GMT
>thanks for the effort
>this code explains my problem:
>when i press the button, i want to copy a panel (living in the
>glasspane on the left side) into an image and be able to draw this
>image on the right side - completely with alpha values etc.
>what it does right now is just removing the alpha values...

Like this?

<sscce>
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class CreateImageTest {
  public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      final JPanel panel = new JPanel();
      final JPanel panel1 = new JPanel();
      final JPanel panel2 = new JPanel();
      final JPanel backgroundPanel = new JPanel();

      JButton b = new JButton("copy");
      final JLabel label2 = new JLabel();
      label2.setOpaque(false);
      b.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              Image c = createImage(backgroundPanel, panel1);
              label2.setIcon(new ImageIcon(c));
              panel2.repaint();
          }
      });
      panel1.add(b);
      panel1.add(new JLabel("haj"));
      panel1.add(new MyAplhaComponent());
      panel1.setOpaque(false);

      Dimension preferedSize = new Dimension(100, 100);
      panel1.setPreferredSize(preferedSize);

      panel2.add(label2);
      panel2.setPreferredSize(preferedSize);
      panel2.setOpaque(false);
      panel2.setBackground(Color.BLUE);

      panel.add(panel1);
      panel.add(panel2);
      panel.setBackground(Color.GREEN);
      panel.setOpaque(false);
      frame.setGlassPane(panel);
      frame.getGlassPane().setVisible(true);
      JTextArea textArea = new JTextArea(
        "iufpf w fw ffjoifopfree ef\n" +
        ", joiewfeeffjepfie wfeifoewfjepf e\n" +
        " huep heufhfewfeefehufehfueoie\n" +
        "weifehfwefefheo");
      textArea.setPreferredSize(new Dimension(200, 200));
      backgroundPanel.add(textArea);
      frame.getContentPane().add(backgroundPanel,
        BorderLayout.CENTER);
      frame.pack();
      frame.setVisible(true);
  }

  public static BufferedImage createImage(JComponent background,
      JComponent component) {
      Dimension d = component.getSize();
      if (d.width == 0) {
          d = component.getPreferredSize();
          component.setSize(d);
      }
      BufferedImage bi = new BufferedImage(d.width, d.height,
         BufferedImage.TYPE_INT_ARGB);
      Graphics g = bi.createGraphics();
      background.paint(g);
      component.paint(g);
      return bi;
  }

  public static BufferedImage createImage(JComponent component) {
      Dimension d = component.getSize();
      if (d.width == 0) {
          d = component.getPreferredSize();
          component.setSize(d);
      }

      //  Note carefully the 'A'
      BufferedImage bi = new BufferedImage(d.width, d.height,
        BufferedImage.TYPE_INT_ARGB);
      Graphics g = bi.createGraphics();
      component.paint(g);
      return bi;
  }

  public static class MyAplhaComponent extends JComponent {
      {
          setPreferredSize(new Dimension(40, 40));
      }

      @Override
      protected void paintComponent(Graphics g) {
          Graphics2D g2d = (Graphics2D) g;
          g.setColor(new Color(255, 0, 0, 140));
          g2d.setStroke(new BasicStroke(10,
            BasicStroke.CAP_ROUND,
            BasicStroke.JOIN_BEVEL));
          g.drawLine(0, 0, 24, 25);
      }
  }
}
</sscce>

Signature

Andrew Thompson
http://www.physci.org/

Andrew Thompson - 14 Dec 2007 07:07 GMT
>>thanks for the effort
>>this code explains my problem:
[quoted text clipped - 6 lines]
>
><sscce>
...
>       //  Note carefully the 'A'
>       BufferedImage bi = new BufferedImage(d.width, d.height,
>         BufferedImage.TYPE_INT_ARGB);

Or 'not'.  It seems to work just fine without it.  Just like the original..

        ...
        BufferedImage.TYPE_INT_RGB);

Signature

Andrew Thompson
http://www.physci.org/

aksel.schmidt - 14 Dec 2007 07:45 GMT
again -thanks for the effort - but it doesn't work

the right side of the glass pane is supposed to draw the image of the
left side of the glasspane - complete with half-transparent red line
etc ABOVE the text that is on the background textarea

/aksel
Andrew Thompson - 14 Dec 2007 09:24 GMT
>again -thanks for the effort - but it doesn't work

According to my understanding of the requirement, it works
here using Java 1.6.0_02 on Win XP.

Here is a screenshot of the GUI as it first appears.
<http://www.physci.org/test/screenshot/transs01.png>

>the right side of the glass pane is supposed to draw the image of the
>left side of the glasspane - complete with half-transparent red line
>etc ABOVE the text that is on the background textarea

And a screenshot after I click the button..
<http://www.physci.org/test/screenshot/transs02.png>

Are you seeing something different?

Signature

Andrew Thompson
http://www.physci.org/

aksel.schmidt - 14 Dec 2007 12:31 GMT
hi

its the same as what i see - but not the desired functionality
as you see, the image drawn on the right side is not transparent - the
text in the background is not seen anymore as desired

/aksel
aksel.schmidt - 14 Dec 2007 12:41 GMT
found a solution: use paintComponents intead of paint..
package aks;

import java.awt.BasicStroke;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class CreateImageTest {
   public static void main(String[] args) {
       JFrame frame = new JFrame();
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       final JPanel panel = new JPanel();
       final JPanel panel1 = new JPanel();
       final JPanel panel2 = new JPanel();
       final JPanel backgroundPanel = new JPanel();

       JButton b = new JButton("copy");
       final JLabel label2 = new JLabel();
       label2.setOpaque(false);
       b.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               Image c = createImage(panel1);
               label2.setIcon(new ImageIcon(c));
               panel2.repaint();
           }
       });
       panel1.add(b);
       panel1.add(new JLabel("haj"));
       panel1.add(new MyAplhaComponent());
       panel1.setOpaque(false);

       Dimension preferedSize = new Dimension(100, 100);
       panel1.setPreferredSize(preferedSize);

       panel2.add(label2);
       panel2.setPreferredSize(preferedSize);
       panel2.setOpaque(false);
       panel2.setBackground(Color.BLUE);

       panel.add(panel1);
       panel.add(panel2);
       panel.setBackground(Color.GREEN);
       panel.setOpaque(false);
       frame.setGlassPane(panel);
       frame.getGlassPane().setVisible(true);
       JTextArea textArea = new JTextArea("iufpf w fw ffjoifopfree ef
\n" + ", joiewfeeffjepfie wfeifoewfjepf e\n"
               + " huep heufhfewfeefehufehfueoie\n" +
"weifehfwefefheo");
       textArea.setPreferredSize(new Dimension(200, 200));
       backgroundPanel.add(textArea);
       frame.getContentPane().add(backgroundPanel,
BorderLayout.CENTER);
       frame.pack();
       frame.setVisible(true);
   }

   public static BufferedImage createImage(JComponent component) {
       Dimension d = component.getSize();
       if (d.width == 0) {
           d = component.getPreferredSize();
           component.setSize(d);
       }

       // Note carefully the 'A'
       BufferedImage bi = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_ARGB);
       Graphics g = bi.createGraphics();
       component.paintComponents(g);
       return bi;
   }

   public static class MyAplhaComponent extends JComponent {
       {
           setPreferredSize(new Dimension(40, 40));
       }

       @Override
       protected void paintComponent(Graphics g) {
           Graphics2D g2d = (Graphics2D) g;
           g.setColor(new Color(255, 0, 0, 140));
           g2d.setStroke(new BasicStroke(10, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
           g.drawLine(0, 0, 24, 25);
       }
   }
}
Andrew Thompson - 14 Dec 2007 13:34 GMT
>found a solution: use paintComponents intead of paint..

Congratulations!  *Now* I understand what you meant.
A picture paints a thousand words.  :-)

Well, at least, 'now' after I fixed the line-wrap that broke
both examples when I tried to compile them.  It is best
to keep code posting width to less than around 62 chars,
so line-wrap is not a problem.  My SSCCE earlier,
showed how to reorganise the code so that the longest
lines were shorter.

Here is a tool to help check line width.
<http://www.physci.org/twc.jnlp>

But finally..  (to self and nobody else in particular).

>        // Note carefully the 'A'
>        BufferedImage bi = new BufferedImage(d.width, d.height,
>BufferedImage.TYPE_INT_ARGB);

Ha!  I 'knew' transparency in the BufferedImage
would somehow be important.  ;-)

Signature

Andrew Thompson
http://www.physci.org/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.