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
.UIManager
;
12 import javax
.swing
.UnsupportedLookAndFeelException
;
15 * Some Java Swing utilities.
19 public class UIUtils
{
21 * Set a fake "native look & feel" for the application if possible
22 * (check for the one currently in use, then try GTK).
24 * <b>Must</b> be called prior to any GUI work.
26 static public void setLookAndFeel() {
29 String noLF
= "javax.swing.plaf.metal.MetalLookAndFeel";
30 String lf
= UIManager
.getSystemLookAndFeelClassName();
32 lf
= "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
33 UIManager
.setLookAndFeel(lf
);
34 } catch (InstantiationException e
) {
35 } catch (ClassNotFoundException e
) {
36 } catch (UnsupportedLookAndFeelException e
) {
37 } catch (IllegalAccessException e
) {
42 * Draw a 3D-looking ellipse at the given location, if the given
43 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
44 * simple ellipse if not.
47 * the {@link Graphics} to draw on
59 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
60 int width
, int height
) {
61 if (g
instanceof Graphics2D
) {
62 Graphics2D g2
= (Graphics2D
) g
;
63 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
64 RenderingHints
.VALUE_ANTIALIAS_ON
);
66 // Retains the previous state
67 Paint oldPaint
= g2
.getPaint();
71 g2
.fillOval(x
, y
, width
, height
);
73 // Compute dark/bright colours
75 Color dark
= color
.darker();
76 Color bright
= color
.brighter();
77 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
79 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
81 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
84 // Adds shadows at the bottom left
85 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
87 g2
.fillOval(x
, y
, width
, height
);
89 // Adds highlights at the top right
90 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
92 g2
.fillOval(x
, y
, width
, height
);
95 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
96 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
97 new Color
[] { darkEnd
, darkPartial
},
98 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
100 g2
.fillOval(x
, y
, width
, height
);
102 // Adds inner highlight at the top right
103 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
104 Math
.min(width
/ 4f
, height
/ 4f
),
105 new float[] { 0.0f
, 0.8f
},
106 new Color
[] { bright
, brightEnd
},
107 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
109 g2
.fillOval(x
* 2, y
, width
, height
);
111 // Reset original paint
112 g2
.setPaint(oldPaint
);
115 g
.fillOval(x
, y
, width
, height
);