Urbi SDK Remote for Java  2.7.5
ImageComponent.java
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 import java.awt.geom.Line2D;
00017 
00018 import javax.swing.*;
00019 
00020 import urbi.UClient;
00021 
00022 /**
00023  * The ImageComponent class is used to display an image. It subclasses
00024  * JPanel to take advantage of Swing's double buffering.
00025  * <p>
00026  * <p>
00027  * @author Bastien Saltel
00028  */
00029 
00030 public class    ImageComponent extends JPanel
00031 {
00032     /** The image to display. */
00033         private BufferedImage   image = null;
00034 
00035     /** The width of the image. */
00036         private int width = 0;
00037 
00038     /** The height of the image. */
00039         private int height = 0;
00040 
00041         private double          x = 0;
00042 
00043         private double          y = 0;
00044 
00045         /**
00046      * Constructor for ImageComponent. Calls the init method to set
00047          * the background in white color.
00048      * <p>
00049          */
00050         public ImageComponent()
00051         {
00052                 init();
00053         }
00054 
00055         /**
00056          * Sets the width of the image.
00057      * <p>
00058          */
00059         public void             setWidth(int i)
00060         {
00061                 width = i;
00062         }
00063 
00064         /**
00065          * Sets the height of the image.
00066      * <p>
00067          */
00068         public void             setHeight(int i)
00069         {
00070                 height = i;
00071         }
00072 
00073         /**
00074          * Returns the width of the image.
00075      * <p>
00076          */
00077         public int              getWidth()
00078         {
00079                 return width;
00080         }
00081 
00082         /**
00083          * Returns the height of the image.
00084      * <p>
00085          */
00086         public int              getHeight()
00087         {
00088                 return height;
00089         }
00090 
00091         public void             setImage(byte[] buffer)
00092         {
00093                 ImageUtilities.setWidth(width);
00094                 ImageUtilities.setHeight(height);
00095                 Image im = ImageUtilities.blockingLoad(buffer);
00096                 if (im == null)
00097                         image = null;
00098                 else
00099                         {
00100                                 image = ImageUtilities.makeBufferedImage(im);
00101                                 if (getGraphics() != null)
00102                                         repaint();
00103                                 im = null;
00104                         }
00105         }
00106 
00107         /**
00108          * Sets the image by calling the blockingLoad and makeBufferedImage
00109          * methods of the ImageUtilities class and displays it.
00110      * <p>
00111          * @param buffer The buffer containing the binary data of the image.
00112          * @see ImageUtilities
00113          */
00114         public void             setImage(byte[] buffer, double x, double y)
00115         {
00116                 ImageUtilities.setWidth(width);
00117                 ImageUtilities.setHeight(height);
00118                 Image im = ImageUtilities.blockingLoad(buffer);
00119                 this.x = x;
00120                 this.y = y;
00121                 if (im == null)
00122                         image = null;
00123                 else
00124                         {
00125                                 image = ImageUtilities.makeBufferedImage(im);
00126                                 if (getGraphics() != null)
00127                                         repaint();
00128                                 im = null;
00129                         }
00130         }
00131 
00132         /**
00133          * Sets the image and displays it.
00134      * <p>
00135          * @param im The buffered image.
00136          * @see ImageUtilities
00137          */
00138         public void             setImage(BufferedImage im)
00139         {
00140                 image = im;
00141                 if (getGraphics() != null)
00142                         repaint();
00143         }
00144 
00145         public void             setImage(BufferedImage im, double x, double y)
00146         {
00147                 image = im;
00148                 this.x = x;
00149                 this.y = y;
00150                 if (getGraphics() != null)
00151                         repaint();
00152         }
00153 
00154         /**
00155          * Sets the background in white color.
00156      * <p>
00157          */
00158         private void    init()
00159         {
00160                 setBackground(Color.white);
00161         }
00162 
00163         /**
00164          * Returns the displayed image.
00165      * <p>
00166          */
00167         public BufferedImage    getImage()
00168         {
00169                 return image;
00170         }
00171 
00172         public void             update(Graphics graphics)
00173         {
00174                 if (graphics != null)
00175                         {
00176                                 graphics.setColor(getBackground());
00177                                 graphics.fillRect(0, 0, width, height);
00178                         }
00179         }
00180 
00181         /**
00182          * Displays the image.
00183      * <p>
00184          */
00185         public void             paint(Graphics graphics)
00186         {
00187                 if (getImage() != null && graphics != null)
00188                         {
00189                                 graphics.drawImage(getImage(), 0, 0, null);
00190                                 graphics.setColor(Color.red);
00191                                 if (y != 0)
00192                                         graphics.drawLine(0, (int)y, width, (int)y);
00193                                 if (x != 0)
00194                                         graphics.drawLine((int)x, 0, (int)x, height);
00195                         }
00196         }
00197 
00198         /**
00199          * Returns the largest image size.
00200      * <p>
00201          * @return  The largest image size.
00202          */
00203         public Dimension                getPreferredSize()
00204         {
00205                 return new Dimension(width, height);
00206         }
00207 
00208 }