Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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.
0
ec000wmr
Asked:
ec000wmr
  • 3
  • 2
1 Solution
 
objectsCommented:
can you post your code. I would have thought an empty offscreen image would be transparent.
0
 
ec000wmrAuthor Commented:
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);
    }
0
 
objectsCommented:
Strange, anyway a workaround is to use a BufferedImage as your offscreen image:

Image i=new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
0
 
ec000wmrAuthor Commented:
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!
0
 
objectsCommented:
It appears the image returned from createImage has a grey background.

Thanks for the points :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now