JFrame loading question

Posted on 2005-04-18
Medium Priority
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
  • 3
  • 2
LVL 14

Expert Comment

ID: 13810753
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

ID: 13810823
I tried that approach and unfortunately got the same results.

LVL 92

Expert Comment

ID: 13812194
Run it with the following vm option:

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

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

Accepted Solution

objects earned 2000 total points
ID: 13813190
unfortunately no

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

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

Author Comment

ID: 13813208
Perfect, thanks objects:


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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