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.FilteredIma geSource;
import java.awt.image.ImageFilter ;
import java.awt.image.RGBImageFil ter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBor der;
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.createScreenCa pture(new Rectangle(captureOrigin, captureDimension));
//decrease brightness of image
ImageFilter dimFilter = new DimImageFilter();
FilteredImageSource dimImageSrc = new FilteredImageSource(fadedS creenImage .getSource (), dimFilter);
fadedScreenImage = Toolkit.getDefaultToolkit( ).createIm age(dimIma geSrc);
//track loading of image
MediaTracker mt = new MediaTracker(this);
mt.addImage(fadedScreenIma ge, 1);
//create undecorated JFrame and position it over portion of screen currently being captured
JFrame fadedFrame = new JFrame();
fadedFrame.setSize(capture Dimension) ;
fadedFrame.setUndecorated( true);
fadedFrame.setLocation(cap tureOrigin .x-1, captureOrigin.y-1);
//objects test code
mt.waitForAll();
System.out.println(mt.chec kID(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(fadedScreenImag e, 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
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.FilteredIma
import java.awt.image.ImageFilter
import java.awt.image.RGBImageFil
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBor
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.createScreenCa
//decrease brightness of image
ImageFilter dimFilter = new DimImageFilter();
FilteredImageSource dimImageSrc = new FilteredImageSource(fadedS
fadedScreenImage = Toolkit.getDefaultToolkit(
//track loading of image
MediaTracker mt = new MediaTracker(this);
mt.addImage(fadedScreenIma
//create undecorated JFrame and position it over portion of screen currently being captured
JFrame fadedFrame = new JFrame();
fadedFrame.setSize(capture
fadedFrame.setUndecorated(
fadedFrame.setLocation(cap
//objects test code
mt.waitForAll();
System.out.println(mt.chec
ImageIcon icon = new ImageIcon(fadedScreenImage
JLabel imagePanel = new JLabel(icon);
//create image panel and position in in frame for display
// ImagePanel imagePanel = new ImagePanel(fadedScreenImag
JPanel cp = new JPanel();
cp.setLayout(new BorderLayout());
cp.setBorder(new LineBorder(Color.GRAY, 1));
fadedFrame.setContentPane(
cp.add(imagePanel);
//reset cursor position to be center of capture area
int xPos = (captureDimension.width/2)
int yPos = (captureDimension.height/2
screenRobot.mouseMove(xPos
//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
ASKER
I tried that approach and unfortunately got the same results.
Thanks,
David
Thanks,
David
Run it with the following vm option:
-Dsun.awt.noerasebackgroun d=true
-Dsun.awt.noerasebackgroun
ASKER
can i specify that from within an executable jar?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect, thanks objects:
System.setProperty("sun.aw t.noeraseb ackground" ,"true");
System.setProperty("sun.aw
JPanel(true) or with method
.setDoubleBuffered(true)