View Javadoc
1   package com.bric.multislider;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Dimension;
6   import java.awt.GradientPaint;
7   import java.awt.Graphics2D;
8   import java.awt.LinearGradientPaint;
9   import java.awt.Paint;
10  import java.awt.Point;
11  import java.awt.Rectangle;
12  import java.awt.RenderingHints;
13  import java.awt.Shape;
14  import java.awt.geom.Point2D;
15  import java.awt.geom.Rectangle2D;
16  
17  import javax.swing.SwingConstants;
18  
19  public class AquaMultiThumbSliderUi<T> extends DefaultMultiThumbSliderUi<T>
20  {
21  
22      private static Color UPPER_GRAY = new Color(168, 168, 168);
23  
24      private static Color LOWER_GRAY = new Color(218, 218, 218);
25  
26      private static Color OUTLINE_OPACITY = new Color(0, 0, 0, 75);
27  
28      public AquaMultiThumbSliderUi(MultiThumbSlider<T> slider)
29      {
30          super(slider);
31          DEPTH = 4;
32          FOCUS_PADDING = 2;
33          trackHighlightColor = new Color(0x3a99fc);
34      }
35  
36      @Override
37      protected int getPreferredComponentDepth()
38      {
39          return 24;
40      }
41  
42      @Override
43      protected void paintFocus(Graphics2D g)
44      {
45          // do nothing, this is really handled in paintThumb now
46      }
47  
48      @Override
49      protected Dimension getThumbSize(int thumbIndex)
50      {
51          Thumb thumb = getThumb(thumbIndex);
52          if (Thumb.Hourglass.equals(thumb))
53          {
54              return new Dimension(5, 16);
55          }
56          else if (Thumb.Triangle.equals(thumb))
57          {
58              return new Dimension(14, 20);
59          }
60          else if (Thumb.Rectangle.equals(thumb))
61          {
62              return new Dimension(10, 20);
63          }
64          else
65          {
66              return new Dimension(16, 16);
67          }
68      }
69  
70      @Override
71      protected void paintTrack(Graphics2D g)
72      {
73          g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
74          Shape trackShape = getTrackOutline();
75  
76          GradientPaint gradient;
77          if (slider.getOrientation() == SwingConstants.HORIZONTAL)
78          {
79              gradient = new GradientPaint(new Point(trackRect.x, trackRect.y), UPPER_GRAY,
80                      new Point(trackRect.x, trackRect.y + trackRect.height), LOWER_GRAY);
81          }
82          else
83          {
84              gradient = new GradientPaint(new Point(trackRect.x, trackRect.y), UPPER_GRAY,
85                      new Point(trackRect.x + trackRect.width, trackRect.y), LOWER_GRAY);
86          }
87          g.setPaint(gradient);
88          g.fill(trackShape);
89  
90          paintTrackHighlight(g);
91  
92          g.setPaint(OUTLINE_OPACITY);
93          g.setStroke(new BasicStroke(1));
94          g.draw(trackShape);
95  
96          if (slider.isPaintTicks())
97          {
98              g.setColor(new Color(0x777777));
99              g.setStroke(new BasicStroke(1));
100             paintTick(g, .25f, 4, 9, false);
101             paintTick(g, .5f, 4, 9, false);
102             paintTick(g, .75f, 4, 9, false);
103             paintTick(g, 0f, 4, 9, false);
104             paintTick(g, 1f, 4, 9, false);
105         }
106     }
107 
108     @Override
109     protected Rectangle calculateTrackRect()
110     {
111         Rectangle r = super.calculateTrackRect();
112 
113         // why so much dead space? I don't know. This only tries to emulate
114         // what Apple is doing.
115         int k = 22;
116         if (slider.getOrientation() == SwingConstants.HORIZONTAL)
117         {
118             r.x = k;
119             r.width = slider.getWidth() - k * 2;
120         }
121         else
122         {
123             r.y = k;
124             r.height = slider.getHeight() - k * 2;
125         }
126         return r;
127 
128     }
129 
130     @Override
131     protected void paintThumb(Graphics2D g, int thumbIndex, float selected)
132     {
133         Shape outline = getThumbShape(thumbIndex);
134 
135         Rectangle2D thumbBounds = ShapeBounds.getBounds(outline);
136 
137         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
138         g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
139         Paint fill = new LinearGradientPaint(new Point2D.Double(0, thumbBounds.getMinY()),
140                 new Point2D.Double(0, thumbBounds.getMaxY()), new float[] {0, .5f, .501f, 1},
141                 new Color[] {new Color(0xFFFFFF), new Color(0xF4F4F4), new Color(0xECECEC), new Color(0xEDEDED)});
142         g.setPaint(fill);
143         g.fill(outline);
144 
145         if (mouseIsDown && thumbIndex == slider.getSelectedThumb())
146         {
147             g.setPaint(new Color(0, 0, 0, 28));
148             g.fill(outline);
149         }
150 
151         g.setStroke(new BasicStroke(1f));
152         g.setPaint(new Color(0, 0, 0, 110));
153         g.draw(outline);
154 
155         if (thumbIndex == slider.getSelectedThumb())
156         {
157             Color focusColor = new Color(0xa7, 0xd5, 0xff, 240);
158             PlafPaintUtils.paintFocus(g, outline, FOCUS_PADDING, focusColor, false);
159             g.setStroke(new BasicStroke(1f));
160             g.setPaint(new Color(0, 0, 0, 23));
161             g.draw(outline);
162         }
163     }
164 }