• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package aurelienribon.utils.swing;
2 
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Graphics2D;
6 import java.awt.Rectangle;
7 import java.awt.TexturePaint;
8 import java.awt.image.BufferedImage;
9 import java.io.File;
10 import java.io.IOException;
11 import java.net.URL;
12 import java.util.logging.Level;
13 import java.util.logging.Logger;
14 import javax.imageio.ImageIO;
15 import javax.swing.JPanel;
16 
17 /**
18  * @author Aurelien Ribon | http://www.aurelienribon.com
19  */
20 public class ImagePanel extends JPanel {
21 	private BufferedImage background;
22 	private BufferedImage image;
23 	private boolean useRegion;
24 	private int x, y, width, height;
25 
setBackground(URL bgURL)26 	public void setBackground(URL bgURL) {
27 		try {
28 			background = ImageIO.read(bgURL);
29 		} catch (IOException ex) {
30 		}
31 	}
32 
clearImage()33 	public void clearImage() {
34 		image = null;
35 		repaint();
36 	}
37 
setImage(BufferedImage img)38 	public void setImage(BufferedImage img) {
39 		image = img;
40 		repaint();
41 	}
42 
setImage(File img)43 	public void setImage(File img) {
44 		setImage(img, 0, 0, 0, 0);
45 		useRegion = false;
46 	}
47 
setImage(URL imgUrl)48 	public void setImage(URL imgUrl) {
49 		setImage(imgUrl, 0, 0, 0, 0);
50 		useRegion = false;
51 	}
52 
setImage(File img, int x, int y, int width, int height)53 	public void setImage(File img, int x, int y, int width, int height) {
54 		this.x = x;
55 		this.y = y;
56 		this.width = width;
57 		this.height = height;
58 		useRegion = true;
59 
60 		try {
61 			image = img != null ? ImageIO.read(img) : null;
62 			repaint();
63 		} catch (IOException ex) {
64 			Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
65 		}
66 	}
67 
setImage(URL imgUrl, int x, int y, int width, int height)68 	public void setImage(URL imgUrl, int x, int y, int width, int height) {
69 		this.x = x;
70 		this.y = y;
71 		this.width = width;
72 		this.height = height;
73 		useRegion = true;
74 
75 		try {
76 			image = imgUrl != null ? ImageIO.read(imgUrl) : null;
77 			repaint();
78 		} catch (IOException ex) {
79 			Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
80 		}
81 	}
82 
83 	@Override
paintComponent(Graphics g)84 	protected void paintComponent(Graphics g) {
85 		Graphics2D gg = (Graphics2D)g;
86 		gg.setColor(Color.LIGHT_GRAY);
87 		gg.fillRect(0, 0, getWidth(), getHeight());
88 
89 		if (background != null) {
90 			TexturePaint paint = new TexturePaint(background, new Rectangle(0, 0, background.getWidth(), background.getHeight()));
91 			gg.setPaint(paint);
92 			gg.fillRect(0, 0, getWidth(), getHeight());
93 			gg.setPaint(null);
94 		}
95 
96 		if (image != null && !useRegion) {
97 			float panelRatio = (float)getWidth() / (float)getHeight();
98 			float imgRatio = (float)image.getWidth() / (float)image.getHeight();
99 
100 			if (imgRatio > panelRatio) {
101 				float tw = (float)getWidth();
102 				float th = (float)getWidth() / imgRatio;
103 				gg.drawImage(image, 0, (int)(getHeight()/2 - th/2), (int) tw, (int) th, null);
104 			} else {
105 				float tw = (float)getHeight() * imgRatio;
106 				float th = (float)getHeight();
107 				gg.drawImage(image, (int)((float)getWidth()/2 - tw/2), 0, (int) tw, (int) th, null);
108 			}
109 
110 		} else if (image != null && useRegion) {
111 			float panelRatio = (float)getWidth() / (float)getHeight();
112 			float imgRatio = (float)width / (float)height;
113 
114 			if (imgRatio > panelRatio) {
115 				int tw = getWidth();
116 				int th = (int) (getWidth() / imgRatio);
117 				int tx = 0;
118 				int ty = getHeight()/2 - th/2;
119 				gg.drawImage(image, tx, ty, tx + tw, ty + th, x, y, x + width, y + width, null);
120 			} else {
121 				int tw = (int) (getHeight() * imgRatio);
122 				int th = getHeight();
123 				int tx = getWidth()/2 - tw/2;
124 				int ty = 0;
125 				gg.drawImage(image, tx, ty, tx + tw, ty + th, x, y, x + width, y + width, null);
126 			}
127 		}
128 	}
129 }
130