off-screen images with transparent backgrounds problem

Posted on 2003-03-15
Medium Priority
Last Modified: 2006-11-17
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

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.
Question by:ec000wmr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 92

Expert Comment

ID: 8145698
can you post your code. I would have thought an empty offscreen image would be transparent.

Author Comment

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

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

public class paneltest
    public static void main(String [] args)
        JFrame frame = new JFrame();
        Container c=frame.getContentPane();

        MyPanel p1 = new MyPanel();

        MyPanel p2 = new MyPanel();



class MyPanel extends JPanel
    public MyPanel()

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


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

The following paintComponent produces the visual results
I'm looking for but without the off-screen image:

    public void paintComponent(Graphics g)

LVL 92

Accepted Solution

objects earned 300 total points
ID: 8145848
Strange, anyway a workaround is to use a BufferedImage as your offscreen image:

Image i=new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);

Author Comment

ID: 8145861
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!
LVL 92

Expert Comment

ID: 8145870
It appears the image returned from createImage has a grey background.

Thanks for the points :)

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question