diff options
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/homelinux/nikiroo/utils/gui/JBackgroundPanel.java | 308 |
1 files changed, 308 insertions, 0 deletions
diff --git a/src/net/homelinux/nikiroo/utils/gui/JBackgroundPanel.java b/src/net/homelinux/nikiroo/utils/gui/JBackgroundPanel.java new file mode 100644 index 0000000..ba53b86 --- /dev/null +++ b/src/net/homelinux/nikiroo/utils/gui/JBackgroundPanel.java | |||
@@ -0,0 +1,308 @@ | |||
1 | package net.homelinux.nikiroo.utils.gui; | ||
2 | |||
3 | import java.awt.BorderLayout; | ||
4 | import java.awt.Component; | ||
5 | import java.awt.Dimension; | ||
6 | import java.awt.Graphics; | ||
7 | import java.awt.Graphics2D; | ||
8 | import java.awt.Image; | ||
9 | import java.awt.Insets; | ||
10 | import java.awt.LayoutManager; | ||
11 | import java.awt.Paint; | ||
12 | import java.awt.Rectangle; | ||
13 | |||
14 | import javax.swing.ImageIcon; | ||
15 | import javax.swing.JComponent; | ||
16 | import javax.swing.JPanel; | ||
17 | import javax.swing.JScrollPane; | ||
18 | import javax.swing.JViewport; | ||
19 | |||
20 | /** | ||
21 | * Support custom painting on a panel in the form of <li>images - that can be | ||
22 | * scaled, tiled or painted at original size</li> <li>non solid painting - that | ||
23 | * can be done by using a Paint object</li> Also, any component added directly | ||
24 | * to this panel will be made non-opaque so that the custom painting can show | ||
25 | * through. | ||
26 | */ | ||
27 | public class JBackgroundPanel extends JPanel { | ||
28 | private static final long serialVersionUID = 1L; | ||
29 | |||
30 | public static final int SCALED = 0; | ||
31 | public static final int TILED = 1; | ||
32 | public static final int ACTUAL = 2; | ||
33 | public static final int RATIO = 3; | ||
34 | |||
35 | private Paint painter; | ||
36 | private Image image; | ||
37 | private int style = SCALED; | ||
38 | private float alignmentX = 0.5f; | ||
39 | private float alignmentY = 0.5f; | ||
40 | private boolean isTransparentAdd = true; | ||
41 | |||
42 | /** | ||
43 | * Empty {@link JBackgroundPanel}. | ||
44 | */ | ||
45 | public JBackgroundPanel() { | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * Empty {@link JBackgroundPanel} with the given {@link LayoutManager}. | ||
50 | */ | ||
51 | public JBackgroundPanel(LayoutManager layout) { | ||
52 | super(layout); | ||
53 | } | ||
54 | |||
55 | /* | ||
56 | * Set image as the background with the SCALED style | ||
57 | */ | ||
58 | public JBackgroundPanel(ImageIcon image) { | ||
59 | this(image.getImage()); | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * Set image as the background with the SCALED style | ||
64 | */ | ||
65 | public JBackgroundPanel(ImageIcon image, int style) { | ||
66 | this(image.getImage(), style); | ||
67 | } | ||
68 | |||
69 | /* | ||
70 | * Set image as the backround with the specified style and alignment | ||
71 | */ | ||
72 | public JBackgroundPanel(ImageIcon image, int style, float alignmentX, float alignmentY) { | ||
73 | this(image.getImage(), style, alignmentX, alignmentY); | ||
74 | } | ||
75 | |||
76 | /* | ||
77 | * Set image as the background with the SCALED style | ||
78 | */ | ||
79 | public JBackgroundPanel(Image image) { | ||
80 | this(image, SCALED); | ||
81 | } | ||
82 | |||
83 | /* | ||
84 | * Set image as the background with the specified style | ||
85 | */ | ||
86 | public JBackgroundPanel(Image image, int style) { | ||
87 | setImage(image); | ||
88 | setStyle(style); | ||
89 | setLayout(new BorderLayout()); | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | * Set image as the backround with the specified style and alignment | ||
94 | */ | ||
95 | public JBackgroundPanel(Image image, int style, float alignmentX, float alignmentY) { | ||
96 | setImage(image); | ||
97 | setStyle(style); | ||
98 | setImageAlignmentX(alignmentX); | ||
99 | setImageAlignmentY(alignmentY); | ||
100 | setLayout(new BorderLayout()); | ||
101 | } | ||
102 | |||
103 | /* | ||
104 | * Use the Paint interface to paint a background | ||
105 | */ | ||
106 | public JBackgroundPanel(Paint painter) { | ||
107 | setPaint(painter); | ||
108 | setLayout(new BorderLayout()); | ||
109 | } | ||
110 | |||
111 | /* | ||
112 | * Set the image used as the background | ||
113 | */ | ||
114 | public void setImage(Image image) { | ||
115 | this.image = image; | ||
116 | repaint(); | ||
117 | } | ||
118 | |||
119 | /* | ||
120 | * Set the style used to paint the background image | ||
121 | */ | ||
122 | public void setStyle(int style) { | ||
123 | this.style = style; | ||
124 | repaint(); | ||
125 | } | ||
126 | |||
127 | /* | ||
128 | * Set the Paint object used to paint the background | ||
129 | */ | ||
130 | public void setPaint(Paint painter) { | ||
131 | this.painter = painter; | ||
132 | repaint(); | ||
133 | } | ||
134 | |||
135 | /* | ||
136 | * Specify the horizontal alignment of the image when using ACTUAL style | ||
137 | */ | ||
138 | public void setImageAlignmentX(float alignmentX) { | ||
139 | this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX; | ||
140 | repaint(); | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | * Specify the horizontal alignment of the image when using ACTUAL style | ||
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 | * Override method so we can make the component transparent | ||
153 | */ | ||
154 | public void add(JComponent component) { | ||
155 | add(component, null); | ||
156 | } | ||
157 | |||
158 | /* | ||
159 | * Override to provide a preferred size equal to the image size | ||
160 | */ | ||
161 | @Override | ||
162 | public Dimension getPreferredSize() { | ||
163 | if (image == null) | ||
164 | return super.getPreferredSize(); | ||
165 | else | ||
166 | return new Dimension(image.getWidth(null), image.getHeight(null)); | ||
167 | } | ||
168 | |||
169 | /* | ||
170 | * Override method so we can make the component transparent | ||
171 | */ | ||
172 | public void add(JComponent component, Object constraints) { | ||
173 | if (isTransparentAdd) { | ||
174 | makeComponentTransparent(component); | ||
175 | } | ||
176 | |||
177 | super.add(component, constraints); | ||
178 | } | ||
179 | |||
180 | /* | ||
181 | * Controls whether components added to this panel should automatically be | ||
182 | * made transparent. That is, setOpaque(false) will be invoked. The default | ||
183 | * is set to true. | ||
184 | */ | ||
185 | public void setTransparentAdd(boolean isTransparentAdd) { | ||
186 | this.isTransparentAdd = isTransparentAdd; | ||
187 | } | ||
188 | |||
189 | /* | ||
190 | * Try to make the component transparent. For components that use renderers, | ||
191 | * like JTable, you will also need to change the renderer to be transparent. | ||
192 | * An easy way to do this it to set the background of the table to a Color | ||
193 | * using an alpha value of 0. | ||
194 | */ | ||
195 | private void makeComponentTransparent(JComponent component) { | ||
196 | component.setOpaque(false); | ||
197 | |||
198 | if (component instanceof JScrollPane) { | ||
199 | JScrollPane scrollPane = (JScrollPane) component; | ||
200 | JViewport viewport = scrollPane.getViewport(); | ||
201 | viewport.setOpaque(false); | ||
202 | Component c = viewport.getView(); | ||
203 | |||
204 | if (c instanceof JComponent) { | ||
205 | ((JComponent) c).setOpaque(false); | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | |||
210 | /* | ||
211 | * Add custom painting | ||
212 | */ | ||
213 | @Override | ||
214 | protected void paintComponent(Graphics g) { | ||
215 | super.paintComponent(g); | ||