/**
* Calque permettant d'afficher une image (de type BufferedImage)
*
* @author HEIT Sylvestre & Coustillac Laurent
*
*
* Programme kartoMNT : g�n�ration de MNT � partir d'une carte de lignes de niveaux
* Copyright (C) 2002 HEIT Sylvestre & Coustillac Laurent
*
* Ce programme est libre, vous pouvez le redistribuer et/ou le modifier selon les termes de la Licence
*Publique G�n�rale GNU publi�e par la Free Software Foundation (version 2).
*
*Ce programme est distribu� car potentiellement utile, mais SANS AUCUNE GARANTIE, ni explicite ni implicite,
*y compris les garanties de commercialisation ou d'adaptation dans un but sp�cifique. Reportez-vous � la
*Licence Publique G�n�rale GNU pour plus de d�tails.
*
*Vous devez avoir re�u une copie de la Licence Publique G�n�rale GNU en m�me temps que ce programme ; si
*ce n'est pas le cas, �crivez � la
*Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, �tats-Unis.
*/
package kartoMNT;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
import javax.swing.border.*;
public class ImageCanvas extends GraphicObject
{
// object containing image bytes
private BufferedImage image;
private Color backGroundColor;
private boolean autoresize;
private AffineTransformOp aff;
private int interpolationtype;
private Composite composite;
/** no interpolation
*/
public static final int TYPE_NEAREST_NEIGHBOR=AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
/** bilinear interpolation
*/
public static final int TYPE_BILINEAR=AffineTransformOp.TYPE_BILINEAR;
/** build a new window displaying the given image
*
* @param image: image to be displayed
*/
public ImageCanvas(BufferedImage img)
{
super();
composite=AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
//interpolationtype=TYPE_BILINEAR;
interpolationtype=TYPE_NEAREST_NEIGHBOR;
setBackGroundColor(new Color(0.0f,0.0f,0.0f));
set(img);
}
/** build a new window displaying no image
*/
public ImageCanvas()
{
super();
composite=AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
interpolationtype=TYPE_BILINEAR;
setBackGroundColor(new Color(0.0f,0.0f,0.0f));
image=null;
setPreferredSize(new Dimension(320,200));
aff=new AffineTransformOp(new AffineTransform(referential.scalex,0,0,referential.scaley,0,0) , interpolationtype);
//setBorder(new EmptyBorder(0,0,0,0));// enlever le border
}
/** set the image to display
*/
public void set(BufferedImage imgd)
{
image = imgd;
updateDimension();
aff=new AffineTransformOp( new AffineTransform(referential.scalex,0,0,referential.scaley,0,0) , interpolationtype);
revalidate();
repaint();
}
/**
* get the current displayed image
*/
public BufferedImage getImage()
{
return image;
}
public BufferedImage get()
{
return image;
}
/** get RGB color at point of coordinates x,y
* the coordinate are relative to the upper left corner of the panel
* they are given in panel coordinates (they don't take in account the zoom applied)
*/
public int getRGB(int x,int y)
{
int px,py;
px=(int)canvas2ObjX(x);
py=(int)canvas2ObjY(y);
if( (px>=image.getWidth()) || (py>=image.getHeight()) || (px<0) || (py<0) )
{
return 0;
}
return image.getRGB(px,py);
}
/** get RGB color at point of coordinates x,y
* the coordinate are relative to the upper left corner of the panel
* they are given in panel coordinates (they don't take in account the zoom)
*/
public Color getColor(int x,int y)
{
int px,py;
px=(int)canvas2ObjX(x);
py=(int)canvas2ObjY(y);
if( (px>=image.getWidth()) || (py>=image.getHeight()) || (px<0) || (py<0) )
{
return null;
}
return new Color(image.getRGB(px,py));
}
/** if the position or scale has changed this fonction
* must be called to show the changes
*/
public void updateReferential()
{
if(image!=null)
{
updateDimension();
}
aff=new AffineTransformOp( new AffineTransform(referential.scalex,0,0,referential.scaley,posx,posy) , interpolationtype);
super.updateReferential();
revalidate();
repaint();
}
/** set the transparency of the image
* @param a: tranparency (between 0.0 and 1.0)
*/
public void setAlpha(double a)
{
alpha=a;
composite=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,(float)alpha);
repaint();
}
/** paint the image
*/
public void paint(Graphics gr)
{
super.paint(gr);
Graphics2D gr2d=(Graphics2D) gr;
if(composite!=null)
gr2d.setComposite(composite);
/*setBackground(backGroundColor);
gr.clearRect(0,0,getWidth(),getHeight());*/
if(image!=null)
{
gr2d.drawImage((BufferedImage)image, aff, 0,0) ;
}
}
/** update the image
*/
/*public void update(Graphics g)
{
paint(g);
}*/
public void setBackGroundColor(Color c)
{
backGroundColor=c;
repaint();
}
public void setInterpolationType(int t)
{
interpolationtype=t;
aff=new AffineTransformOp( new AffineTransform(referential.scalex,0,0,referential.scaley,0,0) , interpolationtype);
repaint();
}
public int getInterpolationType()
{
return interpolationtype;
}
//retourne dimensions de l'image en coordonees ecran (tient compte du zoom)
public Dimension getImageDimension()
{
if(image!=null)
return new Dimension((int)(referential.scalex*image.getWidth(this)),(int)(referential.scaley*image.getHeight(this)));
else
return new Dimension(0,0);
}
protected void updateDimension()
{
Dimension d=getImageDimension();
setPreferredSize(d);
setSize(d);
}
}