Advertisement
martinleung93

JPictureBox.java

Nov 30th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. package evocr.components;
  2.  
  3. import evocr.components.pictureBox.PictureBoxSizeMode;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Graphics2D;
  8. import java.awt.Image;
  9. import java.awt.RenderingHints;
  10. import java.awt.image.BufferedImage;
  11. import javax.swing.Icon;
  12.  
  13. public class JPictureBox extends Component {
  14.     private BufferedImage image = null;
  15.     private Icon errorIcon = null;
  16.     private PictureBoxSizeMode sizeMode = PictureBoxSizeMode.AUTO_SIZE;
  17.     private double zoomFactor = 1;
  18.  
  19.     public BufferedImage getImage() {
  20.         return image;
  21.     }
  22.  
  23.     public void setImage(BufferedImage image) {
  24.         BufferedImage old = this.image;
  25.         this.image = image;
  26.         refresh();
  27.  
  28.         firePropertyChange("Image", old, image);
  29.     }
  30.  
  31.     public PictureBoxSizeMode getSizeMode() {
  32.         return sizeMode;
  33.     }
  34.  
  35.     public void setSizeMode(PictureBoxSizeMode sizeMode) {
  36.         PictureBoxSizeMode old = this.sizeMode;
  37.         this.sizeMode = sizeMode;
  38.         refresh();
  39.  
  40.         firePropertyChange("SizeMode", old, sizeMode);
  41.     }
  42.  
  43.     public Icon getErrorIcon() {
  44.         return errorIcon;
  45.     }
  46.  
  47.     public void setErrorIcon(Icon errorIcon) {
  48.         Icon old = this.errorIcon;
  49.         this.errorIcon = errorIcon;
  50.         refresh();
  51.  
  52.         firePropertyChange("ErrorIcon", old, errorIcon);
  53.     }
  54.  
  55.     protected void refresh() {
  56.         java.awt.EventQueue.invokeLater(new Runnable() {
  57.             @Override
  58.             public void run() {
  59.                 revalidate();
  60.                 repaint();
  61.             }
  62.         });
  63.     }
  64.  
  65.     @Override
  66.     public Dimension getPreferredSize() {
  67.         if (image != null) {
  68.             switch (getSizeMode()) {
  69.                 case AUTO_SIZE: {
  70.                     return new Dimension(image.getWidth(), image.getHeight());
  71.                 }
  72.                 case FIT_WIDTH: {
  73.                     int w = getParent().getWidth();
  74.                     return new Dimension(w, (int) (w / getAR()));
  75.                 }
  76.                 case FIT_HEIGHT: {
  77.                     int h = getParent().getHeight();
  78.                     return new Dimension((int) (h * getAR()), h);
  79.                 }
  80.                 case ZOOM: {
  81.                     int w = (int) Math.round(image.getWidth() * zoomFactor);
  82.                     int h = (int) Math.round(image.getHeight() * zoomFactor);
  83.                     return new Dimension(w, h);
  84.                 }
  85.             }
  86.         }
  87.         return super.getPreferredSize();
  88.     }
  89.  
  90.     public void setZoomFactor(double zoomFactor) {
  91.         double old = this.zoomFactor;
  92.         this.zoomFactor = zoomFactor;
  93.  
  94.         firePropertyChange("ZoomFactor", old, zoomFactor);
  95.         setSizeMode(PictureBoxSizeMode.ZOOM);
  96.     }
  97.  
  98.     public double getZoomFactor() {
  99.         return zoomFactor;
  100.     }
  101.    
  102.     private double getAR() {
  103.         return (double) image.getWidth() / image.getHeight();
  104.     }
  105.  
  106.     @Override
  107.     public void paint(Graphics g) {
  108.         super.paint(g);
  109.  
  110.         int x = 0;
  111.         int y = 0;
  112.         int w = 0;
  113.         int h = 0;
  114.         if (image != null) {
  115.             switch (sizeMode) {
  116.                 case AUTO_SIZE:
  117.                 case NORMAL:
  118.                     w = image.getWidth();
  119.                     h = image.getHeight();
  120.                     break;
  121.                 case CENTER_IMAGE:
  122.                     w = image.getWidth();
  123.                     h = image.getHeight();
  124.                     x = (getWidth() - w) / 2;
  125.                     y = (getHeight() - h) / 2;
  126.                     break;
  127.                 case STRETCH_IMAGE:
  128.                     w = getWidth();
  129.                     h = getHeight();
  130.                     break;
  131.                 case ZOOM:
  132.                     w = (int) Math.round(image.getWidth() * zoomFactor);
  133.                     h = (int) Math.round(image.getHeight() * zoomFactor);
  134.                     break;
  135.                 case FIT_BOTH:
  136.                     if (image.getWidth() > image.getHeight()) {
  137.                         w = getWidth();
  138.                         h = (int) (w / getAR());
  139.  
  140.                         if (h > getHeight()) {
  141.                             h = getHeight();
  142.                             w = (int) (h * getAR());
  143.                         }
  144.                     } else {
  145.                         h = getHeight();
  146.                         w = (int) (h * getAR());
  147.  
  148.                         if (w > getWidth()) {
  149.                             w = getWidth();
  150.                             h = (int) (w / getAR());
  151.                         }
  152.                     }
  153.                     break;
  154.                 case FIT_WIDTH:
  155.                     w = getWidth();
  156.                     h = (int) (w / getAR());
  157.                     break;
  158.                 case FIT_HEIGHT:
  159.                     h = getHeight();
  160.                     w = (int) (h * getAR());
  161.                     break;
  162.             }
  163.            
  164.             Graphics2D g2d = (Graphics2D) g;
  165.             g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
  166.             g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  167.             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  168.             g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  169.             g2d.drawImage(image, x, y, w, h, null);
  170.         } else if (errorIcon != null) {
  171.             w = errorIcon.getIconWidth();
  172.             h = errorIcon.getIconHeight();
  173.             x = (getWidth() - w) / 2;
  174.             y = (getHeight() - h) / 2;
  175.             errorIcon.paintIcon(this, g, x, y);
  176.         }
  177.     }
  178. }
  179. -----------------------------------------------------------------------------------------------------
  180. package evocr.components.pictureBox;
  181.  
  182. public enum PictureBoxSizeMode {
  183.     AUTO_SIZE,
  184.     CENTER_IMAGE,
  185.     NORMAL,
  186.     STRETCH_IMAGE,
  187.     ZOOM,
  188.     FIT_BOTH,
  189.     FIT_WIDTH,
  190.     FIT_HEIGHT
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement