1
1
/**
2
- * Scrollbar.
3
- *
4
- * Move the scrollbars left and right to change the positions of the images.
2
+ * Scrollbar.
3
+ *
4
+ * Move the scrollbars left and right to change the positions of the images.
5
5
*/
6
6
7
+ // True if a mouse button was pressed while no other button was.
8
+ boolean firstMousePress = false ;
7
9
HScrollbar hs1, hs2; // Two scrollbars
8
10
PImage img1, img2; // Two images to load
9
11
10
12
void setup () {
11
13
size (640 , 360 );
12
14
noStroke ();
13
-
15
+
14
16
hs1 = new HScrollbar (0 , height / 2 - 8 , width , 16 , 16 );
15
17
hs2 = new HScrollbar (0 , height / 2 + 8 , width , 16 , 16 );
16
-
18
+
17
19
// Load images
18
20
img1 = loadImage (" seedTop.jpg" );
19
21
img2 = loadImage (" seedBottom.jpg" );
20
22
}
21
23
22
24
void draw () {
23
25
background (255 );
24
-
26
+
25
27
// Get the position of the img1 scrollbar
26
- // and convert to a value to display the img1 image
28
+ // and convert to a value to display the img1 image
27
29
float img1Pos = hs1. getPos()- width / 2 ;
28
30
fill (255 );
29
31
image (img1, width / 2 - img1. width / 2 + img1Pos* 1.5 , 0 );
30
-
32
+
31
33
// Get the position of the img2 scrollbar
32
34
// and convert to a value to display the img2 image
33
35
float img2Pos = hs2. getPos()- width / 2 ;
34
36
fill (255 );
35
37
image (img2, width / 2 - img2. width / 2 + img2Pos* 1.5 , height / 2 );
36
-
38
+
37
39
hs1. update();
38
40
hs2. update();
39
41
hs1. display();
40
42
hs2. display();
41
-
43
+
42
44
stroke (0 );
43
45
line (0 , height / 2 , width , height / 2 );
46
+
47
+ // After it has been used in the sketch, set it back to false
48
+ if (firstMousePress) {
49
+ firstMousePress = false ;
50
+ }
44
51
}
45
52
53
+ void mousePressed () {
54
+ if (! firstMousePress) {
55
+ firstMousePress = true ;
56
+ }
57
+ }
46
58
47
59
class HScrollbar {
48
60
int swidth, sheight; // width and height of bar
@@ -74,7 +86,7 @@ class HScrollbar {
74
86
} else {
75
87
over = false ;
76
88
}
77
- if (mousePressed && over) {
89
+ if (firstMousePress && over) {
78
90
locked = true ;
79
91
}
80
92
if (! mousePressed ) {
@@ -94,7 +106,7 @@ class HScrollbar {
94
106
95
107
boolean overEvent () {
96
108
if (mouseX > xpos && mouseX < xpos+ swidth &&
97
- mouseY > ypos && mouseY < ypos+ sheight) {
109
+ mouseY > ypos && mouseY < ypos+ sheight) {
98
110
return true ;
99
111
} else {
100
112
return false ;
0 commit comments