Link to home
Start Free TrialLog in
Avatar of lomidien
lomidien

asked on

JFrame loading question

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() {    
        try{
            //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.setSize(captureDimension);
            fadedFrame.setUndecorated(true);
            fadedFrame.setLocation(captureOrigin.x-1, captureOrigin.y-1);
           
            //objects test code
            mt.waitForAll();
            System.out.println(mt.checkID(1));
            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));
            fadedFrame.setContentPane(cp);
            cp.add(imagePanel);

            //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
            fadedFrame.setVisible(true);
        }
        catch(Exception e) {
            //error handling code here - not yet implemented
            e.printStackTrace();
        }
       
    }

    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);

    }
   
}
   
}

Thanks,
David
Avatar of StillUnAware
StillUnAware
Flag of Lithuania image

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

ASKER

I tried that approach and unfortunately got the same results.

Thanks,
David
Avatar of Mick Barry
Run it with the following vm option:

-Dsun.awt.noerasebackground=true
can i specify that from within an executable jar?
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
Perfect, thanks objects:

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