1 package be
.nikiroo
.utils
.ui
;
4 import java
.awt
.GradientPaint
;
5 import java
.awt
.Graphics
;
6 import java
.awt
.Graphics2D
;
8 import java
.awt
.RadialGradientPaint
;
9 import java
.awt
.RenderingHints
;
11 import javax
.swing
.JComponent
;
12 import javax
.swing
.JScrollPane
;
13 import javax
.swing
.UIManager
;
14 import javax
.swing
.UnsupportedLookAndFeelException
;
17 * Some Java Swing utilities.
21 public class UIUtils
{
23 * Set a fake "native Look & Feel" for the application if possible
24 * (check for the one currently in use, then try GTK).
26 * <b>Must</b> be called prior to any GUI work.
28 * @return TRUE if it succeeded
30 static public boolean setLookAndFeel() {
32 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
33 String lf
= UIManager
.getSystemLookAndFeelClassName();
35 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
37 return setLookAndFeel(lf
);
41 * Switch to the given Look & Feel for the application if possible
42 * (check for the one currently in use, then try GTK).
44 * <b>Must</b> be called prior to any GUI work.
47 * the Look & Feel to use
49 * @return TRUE if it succeeded
51 static public boolean setLookAndFeel(String laf
) {
53 UIManager
.setLookAndFeel(laf
);
55 } catch (InstantiationException e
) {
56 } catch (ClassNotFoundException e
) {
57 } catch (UnsupportedLookAndFeelException e
) {
58 } catch (IllegalAccessException e
) {
65 * Draw a 3D-looking ellipse at the given location, if the given
66 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
67 * simple ellipse if not.
70 * the {@link Graphics} to draw on
82 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
83 int width
, int height
) {
84 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
88 * Draw a 3D-looking ellipse at the given location, if the given
89 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
90 * simple ellipse if not.
93 * the {@link Graphics} to draw on
105 * fill the content of the ellipse
107 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
108 int width
, int height
, boolean fill
) {
109 if (g
instanceof Graphics2D
) {
110 Graphics2D g2
= (Graphics2D
) g
;
111 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
112 RenderingHints
.VALUE_ANTIALIAS_ON
);
114 // Retains the previous state
115 Paint oldPaint
= g2
.getPaint();
120 g2
.fillOval(x
, y
, width
, height
);
122 g2
.drawOval(x
, y
, width
, height
);
125 // Compute dark/bright colours
127 Color dark
= color
.darker().darker();
128 Color bright
= color
.brighter().brighter();
129 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
131 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
133 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
134 bright
.getBlue(), 0);
136 // Adds shadows at the bottom left
137 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
140 g2
.fillOval(x
, y
, width
, height
);
142 g2
.drawOval(x
, y
, width
, height
);
144 // Adds highlights at the top right
145 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
148 g2
.fillOval(x
, y
, width
, height
);
150 g2
.drawOval(x
, y
, width
, height
);
154 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
155 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
156 new Color
[] { darkEnd
, darkPartial
},
157 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
160 g2
.fillOval(x
, y
, width
, height
);
162 g2
.drawOval(x
, y
, width
, height
);
165 // Adds inner highlight at the top right
166 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
167 Math
.min(width
/ 4f
, height
/ 4f
),
168 new float[] { 0.0f
, 0.8f
},
169 new Color
[] { bright
, brightEnd
},
170 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
173 g2
.fillOval(x
* 2, y
, width
, height
);
175 g2
.drawOval(x
* 2, y
, width
, height
);
178 // Reset original paint
179 g2
.setPaint(oldPaint
);
183 g
.fillOval(x
, y
, width
, height
);
185 g
.drawOval(x
, y
, width
, height
);
191 * Add a {@link JScrollPane} around the given panel and use a sensible (for
192 * me) increment for the mouse wheel.
195 * the panel to wrap in a {@link JScrollPane}
196 * @param allowHorizontal
197 * allow horizontal scrolling (not always desired)
199 * @return the {@link JScrollPane}
201 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
) {
202 return scroll(pane
, allowHorizontal
, true);
206 * Add a {@link JScrollPane} around the given panel and use a sensible (for
207 * me) increment for the mouse wheel.
210 * the panel to wrap in a {@link JScrollPane}
211 * @param allowHorizontal
212 * allow horizontal scrolling (not always desired)
213 * @param allowVertical
214 * allow vertical scrolling (usually yes, but sometimes you only
217 * @return the {@link JScrollPane}
219 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
,
220 boolean allowVertical
) {
221 JScrollPane scroll
= new JScrollPane(pane
);
223 scroll
.getVerticalScrollBar().setUnitIncrement(16);
224 scroll
.getHorizontalScrollBar().setUnitIncrement(16);
226 if (!allowHorizontal
) {
227 scroll
.setHorizontalScrollBarPolicy(
228 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);
230 if (!allowVertical
) {
231 scroll
.setVerticalScrollBarPolicy(
232 JScrollPane
.VERTICAL_SCROLLBAR_NEVER
);