public class PicViewer extends javax.swing.JPanel
{
/**
* Creates new form PicViewer
*/
private Image imageOrg;
private Image imageShown;
private final int maxSize = 32000;
private final int minSize = 1;
private final float zoomMax = 5;
private final float zoomMin = 0.1F;
public synchronized void OpenFromImage(Image image)
{
this.imageOrg = image;
this.imageShown = this.imageOrg;
this.refreshImage();
this.paintImmediately(new Rectangle(0, 0, this.getWidth(), this.getHeight()));
this.pictureChangedEvent();
}
protected void setSizeLimits()
{
if(this.getScaleImage() || (!this.getScaleImage() && this.imageShown == null))
{
this.setMinimumSize(new Dimension(this.minSize, this.minSize));
this.setMaximumSize(new Dimension(this.maxSize, this.maxSize));
}
else if(!this.getScaleImage() && this.imageShown != null)
{
int width = this.imageShown.getWidth(this);
int height = this.imageShown.getHeight(this);
this.setMinimumSize(new Dimension(width, height));
this.setMaximumSize(new Dimension(width, height));
}
}
/**
* Resizes and scales JPanel
*/
public void resize(int x, int y, int width, int height)
{
this.setSizeLimits();
this.setPreferredSize(new Dimension(width, height));
this.setBounds(x, y, width, height);
//It doeasn't work!!!
this.paintImmediately(x, y, width, height);
}
/**
* Scales image using zoom ratio
*/
public void refreshImage()
{
this.refreshImage(0, 0);
}
public void refreshImage(int posX, int posY)
{
if(this.imageOrg != null)
{
float zoomLevel = this.getZoom();
if(!this.getScaleImage())
{
this.imageShown = this.imageOrg.getScaledInstance((int)(this.imageOrg.getWidth(this)*zoomLevel), (int)(this.imageOrg.getHeight(this)*zoomLevel), 0);
}
else if(this.getScaleImage())
{
Component parent = this.getParent();
int parW = parent.getWidth();
int parH = parent.getHeight();
Dimension newDim = this.getScaledDimension(parW, parH);
if(newDim.width > 0 && newDim.height > 0)
this.imageShown = this.imageOrg.getScaledInstance(newDim.width, newDim.height, 0);
}
this.resize(posX, posY, this.imageShown.getWidth(this), this.imageShown.getHeight(this));
}
}
protected Dimension getScaledDimension(int reqWidth, int reqHeight)
{
int newWidth = 0;
int newHeight = 0;
float zoomLevel = this.getZoom();
float localZoomLevel = 0;
if(this.imageOrg != null)
{
int imgWidth = this.imageOrg.getWidth(this);
int imgHeight = this.imageOrg.getHeight(this);
if(reqWidth > reqHeight)
{
localZoomLevel = (float)reqWidth / (float)imgWidth;
newWidth = reqWidth;
newHeight = (int)(imgHeight * localZoomLevel);
}
else
{
localZoomLevel = (float)reqHeight/(float)imgHeight;
newHeight = reqHeight;
newWidth = (int)(imgWidth * localZoomLevel);
}
}
return new Dimension((int)(newWidth * zoomLevel), (int)(newHeight * zoomLevel));
}
public void refreshVisual()
{
this.revalidate();
this.repaint();
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Dimension dim = this.getPreferredSize();
g.drawImage(this.imageShown, 0, 0, dim.width, dim.height, null);
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Dimension dim = this.getPreferredSize();
g.drawImage(this.imageShown, 0, 0, dim.width, dim.height, null);
}
@Override
public void update(Graphics g)
{
super.update(g);
this.paint(g);
}
}