package gamegui; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; public class ScrollBar extends Member { int size; int position; int scrollSpeed; boolean horizontal; public ScrollBar(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int scrollSpeed, final boolean horizontal) { super(newName, newX, newY, newWidth, newHeight); this.size = 0; this.position = 0; this.scrollSpeed = scrollSpeed; this.horizontal = horizontal; } @Override public void clear() { this.size = 0; this.position = 0; } @Override public boolean handleEvent(final MouseEvent e) { final int x = e.getX(); final int y = e.getY(); if (this.horizontal) { if (this.getY() <= y && y <= this.getY() + this.getHeight()) { if (this.getX() <= x && x <= this.getX() + this.getHeight()) { this.setPosition(this.position - this.scrollSpeed); return true; } if (this.getX() + this.getWidth() - this.getHeight() <= x && x <= this.getX() + this.getWidth()) { this.setPosition(this.position + this.scrollSpeed); return true; } } return false; } if (this.getX() <= x && x <= this.getX() + this.getWidth()) { if (this.getY() <= y && y <= this.getY() + this.getWidth()) { this.setPosition(this.position - this.scrollSpeed); return true; } if (this.getY() + this.getHeight() - this.getWidth() <= y && y <= this.getY() + this.getHeight()) { this.setPosition(this.position + this.scrollSpeed); return true; } } return false; } @Override public void draw(final Graphics g) { g.setColor(Color.black); g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.setColor(Color.red); g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); if (this.horizontal) { g.drawLine(this.getX() + this.getHeight(), this.getY(), this.getX() + this.getHeight(), this.getY() + this.getHeight()); g.drawLine(this.getX() + this.getWidth() - this.getHeight(), this.getY(), this.getX() + this.getWidth() - this.getHeight(), this.getY() + this.getHeight()); g.drawLine(this.getX() + this.getHeight() + this.position, this.getY(), this.getX() + this.getHeight() + this.position, this.getY() + this.getHeight()); g.drawLine(this.getX() + this.getHeight() + this.position + this.size, this.getY(), this.getX() + this.getHeight() + this.position + this.size, this.getY() + this.getHeight()); 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); 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); 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); 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); 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); 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); } else { g.drawLine(this.getX(), this.getY() + this.getWidth(), this.getX() + this.getWidth(), this.getY() + this.getWidth()); g.drawLine(this.getX(), this.getY() + this.getHeight() - this.getWidth(), this.getX() + this.getWidth(), this.getY() + this.getHeight() - this.getWidth()); g.drawLine(this.getX(), this.getY() + this.getWidth() + this.position, this.getX() + this.getWidth(), this.getY() + this.getWidth() + this.position); g.drawLine(this.getX(), this.getY() + this.getWidth() + this.position + this.size, this.getX() + this.getWidth(), this.getY() + this.getWidth() + this.position + this.size); 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); 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); 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); 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); 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); 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); } } public int getPosition() { return this.position; } public int getSize() { return this.size; } public int getMaxSize() { if (this.horizontal) { return this.getWidth() - 2 * this.getHeight(); } return this.getHeight() - 2 * this.getWidth(); } public void setPosition(final int position) { if (position > this.getMaxSize() - this.size) { this.position = this.getMaxSize() - this.size; } else if (position < 0) { this.position = 0; } else { this.position = position; } } public void setSize(final int size) { this.size = size; } }