Changeset 8edd04e in lost-haven for gamegui/TabbedWindow.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (5 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/TabbedWindow.java
r155577b r8edd04e 1 1 package gamegui; 2 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import java.util.*; 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.FontMetrics; 6 import java.awt.Graphics; 7 import java.awt.event.MouseEvent; 8 import java.util.ArrayList; 6 9 7 10 public class TabbedWindow extends Member { 8 private ArrayList<Window> windows; 9 private ArrayList<String> windowLabels; 10 private Window activeWindow; 11 private int tabHeight; 12 private Font tabFont; 13 14 public TabbedWindow(String newName, int newX, int newY, int newWidth, int newHeight, int newTabHeight, Font newFont) { 15 super(newName, newX, newY, newWidth, newHeight); 16 17 windows = new ArrayList<Window>(); 18 windowLabels = new ArrayList<String>(); 19 tabHeight = newTabHeight; 20 activeWindow = null; 21 tabFont = newFont; 22 } 23 24 public void add(Window newWindow, String name) { 25 newWindow.offset(getX(), getY()+tabHeight); 26 27 if(activeWindow == null) 28 activeWindow = newWindow; 29 windows.add(newWindow); 30 windowLabels.add(name); 31 } 32 33 public void clear() { 34 activeWindow = windows.get(0); 35 for(int x=0; x < windows.size(); x++) 36 windows.get(x).clear(); 37 } 38 39 public void offset(int xOffset, int yOffset) { 40 super.offset(xOffset, yOffset); 41 42 for(int x=0; x < windows.size(); x++) 43 windows.get(x).offset(xOffset, yOffset); 44 } 45 46 public Window getWindow(String aName) { 47 for(int x=0; x < windows.size(); x++) 48 if(windows.get(x).getName().equals(aName)) 49 return (Window)windows.get(x); 50 51 return null; 52 } 53 54 public boolean handleEvent(MouseEvent e) { 55 for(int x=0; x < windows.size(); x++) { 56 if(isClicked(getX() + x*getWidth()/windows.size(), getY(), getWidth()/windows.size(), tabHeight, e.getX(), e.getY())) { 57 activeWindow = windows.get(x); 58 return true; 59 } 60 } 61 62 return activeWindow.handleEvent(e); 63 } 64 65 private boolean isClicked(int x, int y, int width, int height, int mouseX, int mouseY) { 66 return x <= mouseX && mouseX <= x+width && y <= mouseY && mouseY <= y+height; 67 } 68 69 public void draw(Graphics g) { 70 FontMetrics metrics = g.getFontMetrics(tabFont); 71 72 g.setColor(Color.black); 73 g.fillRect(getX(), getY(), getWidth(), getHeight()); 74 75 g.setFont(tabFont); 76 for(int x=0; x < windows.size(); x++) 77 { 78 g.setColor(Color.green); 79 g.drawString(windowLabels.get(x), getX()+x*getWidth()/windows.size()+(getWidth()/windows.size()-metrics.stringWidth(windowLabels.get(x)))/2, getY() + (tabHeight + metrics.getHeight())/2 - 2); 80 81 g.setColor(Color.red); 82 g.drawLine(getX()+x*getWidth()/windows.size(), getY(), getX()+x*getWidth()/windows.size(), getY()+tabHeight); 83 84 if(windows.get(x).equals(activeWindow)) { 85 ((Member)(windows.get(x))).draw(g); 86 g.setColor(Color.red); 87 g.drawRect(getX(), getY(), getWidth(), getHeight()); 88 g.drawLine(getX(), getY()+tabHeight, getX()+x*getWidth()/windows.size(), getY()+tabHeight); 89 g.drawLine(getX()+(x+1)*getWidth()/windows.size(), getY()+tabHeight, getX()+getWidth(), getY()+tabHeight); 90 } 91 } 92 } 93 94 public String getActive() { 95 if(activeWindow != null) 96 return activeWindow.getName(); 97 else 98 return ""; 99 } 11 12 private ArrayList<Window> windows; 13 private ArrayList<String> windowLabels; 14 private Window activeWindow; 15 private int tabHeight; 16 private Font tabFont; 17 18 public TabbedWindow(String newName, int newX, int newY, int newWidth, int newHeight, int newTabHeight, Font newFont) { 19 super(newName, newX, newY, newWidth, newHeight); 20 this.windows = new ArrayList<Window>(); 21 this.windowLabels = new ArrayList<String>(); 22 this.tabHeight = newTabHeight; 23 this.activeWindow = null; 24 this.tabFont = newFont; 25 } 26 27 public void add(Window newWindow, String name) { 28 newWindow.offset(getX(), getY() + this.tabHeight); 29 if (this.activeWindow == null) { 30 this.activeWindow = newWindow; 31 } 32 this.windows.add(newWindow); 33 this.windowLabels.add(name); 34 } 35 36 public void clear() { 37 this.activeWindow = this.windows.get(0); 38 for (int x = 0; x < this.windows.size(); x++) { 39 ((Window)this.windows.get(x)).clear(); 40 } 41 } 42 43 public void offset(int xOffset, int yOffset) { 44 super.offset(xOffset, yOffset); 45 for (int x = 0; x < this.windows.size(); x++) { 46 ((Window)this.windows.get(x)).offset(xOffset, yOffset); 47 } 48 } 49 50 public Window getWindow(String aName) { 51 for (int x = 0; x < this.windows.size(); x++) { 52 if (((Window)this.windows.get(x)).getName().equals(aName)) { 53 return this.windows.get(x); 54 } 55 } 56 return null; 57 } 58 59 public boolean handleEvent(MouseEvent e) { 60 for (int x = 0; x < this.windows.size(); x++) { 61 if (isClicked(getX() + x * getWidth() / this.windows.size(), getY(), getWidth() / this.windows.size(), this.tabHeight, e.getX(), e.getY())) { 62 this.activeWindow = this.windows.get(x); 63 return true; 64 } 65 } 66 return this.activeWindow.handleEvent(e); 67 } 68 69 private boolean isClicked(int x, int y, int width, int height, int mouseX, int mouseY) { 70 return (x <= mouseX && mouseX <= x + width && y <= mouseY && mouseY <= y + height); 71 } 72 73 public void draw(Graphics g) { 74 FontMetrics metrics = g.getFontMetrics(this.tabFont); 75 g.setColor(Color.black); 76 g.fillRect(getX(), getY(), getWidth(), getHeight()); 77 g.setFont(this.tabFont); 78 for (int x = 0; x < this.windows.size(); x++) { 79 g.setColor(Color.green); 80 g.drawString(this.windowLabels.get(x), getX() + x * getWidth() / this.windows.size() + (getWidth() / this.windows.size() - metrics.stringWidth(this.windowLabels.get(x))) / 2, getY() + (this.tabHeight + metrics.getHeight()) / 2 - 2); 81 g.setColor(Color.red); 82 g.drawLine(getX() + x * getWidth() / this.windows.size(), getY(), getX() + x * getWidth() / this.windows.size(), getY() + this.tabHeight); 83 if (((Window)this.windows.get(x)).equals(this.activeWindow)) { 84 ((Member)this.windows.get(x)).draw(g); 85 g.setColor(Color.red); 86 g.drawRect(getX(), getY(), getWidth(), getHeight()); 87 g.drawLine(getX(), getY() + this.tabHeight, getX() + x * getWidth() / this.windows.size(), getY() + this.tabHeight); 88 g.drawLine(getX() + (x + 1) * getWidth() / this.windows.size(), getY() + this.tabHeight, getX() + getWidth(), getY() + this.tabHeight); 89 } 90 } 91 } 92 93 public String getActive() { 94 if (this.activeWindow != null) { 95 return this.activeWindow.getName(); 96 } 97 return ""; 98 } 100 99 }
Note:
See TracChangeset
for help on using the changeset viewer.