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 static public void setLookAndFeel() {
31 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
32 String lf
= UIManager
.getSystemLookAndFeelClassName();
34 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
35 UIManager
.setLookAndFeel(lf
);
36 } catch (InstantiationException e
) {
37 } catch (ClassNotFoundException e
) {
38 } catch (UnsupportedLookAndFeelException e
) {
39 } catch (IllegalAccessException e
) {
44 * Draw a 3D-looking ellipse at the given location, if the given
45 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
46 * simple ellipse if not.
49 * the {@link Graphics} to draw on
61 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
62 int width
, int height
) {
63 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
67 * Draw a 3D-looking ellipse at the given location, if the given
68 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
69 * simple ellipse if not.
72 * the {@link Graphics} to draw on
84 * fill the content of the ellipse
86 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
87 int width
, int height
, boolean fill
) {
88 if (g
instanceof Graphics2D
) {
89 Graphics2D g2
= (Graphics2D
) g
;
90 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
91 RenderingHints
.VALUE_ANTIALIAS_ON
);
93 // Retains the previous state
94 Paint oldPaint
= g2
.getPaint();
99 g2
.fillOval(x
, y
, width
, height
);
101 g2
.drawOval(x
, y
, width
, height
);
104 // Compute dark/bright colours
106 Color dark
= color
.darker().darker();
107 Color bright
= color
.brighter().brighter();
108 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
110 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
112 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
113 bright
.getBlue(), 0);
115 // Adds shadows at the bottom left
116 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
119 g2
.fillOval(x
, y
, width
, height
);
121 g2
.drawOval(x
, y
, width
, height
);
123 // Adds highlights at the top right
124 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
127 g2
.fillOval(x
, y
, width
, height
);
129 g2
.drawOval(x
, y
, width
, height
);
133 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
134 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
135 new Color
[] { darkEnd
, darkPartial
},
136 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
139 g2
.fillOval(x
, y
, width
, height
);
141 g2
.drawOval(x
, y
, width
, height
);
144 // Adds inner highlight at the top right
145 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
146 Math
.min(width
/ 4f
, height
/ 4f
),
147 new float[] { 0.0f
, 0.8f
},
148 new Color
[] { bright
, brightEnd
},
149 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
152 g2
.fillOval(x
* 2, y
, width
, height
);
154 g2
.drawOval(x
* 2, y
, width
, height
);
157 // Reset original paint
158 g2
.setPaint(oldPaint
);
162 g
.fillOval(x
, y
, width
, height
);
164 g
.drawOval(x
, y
, width
, height
);
170 * Add a {@link JScrollPane} around the given panel and use a sensible (for
171 * me) increment for the mouse wheel.
174 * the panel to wrap in a {@link JScrollPane}
175 * @param allowHorizontal
176 * allow horizontal scrolling (not always desired)
178 * @return the {@link JScrollPane}
180 static public JScrollPane
scroll(JComponent pane
, boolean allowHorizontal
) {
181 JScrollPane scroll
= new JScrollPane(pane
);
182 scroll
.getVerticalScrollBar().setUnitIncrement(16);
183 if (!allowHorizontal
) {
184 scroll
.setHorizontalScrollBarPolicy(
185 JScrollPane
.HORIZONTAL_SCROLLBAR_NEVER
);