Painting behind transparent components

Hi All,
My problem is quite simple. I want to paint on a panel to which I then add a JEditorPane inside a JScrollPane both of which are transparent (using setOpaque(false)). I'm expecting whatever I paint to appear behind any text rendered by the JEditorPane as the component on which it is begin painted is behind the JEditorPane.
What actually happens is that everything drawn on the panel appears above everything else. The following is a self contained example that should display the Google logo BEHIND the GNU GPL but instead paints it on top. Any ideas what the problem is?
import javax.swing.*;
import java.net.URL;
import java.awt.*;
public class Test extends JFrame
     public static void main(String args[]) throws Exception
          Test t = new Test();
     public Test() throws Exception
          setSize(500,500);
          setTitle("Test");
          JPanel backPanel = new JPanel(new BorderLayout())
               ImageIcon background = new ImageIcon(new URL("http://www.google.com/images/logo.gif"));
               public void paint(Graphics g)
                    super.paint(g);
                    g.drawImage(background.getImage(),50,150,this);
          backPanel.setOpaque(true);
          backPanel.setBackground(Color.white);
          JEditorPane textPane = new JEditorPane(new URL("http://www.gnu.org/licenses/gpl.txt"));
          textPane.setEditable(false);
          textPane.setOpaque(false);
          JScrollPane scroller = new JScrollPane(textPane);
          scroller.setOpaque(false);
          scroller.getViewport().setOpaque(false);
          backPanel.add(scroller,BorderLayout.CENTER);
          getContentPane().add(backPanel,BorderLayout.CENTER);
          setVisible(true);
}

You can paint directly on the text component:
import java.awt.*;
import javax.swing.*;
public class TextPaneBackground extends JFrame
     ImageIcon icon;
     public TextPaneBackground()
          icon = new ImageIcon("mong.jpg");
          JTextPane textPane = new JTextPane()
               public void paintComponent(Graphics g)
                    g.drawImage(icon.getImage(), 0, 0, null);
                    super.paintComponent(g);
          textPane.setOpaque( false );
          textPane.setText( "This is some text" );
          JScrollPane scrollPane = new JScrollPane( textPane );
          getContentPane().add( scrollPane );
     public static void main(String[] args)
          TextPaneBackground frame = new TextPaneBackground();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.setSize(200, 200);
          frame.setVisible(true);
}Not positive, but I believe this approach would be more efficient. When Swing trys to paint a non-opaque component it needs to search the components ancestors until it finds an opaque component to paint the background. So I think in your case you have multiple ancestor, JViewport, JScrollPane, JPanel.

