1 | package gamegui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Graphics;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 |
|
---|
7 | public class ScrollBar extends Member {
|
---|
8 | int size;
|
---|
9 | int position;
|
---|
10 | int scrollSpeed;
|
---|
11 | boolean horizontal;
|
---|
12 |
|
---|
13 | public ScrollBar(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int scrollSpeed, final boolean horizontal) {
|
---|
14 | super(newName, newX, newY, newWidth, newHeight);
|
---|
15 | this.size = 0;
|
---|
16 | this.position = 0;
|
---|
17 | this.scrollSpeed = scrollSpeed;
|
---|
18 | this.horizontal = horizontal;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public void clear() {
|
---|
23 | this.size = 0;
|
---|
24 | this.position = 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | @Override
|
---|
28 | public boolean handleEvent(final MouseEvent e) {
|
---|
29 | final int x = e.getX();
|
---|
30 | final int y = e.getY();
|
---|
31 | if (this.horizontal) {
|
---|
32 | if (this.getY() <= y && y <= this.getY() + this.getHeight()) {
|
---|
33 | if (this.getX() <= x && x <= this.getX() + this.getHeight()) {
|
---|
34 | this.setPosition(this.position - this.scrollSpeed);
|
---|
35 | return true;
|
---|
36 | }
|
---|
37 | if (this.getX() + this.getWidth() - this.getHeight() <= x && x <= this.getX() + this.getWidth()) {
|
---|
38 | this.setPosition(this.position + this.scrollSpeed);
|
---|
39 | return true;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | return false;
|
---|
43 | }
|
---|
44 | if (this.getX() <= x && x <= this.getX() + this.getWidth()) {
|
---|
45 | if (this.getY() <= y && y <= this.getY() + this.getWidth()) {
|
---|
46 | this.setPosition(this.position - this.scrollSpeed);
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 | if (this.getY() + this.getHeight() - this.getWidth() <= y && y <= this.getY() + this.getHeight()) {
|
---|
50 | this.setPosition(this.position + this.scrollSpeed);
|
---|
51 | return true;
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void draw(final Graphics g) {
|
---|
59 | g.setColor(Color.black);
|
---|
60 | g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
61 | g.setColor(Color.red);
|
---|
62 | g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
---|
63 | if (this.horizontal) {
|
---|
64 | g.drawLine(this.getX() + this.getHeight(), this.getY(), this.getX() + this.getHeight(), this.getY() + this.getHeight());
|
---|
65 | g.drawLine(this.getX() + this.getWidth() - this.getHeight(), this.getY(), this.getX() + this.getWidth() - this.getHeight(), this.getY() + this.getHeight());
|
---|
66 | g.drawLine(this.getX() + this.getHeight() + this.position, this.getY(), this.getX() + this.getHeight() + this.position, this.getY() + this.getHeight());
|
---|
67 | g.drawLine(this.getX() + this.getHeight() + this.position + this.size, this.getY(), this.getX() + this.getHeight() + this.position + this.size, this.getY() + this.getHeight());
|
---|
68 | g.drawLine(this.getX() + this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 3 / 20, this.getX() + this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 17 / 20);
|
---|
69 | g.drawLine(this.getX() + this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 17 / 20, this.getX() + this.getHeight() * 3 / 20, this.getY() + this.getHeight() / 2);
|
---|
70 | g.drawLine(this.getX() + this.getHeight() * 3 / 20, this.getY() + this.getHeight() / 2, this.getX() + this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 3 / 20);
|
---|
71 | g.drawLine(this.getX() + this.getWidth() - this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 3 / 20, this.getX() + this.getWidth() - this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 17 / 20);
|
---|
72 | g.drawLine(this.getX() + this.getWidth() - this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 17 / 20, this.getX() + this.getWidth() - this.getHeight() * 3 / 20, this.getY() + this.getHeight() / 2);
|
---|
73 | g.drawLine(this.getX() + this.getWidth() - this.getHeight() * 3 / 20, this.getY() + this.getHeight() / 2, this.getX() + this.getWidth() - this.getHeight() * 17 / 20, this.getY() + this.getHeight() * 3 / 20);
|
---|
74 | } else {
|
---|
75 | g.drawLine(this.getX(), this.getY() + this.getWidth(), this.getX() + this.getWidth(), this.getY() + this.getWidth());
|
---|
76 | g.drawLine(this.getX(), this.getY() + this.getHeight() - this.getWidth(), this.getX() + this.getWidth(), this.getY() + this.getHeight() - this.getWidth());
|
---|
77 | g.drawLine(this.getX(), this.getY() + this.getWidth() + this.position, this.getX() + this.getWidth(), this.getY() + this.getWidth() + this.position);
|
---|
78 | g.drawLine(this.getX(), this.getY() + this.getWidth() + this.position + this.size, this.getX() + this.getWidth(), this.getY() + this.getWidth() + this.position + this.size);
|
---|
79 | g.drawLine(this.getX() + this.getWidth() * 3 / 20, this.getY() + this.getWidth() * 17 / 20, this.getX() + this.getWidth() * 17 / 20, this.getY() + this.getWidth() * 17 / 20);
|
---|
80 | g.drawLine(this.getX() + this.getWidth() * 17 / 20, this.getY() + this.getWidth() * 17 / 20, this.getX() + this.getWidth() / 2, this.getY() + this.getWidth() * 3 / 20);
|
---|
81 | g.drawLine(this.getX() + this.getWidth() / 2, this.getY() + this.getWidth() * 3 / 20, this.getX() + this.getWidth() * 3 / 20, this.getY() + this.getWidth() * 17 / 20);
|
---|
82 | g.drawLine(this.getX() + this.getWidth() * 3 / 20, this.getY() + this.getHeight() - this.getWidth() * 17 / 20, this.getX() + this.getWidth() * 17 / 20, this.getY() + this.getHeight() - this.getWidth() * 17 / 20);
|
---|
83 | g.drawLine(this.getX() + this.getWidth() * 17 / 20, this.getY() + this.getHeight() - this.getWidth() * 17 / 20, this.getX() + this.getWidth() / 2, this.getY() + this.getHeight() - this.getWidth() * 3 / 20);
|
---|
84 | g.drawLine(this.getX() + this.getWidth() / 2, this.getY() + this.getHeight() - this.getWidth() * 3 / 20, this.getX() + this.getWidth() * 3 / 20, this.getY() + this.getHeight() - this.getWidth() * 17 / 20);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public int getPosition() {
|
---|
89 | return this.position;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public int getSize() {
|
---|
93 | return this.size;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public int getMaxSize() {
|
---|
97 | if (this.horizontal) {
|
---|
98 | return this.getWidth() - 2 * this.getHeight();
|
---|
99 | }
|
---|
100 | return this.getHeight() - 2 * this.getWidth();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void setPosition(final int position) {
|
---|
104 | if (position > this.getMaxSize() - this.size) {
|
---|
105 | this.position = this.getMaxSize() - this.size;
|
---|
106 | } else if (position < 0) {
|
---|
107 | this.position = 0;
|
---|
108 | } else {
|
---|
109 | this.position = position;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void setSize(final int size) {
|
---|
114 | this.size = size;
|
---|
115 | }
|
---|
116 | }
|
---|