package gamegui; import java.awt.FontMetrics; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.Font; import java.util.ArrayList; public class TabbedWindow extends Member { private ArrayList windows; private ArrayList windowLabels; private Window activeWindow; private int tabHeight; private Font tabFont; public TabbedWindow(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final int newTabHeight, final Font newFont) { super(newName, newX, newY, newWidth, newHeight); this.windows = new ArrayList(); this.windowLabels = new ArrayList(); this.tabHeight = newTabHeight; this.activeWindow = null; this.tabFont = newFont; } public void add(final Window newWindow, final String name) { newWindow.offset(this.getX(), this.getY() + this.tabHeight); if (this.activeWindow == null) { this.activeWindow = newWindow; } this.windows.add(newWindow); this.windowLabels.add(name); } @Override public void clear() { this.activeWindow = this.windows.get(0); for (int x = 0; x < this.windows.size(); ++x) { this.windows.get(x).clear(); } } public void offset(final int xOffset, final int yOffset) { super.offset(xOffset, yOffset); for (int x = 0; x < this.windows.size(); ++x) { this.windows.get(x).offset(xOffset, yOffset); } } public Window getWindow(final String aName) { for (int x = 0; x < this.windows.size(); ++x) { if (this.windows.get(x).getName().equals(aName)) { return this.windows.get(x); } } return null; } @Override public boolean handleEvent(final MouseEvent e) { for (int x = 0; x < this.windows.size(); ++x) { if (this.isClicked(this.getX() + x * this.getWidth() / this.windows.size(), this.getY(), this.getWidth() / this.windows.size(), this.tabHeight, e.getX(), e.getY())) { this.activeWindow = this.windows.get(x); return true; } } return this.activeWindow.handleEvent(e); } private boolean isClicked(final int x, final int y, final int width, final int height, final int mouseX, final int mouseY) { return x <= mouseX && mouseX <= x + width && y <= mouseY && mouseY <= y + height; } @Override public void draw(final Graphics g) { final FontMetrics metrics = g.getFontMetrics(this.tabFont); g.setColor(Color.black); g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.setFont(this.tabFont); for (int x = 0; x < this.windows.size(); ++x) { g.setColor(Color.green); g.drawString(this.windowLabels.get(x), this.getX() + x * this.getWidth() / this.windows.size() + (this.getWidth() / this.windows.size() - metrics.stringWidth(this.windowLabels.get(x))) / 2, this.getY() + (this.tabHeight + metrics.getHeight()) / 2 - 2); g.setColor(Color.red); g.drawLine(this.getX() + x * this.getWidth() / this.windows.size(), this.getY(), this.getX() + x * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight); if (this.windows.get(x).equals(this.activeWindow)) { this.windows.get(x).draw(g); g.setColor(Color.red); g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); g.drawLine(this.getX(), this.getY() + this.tabHeight, this.getX() + x * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight); g.drawLine(this.getX() + (x + 1) * this.getWidth() / this.windows.size(), this.getY() + this.tabHeight, this.getX() + this.getWidth(), this.getY() + this.tabHeight); } } } public String getActive() { if (this.activeWindow != null) { return this.activeWindow.getName(); } return ""; } }