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
, int width
, int height
) {
60 drawEllipse3D(g
, color
, x
, y
, width
, height
, true);
64 * Draw a 3D-looking ellipse at the given location, if the given
65 * {@link Graphics} object is compatible (with {@link Graphics2D}); draw a
66 * simple ellipse if not.
69 * the {@link Graphics} to draw on
81 * fill the content of the ellipse
83 static public void drawEllipse3D(Graphics g
, Color color
, int x
, int y
,
84 int width
, int height
, boolean fill
) {
85 if (g
instanceof Graphics2D
) {
86 Graphics2D g2
= (Graphics2D
) g
;
87 g2
.setRenderingHint(RenderingHints
.KEY_ANTIALIASING
,
88 RenderingHints
.VALUE_ANTIALIAS_ON
);
90 // Retains the previous state
91 Paint oldPaint
= g2
.getPaint();
96 g2
.fillOval(x
, y
, width
, height
);
98 g2
.drawOval(x
, y
, width
, height
);
101 // Compute dark/bright colours
103 Color dark
= color
.darker().darker();
104 Color bright
= color
.brighter().brighter();
105 Color darkEnd
= new Color(dark
.getRed(), dark
.getGreen(),
107 Color darkPartial
= new Color(dark
.getRed(), dark
.getGreen(),
109 Color brightEnd
= new Color(bright
.getRed(), bright
.getGreen(),
110 bright
.getBlue(), 0);
112 // Adds shadows at the bottom left
113 p
= new GradientPaint(0, height
, dark
, width
, 0, darkEnd
);
116 g2
.fillOval(x
, y
, width
, height
);
118 g2
.drawOval(x
, y
, width
, height
);
120 // Adds highlights at the top right
121 p
= new GradientPaint(width
, 0, bright
, 0, height
, brightEnd
);
124 g2
.fillOval(x
, y
, width
, height
);
126 g2
.drawOval(x
, y
, width
, height
);
130 p
= new RadialGradientPaint(x
+ width
/ 2f
, y
+ height
/ 2f
,
131 Math
.min(width
/ 2f
, height
/ 2f
), new float[] { 0f
, 1f
},
132 new Color
[] { darkEnd
, darkPartial
},
133 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
136 g2
.fillOval(x
, y
, width
, height
);
138 g2
.drawOval(x
, y
, width
, height
);
141 // Adds inner highlight at the top right
142 p
= new RadialGradientPaint(x
+ 3f
* width
/ 4f
, y
+ height
/ 4f
,
143 Math
.min(width
/ 4f
, height
/ 4f
),
144 new float[] { 0.0f
, 0.8f
},
145 new Color
[] { bright
, brightEnd
},
146 RadialGradientPaint
.CycleMethod
.NO_CYCLE
);
149 g2
.fillOval(x
* 2, y
, width
, height
);
151 g2
.drawOval(x
* 2, y
, width
, height
);
154 // Reset original paint
155 g2
.setPaint(oldPaint
);
159 g
.fillOval(x
, y
, width
, height
);
161 g
.drawOval(x
, y
, width
, height
);