Similar Messages

  • Clone stamp - paint on transparent

    Can anyone explain why my layer disappears when I click "paint on transparent" when using the clone tool? It doesn't happen when using the normal paint tool, only with the clone tool. Sometimes I can get it to work if I duplicate the layer, click the box, then turn off the original layer. But sometimes when I duplicate the layer it messes up all my clones.

    Thanks for your replies, but it's not any of those things. For example, if you take a jpg, no alpha channel, no masks, just in a clean comp by itself and paint a clone stamp stroke on it and then check "paint on transparent," the whole layer disappears. Whereas with a normal, non-clone paint stroke it will turn off the BG but keep the paint.
    If it matters, the point of my wanting to do this is using mocha, mocha import, and RG warp to stabilize shots for painting out wires and rigs. It wouldn't be an issue except that this combination of things results in a minor quality loss so I want to contain it to the paint strokes only.
    Actually, I just figured out a workaround while writing this, for anyone who cares: the issue seems to be that you can't use the layer you're painting on as a source layer for cloning. But if you duplicate the layer, open the original in one layer window and the duplicate in another, you can use the duplicate to sample from and paint on the original. Then when you check "paint on transparent" it works as promised.

  • Interaction with Content behind Transparent Flex App

    Hi,
    I have a Flex application with wmode=transparent and a transparent section that allows whatever is underneath it on the HTML page to show through.  In this case, what's showing through is another Flash application.  This works fine, but I can't interact with the Flash application showing through.
    I tried setting the z-index of the Flash app to something greater than my Flex app, and that worked, but unfortunately that solution doesn't work for my case because the Flex application has components that occasionally overlay in that area, and setting the z-index of the Flash app to be higher means those components are always stuck behind it.  Also unfortunately I don't control the loading of that Flash app, so I can't load/embed it into my app to get around this problem.
    Are there any other ways to do this?
    Cheers,
    Karthik

    UPDATE:
    So, I spent an entire work-day on trying to get this to
    work.... I finally ended up hacking the flex html filter library to
    display the formatted text as best as I could. It basically created
    a bunch of "<textformat>" tags. There were still a few
    layout/indentation issues. So, I tested it, and deployed to our
    test website, and got approval from one of the managers. Then, I
    come in this morning, and the all my work is gone. "Did I deploy
    the files to right server? I know I tested it last night....", I
    asked myself. The dates on the files were from last night. Odd. So
    I ask my supervisor if I was looking at the correct website for the
    server where I deployed the files.... "Oh yeah", he said, "I had to
    roll that back, because [the manager who gave me the content]
    didn't like how it looked." Nevermind that the files included other
    changes as well (it was a nightly build, the source control for the
    source files are done through a different source control program on
    a different server), so now the whole thing looks screwed up.
    Oh well.... I really need a blog to complain about this....
    Thanks everyone for your help.

  • White box behind transparent images with drop shadow in InDesign CC 2014 (v.10) but not in InDesign CC (v.9)

    Hi all,
    Please can you help? No matter what settings I use in my print options in InDesign CC 2014 (v.10), ALL transparent images with a drop shadow are printing with a white box behind the image. If I remove the drop shadow, all prints fine. I know this is an old issue that happens with ID sometimes but I have been lucky enough to never have had it happen to me before! (All images are regular flattened TIFF images with clipping paths).
    Also, feathered edges are not printing correctly and are sharp instead of feathered.
    When I export the document to IDML and open it up in InDesign CC (v.9) all prints perfectly with no white boxes and feathered edges prints perfectly as well. I have compared all of the print settings (including color management, transparency flattener, etc.) between both versions and they are exactly the same. I am printing to a Xerox color laser printer. Can anyone offer any advice?
    Thank you so much,
    Christine

    Okay...false alarm! It turns out that the issue was with my PRINTER settings (the settings you customize after hitting the “Printer” button at the bottom of the Print dialogue box in ID CC. I thought I had triple checked this but it seems there was one setting I missed earlier…adjusting the print brightness seemed to cause the issue. SO GLAD to know it was not InDesign…20 or so sheets/prints later….and thankful to know what was causing the issue! Thank you so much Steve for helping at any rate:) Much appreciated:)

  • How painting behind a JComponent ?

    Hi all,
    I'm trying to paint stuff like shadows, frames (not borders), etc. behind JComponents.
    So every shape only intersects with the relative JComponent's one.
    Cannot paint these stuff in the container's paintComponent() method since more widgets can have one or more of them, so I guess It should be better do the job in every child's paintComponent() method.
    What follows was the code I tested but doesn't work.
    Any hints ? Thanks in advance.
    Lucio
    import java.awt.*;
    import javax.swing.*;
    public class UpButton extends JButton {
        Dimension dimension = new Dimension(100, 100);
        Point point = new Point(100, 100);
        public UpButton(String text) {
         setSize(dimension);
         setLocation(point);
         setBackground(new Color(255,186,26));
         setText(text);
        public void paintComponent(Graphics g) {
         Component c = (Component) getParent();
         Graphics2D g2 = (Graphics2D)
                                      c.getGraphics().create(0,0,dimension.width,dimension.height);
         g2.translate(-(point.x + 10), -(point.y + 5));
         g2.setPaint(Color.gray);
         g2.fill(new Rectangle(dimension));
         g2.dispose();
         super.paintComponent(g);
        public static void main(String[] args) {
         JFrame jFrame = new JFrame();
         jFrame.getContentPane().setLayout(null);
         jFrame.add(new UpButton("Test"));
         jFrame.setSize(300,300);
         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jFrame.setVisible(true);
    }

    I'm very grateful to you, weebib
    I was missing the setClip method, now perfectly works.
    It doesn't seems to me too difficult (now ;-)
    Here my modified version :
    import java.awt.*;
    import javax.swing.*;
    public class UpButton extends JButton {
        Dimension dimension = new Dimension(100, 100);
        Point point = new Point(100, 100);
        public UpButton(String text) {
         setSize(dimension);
         setLocation(point);
         setBackground(new Color(255,186,26));
         setText(text);
        public void paintComponent(Graphics g) {
         Graphics2D g2 = (Graphics2D) g.create();
         g2.translate(10, 5);
         g2.setClip(0, 0, 100, 100);
         g2.setPaint(Color.gray);
         g2.fill(new Rectangle(dimension));
         g2.dispose();
         super.paintComponent(g);
        public static void main(String[] args) {
         JFrame jFrame = new JFrame();
         jFrame.getContentPane().setLayout(null);
         jFrame.add(new UpButton("Test"));
         jFrame.setSize(300,300);
         jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         jFrame.setVisible(true);
    }

  • Paint - set transparent background?

    Can anyone tell me how to set an image (.gif) transparent background in Windows 8 Paint (ver. 6.2)?  Been scratching head for a couple of hours now but can't for the life of me see how to set a transparent colour.  I'm sure that the XP version
    of pint allowed this.
    Thanks,

    There is a way! it is now hidden in a ribbon command.
    Under the Image section of the ribbon, click the "Select" drop-down menu. The bottom command is called "Transparent Selection" and it can be toggled on or off.
    Just like the older version of of paint, the Color #2 is the transparent color (so you could remove a white or black background, just make Color #2 white or black, respectively)
    FYI, the other command I had a hard time finding was to make the canvas bigger/smaller (but not shrink/stretch the image). It used to be called "Image attributes" but now you go File>Properties, and you can adjust the size there.
    Would you PLEASE walk me through it ?  I have this jpeg, I have always see the Transparent deal under "Select", but I do NOT know HOW to "select" the background to make it transparent?
    Please help and Thanks.

  • SwingUtilities.paintComponent - problems with painting childs of components

    Maybe this question was mention already but i cant find any answer...
    Thats what could be found in API about method paintComponent in SwingUtilities:
    Paints a component c on an arbitrary graphics g in the specified rectangle, specifying the rectangle's upper left corner and size. The component is reparented to a private container (whose parent becomes p) which prevents c.validate() and c.repaint() calls from propagating up the tree. The intermediate container has no other effect.
    The component should either descend from JComponent or be another kind of lightweight component. A lightweight component is one whose "lightweight" property (returned by the Component isLightweight method) is true. If the Component is not lightweight, bad things map happen: crashes, exceptions, painting problems...
    So this method perfectly works for simple Java components like JButton / JCheckbox e.t.c.
    But there are problems with painting JScrollBar / JScrollPane (only background of this component appears).
    I tried to understand why this happens and i think the answer is that it has child components that does not draw at all using SwingUtilities.paintComponent()
    (JScrollBar got at least 2 childs - 2 buttons on its both sides for scroll)
    Only parent component appears on graphics - and thats why i see only gray background of scrollbar
    So is there any way to draw something like panel with a few components on it if i have some Graphics2D to paint on and component itself?
    Simple example with JScrollPane:
    JScrollPane pane = new JScrollPane(new JLabel("TEST"));
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    SwingUtilities.paintComponent(g2d, pane, new JPanel(), 0, 0, 200, 200);Edited by: mgarin on Aug 11, 2008 7:48 AM

    Well the thing you advised could work... but this wont be effective, because i call repaint of the area, where Components painted, very often..
    So it will lag like hell when i make 10-20 repaints in a second... (for e.g. when i drag anything i need mass repainting of the area)
    Isnt there any more optimal way to do painting of the sub-components?
    As i understand when any component (even complicated with childs) paints in swing - its paint method calls its child components paint() to show them... can this thing be reproduced to paint such components as JScrollPane?

  • Use a different paint for different components

    Dear all,
    I would really appreciate it if you could help me with an AWT problem that I am facing. I want to develop an application in which several images will be printed into the same frame (i.e. a background image, several images containing objects etc.). But I want the images to be "updated" according to the user's prefereneces (e.g. when he/she presses button A, only the image containing the object A should be redrawn, whilst if he/she presses the button B, only the image containing object B should be repainted). Until this moment, I have implemented a class containing a paint method where I include different if cases in order to repaint the corresponding objects on the user's input. I was wondering if there is any other easier way to achieve my ends (e.g. use different paint functions in the same class, making each paint responsible for the drawing of a certain image, and then call the corresponding paint on the user's input). I would really appreciate if you could help me by giving me some advice and/or some sample source code.
    Thank you in advance,
    Sincerely yours,
    Enginejim

    Hi NK,
    if you use Transparent Data Encryption you can choose between column encryption and tablespace encryption (or a mix)
    and the master key for both can only be stored in the same wallet (in 11gR2 we have a unified master key for both).
    Also an important concept of TDE is that it is tranparent: application users do not need to know any encryption key (passphrase),
    when you are asking that
    need to encrypt certain columns and these columns should be accessible to certain users who know a "key phrase" - and not othersthen you are making a common conceptual mistake, which is to confuse encryption with access control, there's actually a good
    statement about this in the security guide here: Principle 1: Encryption Does Not Solve Access Control Problems
    http://docs.oracle.com/cd/E11882_01/network.112/e16543/data_encryption.htm#i1006159
    So if some users should not have access to certain data, please solve this with access controls in combination with VPD,
    trying to solve it with encryption is simply ill-advised,
    greetings,
    Harm ten Napel
    Edited by: hnapel on Jan 24, 2013 8:14 AM

  • Painting With Transparency

    I'm trying to paint (or erase ... my problem is the same with either tool) a layer with transparent pixels. I get the foreground color. I've tried setting the mode to "Clear". What am I doing wrong?
    (Photoshop Elements 13)

    After reading your post again and assuming your image is in grayscale mode this might be what your thinking of:
    Ctrl+Alt+1 selects all the white areas  (Ctrl+Alt+3 on photoshop cs4-cs6)
    Ctrl+Shift+I inverts the selection so the black lines are selected
    Ctrl+J Layer via copy sends the black lines to a separate layer
    What version of photoshop and operating system are you using now?

  • Calling paint(g) on components

    When can you call paint(g) on a component? Does it have to be on screen for it to have any effect? ie if I call paint(g) on a component when it is not visible will it have any effect on the g I pass to it? (g is a Graphics instance.)

    Hi,
    that actually depends on the component. JComponent for example first checks whether its size has width and height greater that zero. Unless it doesn't paint. And this may happen if you don't set the size manually if its not on screen.
    Other components may have some check if they're visible or something.
    However this depends on the component in question.
    Michael

  • Drawing Inside Keeps Painting Behind Other Brush Strokes

    I am currently using the draw inside feature on an object of mine.  I find that when i come back to it, like if i save and close the program and then come back, whatever new stuff i paint vanishes behind rather than appearing on top.  The main solid colored object I always have to send behind before i could paint over it every single time.  But now that I have some details over top of that, they are cooperating the same way and need sent behind as well im assuming, with the main object being at the very back.  I'd hate to try and select everything because everything around it would need locked and this and that to select what i need and its just allot of hassle, especially if i have to do it every time i re open the program.  Shouldn't new brush strokes paint above everything rather than below?  Any help would be highly appreciated.  

    I submitted something similar to this about two version back. I know
    when I'm trying to draw a long line, it would be nice to be able to use
    some sort of guide. I think I was imagining a tool that you could hold
    with a second hand while drawing with the wacom pen. I think they really
    could corner the market if they came up with something like this.
    Unfortunately, I think they hold back...after all, if they give it all
    away, they have nothing to upgrade to...right?
    On a similar note, I vaguely remember back when ver 2or 3 of Illustrator
    came out and one could fit graphics(text?) in an envelope. At the time,
    it was a plugin that was subsequently removed.
    For the next 50;) versions I whined about this. Finally, I think it was
    version 9 or 10 that brought this feature back to Illustrator.
    So given that track record, I think we might get it in version 17. Let's
    cross our fingers. ;)

  • URGENT!! How to avoid paint all the components?

    In the following code sample, when you move the mouse, the top-level JPanel (with the name p) repaints itself with a red circle. The strange thing is that also the 4 JButtons repaint themselves!! (this is shown by the System.out). If the container of the 4 JButtons and the "p" had too many components, then the repaint of "p" gets very slow!! If i delete the line "super.paintComponent(g)" in the class "Myb", then the JButtons do not show correctly.
    How can i avoid that the 4 JButtons do not repaint themselves all the time?
    import javax.swing.*;
    import java.awt.*;
    public class Fr extends javax.swing.JFrame {
        private javax.swing.JPanel p;
        private Ap ap1;
        private Myb b4;
        private Myb b3;
        private Myb b2;
        private Myb b1;
        public Fr() {
            p = new javax.swing.JPanel();
            ap1 = new Ap();
            b1 = new Myb();
            b2 = new Myb();
            b3 = new Myb();
            b4 = new Myb();
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    System.exit(0);
            p.setLayout(null);
            p.setBackground(new java.awt.Color(153, 153, 0));
            p.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseMoved(java.awt.event.MouseEvent evt) {
                    ap1.repaintWith(SwingUtilities.convertPoint(p,evt.getPoint(),ap1));
            ap1.setFont(new java.awt.Font("Dialog", 0, 11));
            ap1.setOpaque(false);
            p.add(ap1);
            ap1.setBounds(-10, -40, 500, 500);
            b1.setText("b1");
            p.add(b1);
            b1.setBounds(0, 0, 150, 150);
            b2.setText("b2");
            p.add(b2);
            b2.setBounds(0, 150, 150, 150);
            b3.setText("b3");
            p.add(b3);
            b3.setBounds(150, 0, 150, 150);
            b4.setText("b4");
            p.add(b4);
            b4.setBounds(150, 150, 150, 150);
            getContentPane().add(p, java.awt.BorderLayout.CENTER);
            pack();
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setSize(new java.awt.Dimension(500, 500));
            setLocation((screenSize.width-500)/2,(screenSize.height-500)/2);
        public static void main(String args[]) {
            new Fr().show();
    class Ap extends JPanel
        private Point p;
        public Ap(){
            super();
            p = new Point(10,10);
        public void repaintWith(Point p){
            this.p = p;
            repaint();
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillOval(p.x-5,p.y-5,10,10);
    class Myb extends JButton
        int x = 0;
        public Myb() {
            super();
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            System.out.println(getText()+"-->"+(x++));
    }

    I did that, but still nothing!
    I inserted these lines!
            JPanel subp = new javax.swing.JPanel();
            subp.setBorder(new javax.swing.border.LineBorder(Color.blue, 2));
            subp.setOpaque(false);
            p.add(subp);
            subp.setBounds(200, 0, 200, 200);
            ap1.setOpaque(false);
            subp.add(ap1);
            ap1.setBounds(200, 0, 200, 200);

  • Transparent components?

    anyone know if there's a way to make a UIScrollBar
    transparent? I'd like it to be about 50% so some of the background
    image is showing through it, but you can still see that it's
    there... I tried looking around the help docs and online, couldn't
    find anything :( is it even possible or would I have to skin it
    myself or something like that?

    I had made it completely invisible by just editing it, not a
    new skin. I found the instructions online. I'd imagine if one can
    make it invisible or hcnage the color you can change the
    transparency. Sorry that I don't remember the code but like I said,
    it was available.

  • How to paint transparent icon on component?

    I am trying to paint a transparent image onto a swing component. The image is painted, but the transparent regions of the gif are painted as grey! How to overcome this problem?
    class PrettyComboBox extends JComboBox{
    ImageIcon imageIcon;
    public PrettyComboBox(){
    setOpaque(false);
    this.imageIcon = new ImageIcon("transparentRegions.gif");
    protected void paintComponent(Graphics g){
    super.paintComponent(g);
    imageIcon.paintIcon(this, g, 0, 0);
    Any help or comments greatly appreciated!

    Actually I've figured out how to make the combobox editable and also paint an icon. Now if only I could get the transparency of the icon working, I'd be set!
    BTW the following code is how I use a ListCellRenderer in conjunction with an editable ComboBox, the trick is to set the renderer AFTER setting the combobox to be editable.
    class PrettyComboBox extends JComboBox{
    ImageIcon imageIcon;
    public PrettyComboBox(Vector vec){
    super(vec);
    setEditable(true);
    setRenderer(new ComboBoxRenderer());
    setOpaque(false);
    this.imageIcon = new ImageIcon("hasTransparentRegion.gif");
    protected void paintComponent(Graphics g){
    super.paintComponent(g);
    imageIcon.paintIcon(this, g, 0, 0);
    public class ComboBoxRenderer extends JLabel implements ListCellRenderer {
    public ComboBoxRenderer(){
    setOpaque(true);
    public Component getListCellRendererComponent(
    JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus) {
    setText((String)value);
    //add image to label, if desired
    return this;
    thanks for the comments!
    how the heck do you get code snippets
    to indent properly on this messageboard?
    -Tom

  • Cannot paint over a jpeg suddenly, paint shows up behind on same layer

    When i place an image (jpeg) into photoshop and try to paint over it it will not let me. instead it paints behind the jpeg on that layer. Also, if I create a layer mask on the jpeg it will not paint over it to paint out areas.
    It always worked fine, this issue has come up within the last couple weeks.
    Help would be greatly appreciated, thank you.

    Hi artvector99,
    The first thing that comes to mind is that the mode of the Brush Tool is set to something other than Normal in the tool options bar.

Maybe you are looking for