martinleung93

JPictureBox.java

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