-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slider.java
97 lines (88 loc) · 3.36 KB
/
Slider.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Slider here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Slider extends UIBase {
private int r;
private double min;
private double max;
private Color handleColour;
private Color handleBorderColour;
private Color borderColour;
private int border;
private double value;
/**
* Constructor for Slider
*
* @param x Top left coordinate x
* @param y Top left coordinate y
* @param width Width of slider
* @param height Height of slider
* @param colour Background colour of slider
* @param r Radius of slider handle
* @param min Minimum value of the slider
* @param max Maxiumum value of the slider
* @param handleColour Colour of the handle
* @param handleBorderColour Border colour of the handle
* @param borderColour Border colour of the slider
* @param border Border width
*/
public Slider(int x, int y, int width, int height, Color colour, int r, double min, double max, Color handleColour, Color handleBorderColour, Color borderColour, int border) {
super(x, y, width, height, colour);
this.r = r;
this.min = min;
this.max = max;
this.handleColour = handleColour;
this.handleBorderColour = handleBorderColour;
this.borderColour = borderColour;
this.border = border;
this.value = (min + max) / 2;
createImage();
}
/**
* Listen for a mouse drag
*/
public void act() {
if (enabled) {
if (Greenfoot.mouseDragged(this)) {
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) drag(mouse.getX());
}
}
}
/**
* Update the value of the slider based on the mouse's x-position
*
* @param mouseX The x-coordinate of the mouse
*/
public void drag(int mouseX) {
value = (mouseX - this.x) / (double) width * (max - min) + min;
value = Math.max(Math.min(value, max), min);
createImage();
}
/**
* Draw the rectangle and border for the slider
* Draw the circle and border for the handle
*/
private void createImage() {
GreenfootImage image = new GreenfootImage(width, r * 2);
image.setColor(borderColour);
image.fillRect(r, (r * 2 - height) / 2, width - r * 2, height);
image.setColor(colour);
image.fillRect(r + border, (r * 2 - height) / 2 + border, width - r * 2 - border * 2, height - border * 2);
image.setColor(handleBorderColour);
image.fillOval((int) ((value - min) / (max - min) * (width - 2 * r)), 0, r * 2, r * 2);
image.setColor(handleColour);
image.fillOval((int) ((value - min) / (max - min) * (width - 2 * r)) + border, border, (r - border) * 2, (r - border) * 2);
setImage(image);
}
/**
* Get the value of the slider
*
* @return The value of the slider rounded to 2 decimal places
*/
public double getValue() { return Math.round(value * 100.0) / 100.0; }
}