VisClient/org/hfbk/util/UIUtils.java

Go to the documentation of this file.
00001 package org.hfbk.util;
00002 
00003 import java.awt.AWTException;
00004 import java.awt.Button;
00005 import java.awt.Color;
00006 import java.awt.Component;
00007 import java.awt.Container;
00008 import java.awt.Graphics2D;
00009 import java.awt.Rectangle;
00010 import java.awt.RenderingHints;
00011 import java.awt.Robot;
00012 import java.awt.TextField;
00013 import java.awt.Transparency;
00014 import java.awt.image.BufferedImage;
00015 import java.io.File;
00016 import java.io.IOException;
00017 
00018 import javax.imageio.ImageIO;
00019 
00020 import org.hfbk.vis.Prefs;
00021 
00023 public class UIUtils {
00027         public static void blackify(Component c){
00028                 if(System.getProperty("os.name").equals("Mac OS X") && (c instanceof TextField || c instanceof Button))
00029                 {
00030                         c.setForeground(Color.BLACK);
00031                         c.setBackground(Color.WHITE);                   
00032                 }else{
00033                         c.setForeground(Color.WHITE);
00034                         c.setBackground(Color.BLACK);                   
00035                 }
00036                 
00037                 if (c instanceof Container)  //if this contains further elements paint them black as well
00038                         for (Component c2 : ((Container)c).getComponents())
00039                                 blackify(c2);
00040         }
00041         
00064     public static BufferedImage getScaledInstance(BufferedImage img,
00065                                            int targetWidth,
00066                                            int targetHeight,
00067                                            Object hint,
00068                                            boolean higherQuality)
00069     {
00070         int type = (img.getTransparency() == Transparency.OPAQUE) ?
00071             BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
00072         BufferedImage ret = (BufferedImage)img;
00073         int w, h;
00074         if (higherQuality) {
00075             // Use multi-step technique: start with original size, then
00076             // scale down in multiple passes with drawImage()
00077             // until the target size is reached
00078             w = img.getWidth();
00079             h = img.getHeight();
00080         } else {
00081             // Use one-step technique: scale directly from original
00082             // size to target size with a single drawImage() call
00083             w = targetWidth;
00084             h = targetHeight;
00085         }
00086         
00087         do {
00088             if (higherQuality && w > targetWidth) {
00089                 w /= 2;
00090                 if (w < targetWidth) {
00091                     w = targetWidth;
00092                 }
00093             }
00094 
00095             if (higherQuality && h > targetHeight) {
00096                 h /= 2;
00097                 if (h < targetHeight) {
00098                     h = targetHeight;
00099                 }
00100             }
00101 
00102             BufferedImage tmp = new BufferedImage(w, h, type);
00103             Graphics2D g2 = tmp.createGraphics();
00104             g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
00105             g2.drawImage(ret, 0, 0, w, h, null);
00106             g2.dispose();
00107 
00108             ret = tmp;
00109         } while (w != targetWidth || h != targetHeight);
00110 
00111         return ret;
00112     }
00113 
00114         
00118         public static void screenshot(Component c){
00119                 Rectangle screenRect =c.getBounds();
00120                 // create screen shot
00121                 Robot robot = null;
00122                 try {
00123                         robot = new Robot();
00124                 } catch (AWTException e) {
00125                         throw new RuntimeException(e);
00126                 }
00127                 long t=System.currentTimeMillis();
00128                 String outFileName=Prefs.current.screenshotdir+"/shot"+t+".png";
00129                 BufferedImage image = robot.createScreenCapture(screenRect);
00130                 // save captured image to PNG file
00131                 try {
00132                         ImageIO.write(image, "png", new File(outFileName));
00133                 } catch (IOException e) {
00134                         throw new RuntimeException(e);
00135                 }
00136         
00137                 // give feedback
00138                 System.out.println("Saved screen shot (" + image.getWidth() +
00139                         " x " + image.getHeight() + " pixels) to file \"" +
00140                         outFileName + "\".");
00141 
00142                 if (Prefs.current.screenshotupload)
00143                 {
00144                 int w=image.getWidth();int h=image.getHeight();
00145                 if ((w>800) | (h>625))
00146                 {
00147                         if (w>h){
00148                                 h=(int)Math.floor((800f/w)*h);
00149                                 w=800;
00150                                 
00151                         }
00152                         else
00153                         {
00154                                 w=(int)Math.floor((625f/h)*w);
00155                                 h=625;
00156                                 }
00157                         
00158                         
00159                 }
00160                 BufferedImage smallimage=getScaledInstance(image,w,h,RenderingHints.VALUE_INTERPOLATION_BILINEAR,true);
00161 //               save scaled image to JPG file
00162                 String tempFileName=Prefs.current.screenshotdir+"/"+t+".jpg";
00163                 
00164                 
00165                 try {
00166                         ImageIO.write(smallimage, "jpg", new File(tempFileName));
00167                 } catch (IOException e) {
00168                         throw new RuntimeException(e);
00169                 }
00170                 
00171         
00172                 HTTPUtils.SubmitPicture(tempFileName, "");
00173                 }
00174         }
00175 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1