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