Package components.imagecollector.virtual

Source Code of components.imagecollector.virtual.VirtualImageCollector

package components.imagecollector.virtual;

import java.awt.*;
import java.awt.image.*;

import org.eclipse.swt.widgets.Display;

import utils.ErrorMessage;
import utils.Semaphore;
import utils.defines.Defines;
import utils.logging.Logger;
import components.imagecollector.ImageCollector;


/**
* La clase <code>VirtualImageCollector</code> representa un recolector de
* im�genes virtual, utilizado en simulaciones virtuales.
*/
public class VirtualImageCollector extends ImageCollector
{
 
  private boolean              closedDevice;
  private Dimension            imageResolution;
  private String              imageFileName;
  private Semaphore            collectorSemaphore;
 
  private Image              originalImage;
  private BufferedImage          sceneImage;
  private Graphics2D            gc;
 
  private double[]            pos;
  private Color              color;

 
  public VirtualImageCollector()
  {
    closedDevice    = false;
    originalImage    = null;
    imageResolution    = new Dimension( -1 , -1 );
    collectorSemaphore  = new Semaphore( 1 );
    pos          = new double[2];
    color        = null;
  }

 
  public void finalize() throws Throwable
  { }
 
 
  public synchronized void setImageFileName(String fileName)
  {
    if ( fileName != null )
    {
      imageFileName  = new String( fileName );
     
      originalImage = Toolkit.getDefaultToolkit().createImage( fileName );
     
      originalImage.getWidth(null);
      originalImage.getHeight(null);
     
      // Se duerme un intervalo, para darle tiempo a los m�todos
      // asincr�nicos getWidth() y getHeight() a que terminen de
      // cargar las dimensiones.
      try
      {
        Thread.sleep( 1000 );
      }
      catch (InterruptedException e)
      {}
     
      if ( originalImage != null )
      {
        imageResolution.width   = originalImage.getWidth(null);
        imageResolution.height  = originalImage.getHeight(null);
       
        if ( imageResolution.width == -||  imageResolution.height == -1 )
        {
          ErrorMessage.errorMessage(Defines.ERROR_OPENNING_IMAGE_FILE,Display.getCurrent().getActiveShell());
          Logger.error( "No se pudo Abrir la imagen indicada" );
          return;
         
        }
       
        sceneImage = new BufferedImageimageResolution.width     ,
                        imageResolution.height    ,
                        BufferedImage.TYPE_INT_RGB  );
   
        gc = sceneImage.createGraphics();
      }
      else
      {
        // TODO: llamar al logger.
        Logger.error( "La imagen original es nula." );
      }
     
    }
  }


  public String getImageFileName()
  {
    return imageFileName;
  }
 
 
  public BufferedImage grabImage()
  {
    if ( closedDevice  ||  originalImage == null  ||  sceneImage == null )
      return null;
   
    // Se dibuja el estado (posici�n) actual de los robots,
    // sobre la imagen original, y se la retorna.
   
    // 1) Se borra todo lo anterior.
    gc.drawImage(originalImage, 0, 0, null);

 
    // 2) Se dibujan los robots en sus posiciones actuales
   
    collectorSemaphore.WAIT();

    if ( color != null )
    {
      gc.setPaint( color );
      gc.fillOval((int)(pos[0] - Defines.ROBOT_RADIO) ,
            (int)(pos[1] - Defines.ROBOT_RADIO) ,
            (int)(2*Defines.ROBOT_RADIO)    ,
            (int)(2*Defines.ROBOT_RADIO)     );
    }

    collectorSemaphore.SIGNAL();

   
    return sceneImage;
  }

 
  public synchronized void close()
  {
    closedDevice = true;
  }

 
  public synchronized boolean isClosed()
  {  return closedDevice; }


  public synchronized void setImageResolution(Dimension imageResolution)
  {
    this.imageResolution.width  = imageResolution.width;
    this.imageResolution.height  = imageResolution.height;
  }


  public synchronized Dimension getImageResolution()
  {
    return (Dimension)imageResolution.clone();
  }
 
 
  public void drawRobot(Color robotColor, double[] robotPos)
  {
    collectorSemaphore.WAIT();
   
    pos[0] = robotPos[0];
    pos[1] = robotPos[1];
   
    if ( color == null  ||  !color.equals(robotColor) )
      color = new Color( robotColor.getRGB() );
   
    collectorSemaphore.SIGNAL();
  }


  public String getXMLConfig(String ident)
  {
    // TODO Auto-generated method stub
    return null;
  }
 
}
TOP

Related Classes of components.imagecollector.virtual.VirtualImageCollector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.