Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package evocr.components;
- import evocr.components.pictureBox.PictureBoxSizeMode;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import javax.swing.Icon;
- public class JPictureBox extends Component {
- private BufferedImage image = null;
- private Icon errorIcon = null;
- private PictureBoxSizeMode sizeMode = PictureBoxSizeMode.AUTO_SIZE;
- private double zoomFactor = 1;
- public BufferedImage getImage() {
- return image;
- }
- public void setImage(BufferedImage image) {
- BufferedImage old = this.image;
- this.image = image;
- refresh();
- firePropertyChange("Image", old, image);
- }
- public PictureBoxSizeMode getSizeMode() {
- return sizeMode;
- }
- public void setSizeMode(PictureBoxSizeMode sizeMode) {
- PictureBoxSizeMode old = this.sizeMode;
- this.sizeMode = sizeMode;
- refresh();
- firePropertyChange("SizeMode", old, sizeMode);
- }
- public Icon getErrorIcon() {
- return errorIcon;
- }
- public void setErrorIcon(Icon errorIcon) {
- Icon old = this.errorIcon;
- this.errorIcon = errorIcon;
- refresh();
- firePropertyChange("ErrorIcon", old, errorIcon);
- }
- protected void refresh() {
- java.awt.EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- revalidate();
- repaint();
- }
- });
- }
- @Override
- public Dimension getPreferredSize() {
- if (image != null) {
- switch (getSizeMode()) {
- case AUTO_SIZE: {
- return new Dimension(image.getWidth(), image.getHeight());
- }
- case FIT_WIDTH: {
- int w = getParent().getWidth();
- return new Dimension(w, (int) (w / getAR()));
- }
- case FIT_HEIGHT: {
- int h = getParent().getHeight();
- return new Dimension((int) (h * getAR()), h);
- }
- case ZOOM: {
- int w = (int) Math.round(image.getWidth() * zoomFactor);
- int h = (int) Math.round(image.getHeight() * zoomFactor);
- return new Dimension(w, h);
- }
- }
- }
- return super.getPreferredSize();
- }
- public void setZoomFactor(double zoomFactor) {
- double old = this.zoomFactor;
- this.zoomFactor = zoomFactor;
- firePropertyChange("ZoomFactor", old, zoomFactor);
- setSizeMode(PictureBoxSizeMode.ZOOM);
- }
- public double getZoomFactor() {
- return zoomFactor;
- }
- private double getAR() {
- return (double) image.getWidth() / image.getHeight();
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- int x = 0;
- int y = 0;
- int w = 0;
- int h = 0;
- if (image != null) {
- switch (sizeMode) {
- case AUTO_SIZE:
- case NORMAL:
- w = image.getWidth();
- h = image.getHeight();
- break;
- case CENTER_IMAGE:
- w = image.getWidth();
- h = image.getHeight();
- x = (getWidth() - w) / 2;
- y = (getHeight() - h) / 2;
- break;
- case STRETCH_IMAGE:
- w = getWidth();
- h = getHeight();
- break;
- case ZOOM:
- w = (int) Math.round(image.getWidth() * zoomFactor);
- h = (int) Math.round(image.getHeight() * zoomFactor);
- break;
- case FIT_BOTH:
- if (image.getWidth() > image.getHeight()) {
- w = getWidth();
- h = (int) (w / getAR());
- if (h > getHeight()) {
- h = getHeight();
- w = (int) (h * getAR());
- }
- } else {
- h = getHeight();
- w = (int) (h * getAR());
- if (w > getWidth()) {
- w = getWidth();
- h = (int) (w / getAR());
- }
- }
- break;
- case FIT_WIDTH:
- w = getWidth();
- h = (int) (w / getAR());
- break;
- case FIT_HEIGHT:
- h = getHeight();
- w = (int) (h * getAR());
- break;
- }
- Graphics2D g2d = (Graphics2D) g;
- g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
- g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- g2d.drawImage(image, x, y, w, h, null);
- } else if (errorIcon != null) {
- w = errorIcon.getIconWidth();
- h = errorIcon.getIconHeight();
- x = (getWidth() - w) / 2;
- y = (getHeight() - h) / 2;
- errorIcon.paintIcon(this, g, x, y);
- }
- }
- }
- -----------------------------------------------------------------------------------------------------
- package evocr.components.pictureBox;
- public enum PictureBoxSizeMode {
- AUTO_SIZE,
- CENTER_IMAGE,
- NORMAL,
- STRETCH_IMAGE,
- ZOOM,
- FIT_BOTH,
- FIT_WIDTH,
- FIT_HEIGHT
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement