-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingBarJslider.java
More file actions
114 lines (93 loc) · 4.52 KB
/
MovingBarJslider.java
File metadata and controls
114 lines (93 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MovingBarJslider extends JSlider {
private UIDefaults d;
private UIDefaults z;
public MovingBarJslider(int min, int max, int value) {
super(min, max, value);
this.setOpaque(false);
d = new UIDefaults();
d.put("Slider:SliderTrack[Enabled].backgroundPainter", new Painter<MovingBarJslider>() {
@Override
public void paint(Graphics2D g, MovingBarJslider c, int w, int h) {
int arc = 10;
int trackHeight = 8;
int trackWidth = w - 2;
int fillTop = 4;
int fillLeft = 1;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(1.5f));
g.setColor(Color.GRAY);
g.fillRoundRect(fillLeft, fillTop, trackWidth, trackHeight, arc, arc);
int fillBottom = fillTop + trackHeight;
int fillRight = xPositionForValue(
c.getValue(), c,
new Rectangle(fillLeft, fillTop, trackWidth, fillBottom - fillTop));
g.setColor(new Color(40, 215, 40));
g.fillRect(fillLeft + 1, fillTop + 1, fillRight - fillLeft, fillBottom - fillTop);
g.setColor(Color.GRAY);
g.drawRoundRect(fillLeft, fillTop, trackWidth, trackHeight, arc, arc);
}
protected int xPositionForValue(int value, MovingBarJslider slider, Rectangle trackRect) {
int min = slider.getMinimum();
int max = slider.getMaximum();
int trackLength = trackRect.width;
double valueRange = (double) max - (double) min;
double pixelsPerValue = (double) trackLength / valueRange;
int trackLeft = trackRect.x;
int trackRight = trackRect.x + (trackRect.width - 1);
int xPosition;
xPosition = trackLeft;
xPosition += Math.round(pixelsPerValue * ((double) value - min));
xPosition = Math.max(trackLeft, xPosition);
xPosition = Math.min(trackRight, xPosition);
return xPosition;
}
});
z = new UIDefaults();
z.put("Slider:SliderTrack[Enabled].backgroundPainter", new Painter<MovingBarJslider>() {
@Override
public void paint(Graphics2D g, MovingBarJslider c, int w, int h) {
int arc = 10;
int trackHeight = 8;
int trackWidth = w - 2;
int fillTop = 4;
int fillLeft = 1;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(1.5f));
g.setColor(Color.GRAY);
g.fillRoundRect(fillLeft, fillTop, trackWidth, trackHeight, arc, arc);
int fillBottom = fillTop + trackHeight;
int fillRight = xPositionForValue(
c.getValue(), c,
new Rectangle(fillLeft, fillTop, trackWidth, fillBottom - fillTop));
g.setColor(new Color(40, 215, 40));
g.fillRect(fillLeft + 1, fillTop + 1, fillRight - fillLeft, fillBottom - fillTop);
g.setColor(Color.GRAY);
g.drawRoundRect(fillLeft, fillTop, trackWidth, trackHeight, arc, arc);
}
protected int xPositionForValue(int value, MovingBarJslider slider, Rectangle trackRect) {
int min = slider.getMinimum();
int max = slider.getMaximum();
int trackLength = trackRect.width;
double valueRange = (double) max - (double) min;
double pixelsPerValue = (double) trackLength / valueRange;
int trackLeft = trackRect.x;
int trackRight = trackRect.x + (trackRect.width - 1);
int xPosition;
xPosition = trackLeft;
xPosition += Math.round(pixelsPerValue * ((double) value - min));
xPosition = Math.max(trackLeft, xPosition);
xPosition = Math.min(trackRight, xPosition);
return xPosition;
}
});
putClientProperty("Nimbus.Overrides", z);
this.addMouseListener(new MouseAdapter() {
});
}
}