Добавил:
Eatmore
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:написанные программы / информационная система umbrella - сырцы( java + .net ) / Umbrella / src / ImagePanel
.java//
// Created by Grishul Eugeny
//
// Copyright © Grishul Eugeny 2008
//
import java.awt.Graphics;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private Image _image;
private Image _scaledImage;
private float _cachedControlWidth = 0;
private float _cachedControlHeight = 0;
private byte[] _imageBuffer = new byte[0];
private boolean _imageChanged = false;
public ImagePanel() {
super();
}
public void setBackgroundImage( byte[] image ) throws IOException {
if( image == _imageBuffer )
return;
if( image != null && image.length > 0 ) {
_image = ImageIO.read( new ByteArrayInputStream( image ) );
_imageBuffer = image;
} else {
_image = null;
_scaledImage = null;
}
_imageChanged = true;
updateUI();
}
public byte[] getBackgroundImage() {
return _imageBuffer;
}
//public void scaleImage() {
// setScaledImage();
//}
@Override
public void paintComponent( Graphics g ) {
float controlWidth = this.getWidth();
float controlHeight = this.getHeight();
super.paintComponent( g );
if( _cachedControlWidth != controlWidth || _cachedControlHeight != controlHeight || _imageChanged )
if( _image != null ) {
float scaledImageWidth = _image.getWidth( this );
float scaledImageHeight = _image.getHeight( this );
_cachedControlWidth = controlWidth;
_cachedControlHeight = controlHeight;
_imageChanged = false;
if( controlWidth < scaledImageWidth || controlHeight < scaledImageHeight ) {
if( ( controlWidth / controlHeight ) > ( scaledImageWidth / scaledImageHeight ) ) {
scaledImageWidth = -1;
scaledImageHeight = controlHeight;
} else {
scaledImageWidth = controlWidth;
scaledImageHeight = -1;
}
if( scaledImageWidth == 0 )
scaledImageWidth = -1;
if( scaledImageHeight == 0 )
scaledImageHeight = -1;
_scaledImage = _image.getScaledInstance( new Float( scaledImageWidth ).intValue(), new Float( scaledImageHeight ).intValue(), Image.SCALE_DEFAULT );
} else
_scaledImage = _image;
}
if( _scaledImage != null )
g.drawImage( _scaledImage, 0, 0, this );
}
}
Соседние файлы в папке src