aboutsummaryrefslogtreecommitdiff
path: root/java/applets/src/aurelienribon/utils/swing/ImagePanel.java
blob: 8e9be0fe5ce6155c97c5ce64a0de2b017f556558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package aurelienribon.utils.swing;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * @author Aurelien Ribon | http://www.aurelienribon.com
 */
public class ImagePanel extends JPanel {
	private BufferedImage background;
	private BufferedImage image;
	private boolean useRegion;
	private int x, y, width, height;

	public void setBackground(URL bgURL) {
		try {
			background = ImageIO.read(bgURL);
		} catch (IOException ex) {
		}
	}

	public void clearImage() {
		image = null;
		repaint();
	}

	public void setImage(BufferedImage img) {
		image = img;
		repaint();
	}

	public void setImage(File img) {
		setImage(img, 0, 0, 0, 0);
		useRegion = false;
	}

	public void setImage(URL imgUrl) {
		setImage(imgUrl, 0, 0, 0, 0);
		useRegion = false;
	}

	public void setImage(File img, int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		useRegion = true;

		try {
			image = img != null ? ImageIO.read(img) : null;
			repaint();
		} catch (IOException ex) {
			Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public void setImage(URL imgUrl, int x, int y, int width, int height) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		useRegion = true;

		try {
			image = imgUrl != null ? ImageIO.read(imgUrl) : null;
			repaint();
		} catch (IOException ex) {
			Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D gg = (Graphics2D)g;
		gg.setColor(Color.LIGHT_GRAY);
		gg.fillRect(0, 0, getWidth(), getHeight());

		if (background != null) {
			TexturePaint paint = new TexturePaint(background, new Rectangle(0, 0, background.getWidth(), background.getHeight()));
			gg.setPaint(paint);
			gg.fillRect(0, 0, getWidth(), getHeight());
			gg.setPaint(null);
		}

		if (image != null && !useRegion) {
			float panelRatio = (float)getWidth() / (float)getHeight();
			float imgRatio = (float)image.getWidth() / (float)image.getHeight();

			if (imgRatio > panelRatio) {
				float tw = (float)getWidth();
				float th = (float)getWidth() / imgRatio;
				gg.drawImage(image, 0, (int)(getHeight()/2 - th/2), (int) tw, (int) th, null);
			} else {
				float tw = (float)getHeight() * imgRatio;
				float th = (float)getHeight();
				gg.drawImage(image, (int)((float)getWidth()/2 - tw/2), 0, (int) tw, (int) th, null);
			}

		} else if (image != null && useRegion) {
			float panelRatio = (float)getWidth() / (float)getHeight();
			float imgRatio = (float)width / (float)height;

			if (imgRatio > panelRatio) {
				int tw = getWidth();
				int th = (int) (getWidth() / imgRatio);
				int tx = 0;
				int ty = getHeight()/2 - th/2;
				gg.drawImage(image, tx, ty, tx + tw, ty + th, x, y, x + width, y + width, null);
			} else {
				int tw = (int) (getHeight() * imgRatio);
				int th = getHeight();
				int tx = getWidth()/2 - tw/2;
				int ty = 0;
				gg.drawImage(image, tx, ty, tx + tw, ty + th, x, y, x + width, y + width, null);
			}
		}
	}
}