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