Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

Frame/JFrame screen capture from Java

Thread view: 
hiwa - 08 Dec 2003 09:13 GMT
Java GUI components can be dumped to standard image file using code
exampled below. However, the outermost frame, including title header,
of Frame or JFrame can't be captured through this method because they
aren't rendered by Java but the native windowing system. I'd like to
know a workaround, if any.
<code>
 public static void writeComponentToFile
  (Component comp, String fileName){
                   //output file
   String[] temp = fileName.split("\\.");
   String ext = temp[temp.length - 1];
   
   BufferedImage bImage
    = new BufferedImage(comp.getWidth(), comp.getHeight(),
                         BufferedImage.TYPE_INT_RGB);
   
   Graphics g = bImage.getGraphics();
   comp.paint(g);
   
   try{
     File file = new File(fileName);
     if (ext.equals("png")){
       ImageIO.write(rImage, "png", file);
     }  
     else if (ext.equals("jpg")){
       ImageIO.write(rImage, "jpg", file);
     }
     else{
       System.err.println("Unsupported format: " + ext);
       System.err.println("We only support .jpg and .png files);
     }
   }
   catch(Exception e){
     e.printStackTrace();
   }
 }
</code
Harald Hein - 08 Dec 2003 10:03 GMT
> Java GUI components can be dumped to standard image file using code
> exampled below. However, the outermost frame, including title header,
> of Frame or JFrame can't be captured through this method because they
> aren't rendered by Java but the native windowing system. I'd like to
> know a workaround, if any.

Maybe java.awt.Robot
hiwa - 09 Dec 2003 01:50 GMT
> > Java GUI components can be dumped to standard image file using code
> > exampled below. However, the outermost frame, including title header,
[quoted text clipped - 3 lines]
>
> Maybe java.awt.Robot
Thanks Harald. It works fine. But couple of caveats here:

<code>
 RenderedImage getCompImageFromRobot(Component comp){
   int x, y, width, height;
   BufferedImage bImage = null;
   
   //these conditionals are necessary, but I don't know why
   if (comp instanceof Window){
     x = comp.getX();
     y = comp.getY();
   }
   else{
     Point p = comp.getLocationOnScreen();
     x = p.x;
     y = p.y;
   }
   width = comp.getWidth();
   height = comp.getHeight();
 
   Rectangle rec = new Rectangle(x, y, width, height);

   try{
     Robot robo = new Robot();
      //you need to give generous amount of delay
     robo.delay(100);
     bImage = robo.createScreenCapture(rec);
   }
   catch(Exception e){
     e.printStackTrace();
   }

   return bImage;
 }
</code


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.