Link to home
Start Free TrialLog in
Avatar of ec000wmr
ec000wmr

asked on

off-screen images with transparent backgrounds problem

I have some equations that need to be graphed, so I wrote a little class (MyPanel) that extends
JPanel and plots the appropriate points using the Graphics object's drawing methods in
paintComponent.

I want MyPanel to be transparent so that the background color of its container could show thru.  To that end I did a setOpaque(false) in the constructor.  Everything works fine when I plot the points of the equations directly using the Graphics object passed into paintComponent and
call super.paintComponent(g) at the end of MyPanel's paintComponent method.

In the course of things I decided that since there are a LOT of math operations that need to
be done to determine each point to plot, I could make things like scrolling more efficient if I
created an off-screen image buffer that could be created once, with different sections displayed
as the user scrolls.  So I used MyPanel's createImage(w,h) method, did an
Graphics ig = image.getGraphics(), plotted the points with ig and then used my original Graphics object's drawImage method to display the off-screen buffer.  When I do this the background ends up being gray.

I tried clearing the off-screen image by going setColor(new Color(0,0,0,0)); fillRect(...);
figuring that a transparent color as the background would allow the JPanel's container to show
thru since the JPanel is opaque.  But that did not work.

I tried specifying a bgcolor in the drawImage method, but it didn't seem to make any difference
there...I'm not really clear on what that is supposed to do actually since reguardless of the
alpha value of the pixels I draw in the off-screen image, the bgcolor argument to drawImage
seems to be ignored.

I'd prefer a solution that made MyPanel's parent container's background show thru instead of
just clearing the off-screen image to that color.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

can you post your code. I would have thought an empty offscreen image would be transparent.
Avatar of ec000wmr
ec000wmr

ASKER

Sure, I've tried various permutations...but here's the
simplest example of code that doesn't produce correct
results:

import javax.swing.*;
import java.awt.*;

public class paneltest
{
    public static void main(String [] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(300,300);
        Container c=frame.getContentPane();
        c.setLayout(null);
        c.setBackground(Color.cyan);

        MyPanel p1 = new MyPanel();
        p1.setSize(50,50);
        p1.setLocation(30,30);

        MyPanel p2 = new MyPanel();
        p2.setSize(50,50);
        p2.setLocation(50,50);

        c.add(p1);
        c.add(p2);

        frame.show();
    }
}

class MyPanel extends JPanel
{
    public MyPanel()
    {
        setOpaque(false);
    }

    public void paintComponent(Graphics g)
    {
        Image i=createImage(50,50);
        Graphics ig = i.getGraphics();

        ig.setColor(Color.blue);
        ig.drawLine(0,25,50,25);

        g.drawImage(i, 0, 0, this);

        super.paintComponent(g);
    }
}
-------------
The following paintComponent produces the visual results
I'm looking for but without the off-screen image:

    public void paintComponent(Graphics g)
    {
        g.setColor(Color.blue);
        g.drawLine(0,25,50,25);

        super.paintComponent(g);
    }
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sweet!
I'm definately curious as to why it doesn't work with regular
images, but BufferedImage works great so I'll use that.

Thanks for your help!
It appears the image returned from createImage has a grey background.

Thanks for the points :)