|
Urbi SDK Remote for Java
2.7.5
|
00001 /* 00002 * Copyright (C) 2010-2011, Gostai S.A.S. 00003 * 00004 * This software is provided "as is" without warranty of any kind, 00005 * either expressed or implied, including but not limited to the 00006 * implied warranties of fitness for a particular purpose. 00007 * 00008 * See the LICENSE file for more information. 00009 */ 00010 00011 package urbi; 00012 00013 import java.awt.*; 00014 import java.awt.event.*; 00015 import java.awt.image.BufferedImage; 00016 00017 /** 00018 * The ImageUtilities class is full of handy static methods. 00019 * <p> 00020 * <p> 00021 * @author Bastien Saltel 00022 */ 00023 00024 public class ImageUtilities 00025 { 00026 /** The component. */ 00027 private static final Component component = new Component() {}; 00028 00029 /** The mediatracker.associated with the component */ 00030 private static final MediaTracker mediatracker = new MediaTracker(component); 00031 00032 /** The id of the image. */ 00033 private static int id = 0; 00034 00035 /** The width of the image. */ 00036 private static int width = 0; 00037 00038 /** The height of the image. */ 00039 private static int height = 0; 00040 00041 /** 00042 * Sets the width of the image. 00043 * <p> 00044 */ 00045 public static void setWidth(int i) 00046 { 00047 width = i; 00048 } 00049 00050 /** 00051 * Sets the height of the image. 00052 * <p> 00053 */ 00054 public static void setHeight(int i) 00055 { 00056 height = i; 00057 } 00058 00059 /** 00060 * Returns the width of the image. 00061 * <p> 00062 */ 00063 public static int getWidth() 00064 { 00065 return width; 00066 } 00067 00068 /** 00069 * Returns the height of the image. 00070 * <p> 00071 */ 00072 public static int getHeight() 00073 { 00074 return height; 00075 } 00076 00077 /** 00078 * Waits for the given image to load fully. Returns true if 00079 * everything goes well or false if there is an error while loading 00080 * the image. 00081 * <p> 00082 * @return true if everything goes well or false if there is an error 00083 * during loading the image. 00084 */ 00085 public static boolean waitForImage(Image image) 00086 { 00087 int i; 00088 00089 synchronized (component) 00090 { 00091 i = id++; 00092 } 00093 mediatracker.addImage(image, i, width, height); 00094 try 00095 { 00096 mediatracker.waitForID(i); 00097 } 00098 catch (InterruptedException ie) 00099 { 00100 return false; 00101 } 00102 if (mediatracker.isErrorID(i)) 00103 return false; 00104 return true; 00105 } 00106 00107 /** 00108 * Loads an image from the given binary buffer and will not return until 00109 * the image is fully loaded. 00110 * <p> 00111 * @param buffer The buffer containing the binary data of the image. 00112 * @return The fully loaded image if everything goes well or null if there 00113 * is an error while loading. 00114 */ 00115 public static Image blockingLoad(byte[] buffer) 00116 { 00117 Image image = Toolkit.getDefaultToolkit().createImage(buffer); 00118 if (waitForImage(image) == false) 00119 return null; 00120 return image; 00121 } 00122 00123 /** 00124 * Creates a buffered image from the supplied image. 00125 * <p> 00126 * @param image The supplied image. 00127 * @return The buffered image. 00128 */ 00129 public static BufferedImage makeBufferedImage(Image image) 00130 { 00131 if (image == null) 00132 System.out.println("ImageUtilities l.67 Image null"); 00133 return makeBufferedImage(image, BufferedImage.TYPE_INT_RGB); 00134 } 00135 00136 /** 00137 * Creates a buffered image from the supplied image and its type. 00138 * <p> 00139 * @param image The supplied image. 00140 * @param imageType The type of the image. 00141 * @return The buffered image. 00142 */ 00143 public static BufferedImage makeBufferedImage(Image image, int imageType) 00144 { 00145 if (waitForImage(image) == false) 00146 return null; 00147 if (image == null) 00148 System.out.println("ImageUtilities l.77 Image null"); 00149 BufferedImage bufferedImage = new BufferedImage(width, height, imageType); 00150 00151 Graphics2D g = bufferedImage.createGraphics(); 00152 if (image == null) 00153 System.out.println("ImageUtilities l.80 Image null"); 00154 g.drawImage(image, null, null); 00155 return bufferedImage; 00156 } 00157 00158 /** 00159 * Returns a frame subclass whose the update mathod does not clear the frame's 00160 * drawing surface. 00161 * <p> 00162 * @param name The frame's title. 00163 * @param c The component whose is used to fit the frame's size. 00164 * @return The generated frame. 00165 */ 00166 public static Frame getNonClearingFrame(String name, Component c) 00167 { 00168 final Frame f = new Frame(name) 00169 { 00170 public void update(Graphics g) 00171 { 00172 paint(g); 00173 } 00174 }; 00175 sizeContainerToComponent(f, c); 00176 centerFrame(f); 00177 f.setLayout(new BorderLayout()); 00178 f.add(c, BorderLayout.CENTER); 00179 f.addWindowListener(new WindowAdapter() 00180 { 00181 public void windowClosing(WindowEvent e) 00182 { 00183 f.dispose(); 00184 } 00185 }); 00186 return f; 00187 } 00188 00189 /** 00190 * Resizes the given container to enclose the preferred size of the given component. 00191 * <p> 00192 * @param container The container to resize. 00193 * @param component The component. 00194 */ 00195 public static void sizeContainerToComponent(Container container, Component component) 00196 { 00197 if (container.isDisplayable() == false) 00198 container.addNotify(); 00199 Insets insets = container.getInsets(); 00200 Dimension size = component.getPreferredSize(); 00201 int w = insets.left + insets.right + size.width; 00202 int h = insets.top + insets.bottom + size.height; 00203 container.setSize(w, h); 00204 } 00205 00206 /** 00207 * Places the given frame in the center of the screen. 00208 */ 00209 public static void centerFrame(Frame f) 00210 { 00211 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 00212 Dimension d = f.getSize(); 00213 int x = (screen.width - d.width) / 2; 00214 int y = (screen.height - d.height) / 2; 00215 f.setLocation(x, y); 00216 } 00217 }