1 package fr.ifremer.reefdb.ui.swing.util.image;
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 import fr.ifremer.reefdb.config.ReefDbConfiguration;
27 import org.jdesktop.swingx.JXPanel;
28 import org.jdesktop.swingx.painter.MattePainter;
29 import org.jdesktop.swingx.util.PaintUtils;
30
31 import javax.swing.BorderFactory;
32 import javax.swing.JComponent;
33 import javax.swing.border.Border;
34 import java.awt.*;
35 import java.awt.image.BufferedImage;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class BackgroundPanel extends JXPanel {
51
52 private Image image;
53 private BufferedImage scaledImage;
54 private boolean selected = false;
55
56 private static final Border outsideBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);
57 private float alignmentX = 0.5f;
58 private float alignmentY = 0.5f;
59 public static final double NO_SCALE = 1d;
60 private boolean scaled = false;
61 private double ratio = NO_SCALE;
62
63
64
65
66 public BackgroundPanel() {
67 this(null, false);
68 }
69
70
71
72
73
74
75
76
77
78
79
80 public BackgroundPanel(Image image, boolean withBackground) {
81 setLayout(new BorderLayout());
82 if (withBackground) {
83 setBackgroundPainter(new MattePainter(PaintUtils.getCheckerPaint(Color.white, new Color(250, 250, 250), 50)));
84 }
85 setImage(image);
86 setSelected(false);
87 }
88
89
90
91
92
93
94
95
96
97
98 public void setImage(Image image) {
99 this.image = image;
100 repaint();
101 }
102
103
104
105
106
107
108 public boolean isSelected() {
109 return selected;
110 }
111
112
113
114
115
116
117 public void setSelected(boolean selected) {
118 this.selected = selected;
119 setBorder(BorderFactory.createCompoundBorder(outsideBorder,
120 selected ? BorderFactory.createMatteBorder(2, 2, 2, 2, ReefDbConfiguration.getInstance().getColorSelectedRow()) : null));
121 }
122
123
124
125
126
127
128
129
130
131
132 public void setImageAlignmentX(float alignmentX) {
133 this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
134 repaint();
135 }
136
137
138
139
140
141
142
143
144
145
146 public void setImageAlignmentY(float alignmentY) {
147 this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
148 repaint();
149 }
150
151
152
153
154
155
156 public void setScaled(boolean scaled) {
157 this.scaled = scaled;
158 scaleImage();
159 repaint();
160 }
161
162 public void setRatio(double ratio) {
163 this.ratio = ratio;
164 setScaled(ratio != NO_SCALE);
165 }
166
167
168
169
170 private void scaleImage() {
171 if (scaled && image != null) {
172
173 int imageWidth = image.getWidth(this);
174 int imageHeight = image.getHeight(this);
175
176 if (ratio == NO_SCALE) {
177 int pw = super.getPreferredSize().width;
178 int ph = super.getPreferredSize().height;
179
180
181 if (pw < imageWidth || ph < imageHeight) {
182
183 double rw = (double) pw / (double) imageWidth;
184 double rh = (double) ph / (double) imageHeight;
185
186 ratio = Math.min(rw, rh);
187
188 }
189 }
190 int newWidth = Math.max((int) (imageWidth * ratio), 1);
191 int newHeight = Math.max((int) (imageHeight * ratio), 1);
192
193 scaledImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
194 Graphics2D g = scaledImage.createGraphics();
195 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
196 g.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, imageWidth, imageHeight, null);
197 g.dispose();
198
199 } else {
200 scaledImage = null;
201 }
202 }
203
204
205
206
207
208
209
210
211
212 public void add(JComponent component) {
213 add(component, null);
214 }
215
216
217
218
219
220
221
222
223 @Override
224 public Dimension getPreferredSize() {
225 Image imageToRender = scaled ? scaledImage : image;
226 if (imageToRender != null) {
227 Insets insets = getInsets();
228 return new Dimension(imageToRender.getWidth(this) + insets.left + insets.right, imageToRender.getHeight(this) + insets.top + insets.bottom);
229 } else {
230 return super.getPreferredSize();
231 }
232 }
233
234
235
236
237
238
239
240
241 @Override
242 protected void paintComponent(Graphics g) {
243 super.paintComponent(g);
244
245
246 Image imageToDraw = scaled ? scaledImage : image;
247 if (imageToDraw != null) {
248 Dimension d = getSize();
249 Insets insets = getInsets();
250 int width = d.width - insets.left - insets.right;
251 int height = d.height - insets.top - insets.left;
252 float x = (width - imageToDraw.getWidth(null)) * alignmentX;
253 float y = (height - imageToDraw.getHeight(null)) * alignmentY;
254 g.drawImage(imageToDraw, (int) x + insets.left, (int) y + insets.top, this);
255 }
256 }
257
258 }