JFrame loading question

Posted on 2005-04-18
Last Modified: 2008-03-17
I am taking a screenshot and running it through a filter to dim the image. I take this image and place it in a JLabel and show over a portion of the screen. When I do this, even though I use a MediaTracker, it shows a grey background for the JFrame for about .1s before the image is shown. I've trimmed out all the unnecessary code and I'm posting a compileable version below.

If I do not run the image through a filter, it displays instantly as it should. What could be causing this delay?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.RGBImageFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class TestCase extends JPanel {
    /** Creates a new instance of TestCase */
    public TestCase() {    
            //take screenshot of selected area of screen
            Robot screenRobot = new Robot();  //temporary robot for acquiring screenshot
            Point captureOrigin = new Point(10,10);
            Dimension captureDimension = new Dimension(760, 600);
            Image fadedScreenImage = screenRobot.createScreenCapture(new Rectangle(captureOrigin, captureDimension));
            //decrease brightness of image                    
            ImageFilter dimFilter = new DimImageFilter();
            FilteredImageSource dimImageSrc = new FilteredImageSource(fadedScreenImage.getSource(), dimFilter);
            fadedScreenImage = Toolkit.getDefaultToolkit().createImage(dimImageSrc);
            //track loading of image
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(fadedScreenImage, 1);

            //create undecorated JFrame and position it over portion of screen currently being captured
            JFrame fadedFrame = new JFrame();
            fadedFrame.setLocation(captureOrigin.x-1, captureOrigin.y-1);
            //objects test code
            ImageIcon icon = new ImageIcon(fadedScreenImage);
            JLabel imagePanel = new JLabel(icon);
            //create image panel and position in in frame for display
//            ImagePanel imagePanel = new ImagePanel(fadedScreenImage, this);
            JPanel cp = new JPanel();
            cp.setLayout(new BorderLayout());
            cp.setBorder(new LineBorder(Color.GRAY, 1));

            //reset cursor position to be center of capture area
            int xPos = (captureDimension.width/2) + captureOrigin.x;
            int yPos = (captureDimension.height/2) + captureOrigin.y;
            screenRobot.mouseMove(xPos, yPos);            
            //show simulated overlay
        catch(Exception e) {
            //error handling code here - not yet implemented

    public static void main(String[] args) {
        TestCase test = new TestCase();      
    class DimImageFilter extends RGBImageFilter {
    /** Creates a new instance of PauseFilter */
    public DimImageFilter() {
        // work with pixels whose indices are into a color table
        canFilterIndexColorModel = true;
    public int filterRGB(int x, int y, int rgb) {
        // find out the red, green and blue color components
        int r = (rgb >> 16) & 0xff;
        int g = (rgb >> 8)  & 0xff;
        int b = (rgb)       & 0xff;

        //calculate the muted colors
        r = (r * 90)/100;
        g = (g * 90)/100;
        b = (b * 90)/100;

        return (rgb & 0xff000000) | (r<<16) | (g<<8) | (b);


Question by:lomidien
    LVL 14

    Expert Comment

    There might be a slight increase in performance if You'd create Your JPanels double buffered either with constructor
    JPanel(true) or with method

    Author Comment

    I tried that approach and unfortunately got the same results.

    LVL 92

    Expert Comment

    Run it with the following vm option:


    Author Comment

    can i specify that from within an executable jar?
    LVL 92

    Accepted Solution

    unfortunately no

    try adding the following as the first line in your main()

    System.setProperty("sun.awt.noerasebackground", "true");

    Author Comment

    Perfect, thanks objects:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now