Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / ui / UIUtils.java
... / ...
CommitLineData
1package be.nikiroo.utils.ui;
2
3import java.awt.Color;
4import java.awt.GradientPaint;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.Paint;
8import java.awt.RadialGradientPaint;
9import java.awt.RenderingHints;
10
11import javax.swing.UIManager;
12import javax.swing.UnsupportedLookAndFeelException;
13
14/**
15 * Some Java Swing utilities.
16 *
17 * @author niki
18 */
19public class UIUtils {
20 /**
21 * Set a fake "native look & feel" for the application if possible
22 * (check for the one currently in use, then try GTK).
23 * <p>
24 * <b>Must</b> be called prior to any GUI work.
25 */
26 static public void setLookAndFeel() {
27 // native look & feel
28 try {
29 String noLF = "javax.swing.plaf.metal.MetalLookAndFeel";
30 String lf = UIManager.getSystemLookAndFeelClassName();
31 if (lf.equals(noLF))
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) {
38 }
39 }
40
41 /**
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.
45 *
46 * @param g
47 * the {@link Graphics} to draw on
48 * @param color
49 * the base colour
50 * @param x
51 * the X coordinate
52 * @param y
53 * the Y coordinate
54 * @param width
55 * the width radius
56 * @param height
57 * the height radius
58 */
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);
65
66 // Retains the previous state
67 Paint oldPaint = g2.getPaint();
68
69 // Base shape
70 g2.setColor(color);
71 g2.fillOval(x, y, width, height);
72
73 // Compute dark/bright colours
74 Paint p = null;
75 Color dark = color.darker();
76 Color bright = color.brighter();
77 Color darkEnd = new Color(dark.getRed(), dark.getGreen(),
78 dark.getBlue(), 0);
79 Color darkPartial = new Color(dark.getRed(), dark.getGreen(),
80 dark.getBlue(), 64);
81 Color brightEnd = new Color(bright.getRed(), bright.getGreen(),
82 bright.getBlue(), 0);
83
84 // Adds shadows at the bottom left
85 p = new GradientPaint(0, height, dark, width, 0, darkEnd);
86 g2.setPaint(p);
87 g2.fillOval(x, y, width, height);
88
89 // Adds highlights at the top right
90 p = new GradientPaint(width, 0, bright, 0, height, brightEnd);
91 g2.setPaint(p);
92 g2.fillOval(x, y, width, height);
93
94 // Darken the edges
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);
99 g2.setPaint(p);
100 g2.fillOval(x, y, width, height);
101
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);
108 g2.setPaint(p);
109 g2.fillOval(x * 2, y, width, height);
110
111 // Reset original paint
112 g2.setPaint(oldPaint);
113 } else {
114 g.setColor(color);
115 g.fillOval(x, y, width, height);
116 }
117 }
118}