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