Topbar.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package gui.simulator;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics2D;
  6. import java.awt.Point;
  7. import java.awt.Rectangle;
  8. import java.awt.Window;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.awt.geom.GeneralPath;
  12. import java.awt.geom.Rectangle2D;
  13. import java.awt.image.BufferedImage;
  14. import java.io.IOException;
  15. import java.util.Calendar;
  16. import java.util.GregorianCalendar;
  17. import javax.imageio.ImageIO;
  18. public class Topbar implements MouseListener {
  19. private int oldwidth, people;
  20. private GeneralPath background;
  21. BufferedImage t1,t2,t3,t4,t5;
  22. private GregorianCalendar time;
  23. private Rectangle[] buttons;
  24. private Terrain terrain;
  25. public Topbar(Terrain terrain){
  26. background = new GeneralPath();
  27. time = new GregorianCalendar();
  28. buttons = new Rectangle[5];
  29. this.terrain = terrain;
  30. try {
  31. t2 = ImageIO.read(Window.class.getResource("/topbar/play.png"));
  32. t1 = ImageIO.read(Window.class.getResource("/topbar/stop.png"));
  33. t3 = ImageIO.read(Window.class.getResource("/topbar/faster.png"));
  34. t4 = ImageIO.read(Window.class.getResource("/topbar/grid.png"));
  35. t5 = ImageIO.read(Window.class.getResource("/topbar/people.png"));
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. public void draw(Graphics2D g2, int screenwidth){
  41. screenwidth += 100;
  42. if(oldwidth != screenwidth){
  43. //only create a new general path when the window is resized
  44. background.reset();
  45. background.moveTo(screenwidth/4, 0);
  46. background.curveTo(screenwidth/4, 0, screenwidth/4 -20, 20, screenwidth/4 +30, 40);
  47. background.lineTo(screenwidth/4*3-30, 40);
  48. background.curveTo(screenwidth/4*3-30, 40, screenwidth/4*3 + 20, 20, screenwidth/4*3, 0);
  49. background.closePath();
  50. oldwidth = screenwidth;
  51. buttons[0] = new Rectangle(screenwidth/4+40,7,26,26);
  52. buttons[1] = new Rectangle(screenwidth/4+90,7,26,26);
  53. buttons[2] = new Rectangle(screenwidth/4+130,7,26,30);
  54. buttons[3] = new Rectangle(screenwidth/4*3-60,7,26,26);
  55. buttons[4] = new Rectangle(screenwidth/4*3-150,7,48,26);
  56. }
  57. g2.setColor(Color.GRAY);
  58. g2.fill(background);
  59. g2.setColor(Color.BLACK);
  60. g2.setStroke(new BasicStroke(2));
  61. g2.draw(background);
  62. g2.drawImage(t1, screenwidth/4 + 40, 7, null );
  63. g2.drawImage(t2, screenwidth/4 + 90, 7, null );
  64. g2.drawImage(t3, screenwidth/4 + 130, 7, null );
  65. g2.setFont(new Font("Sans-serif", Font.BOLD, 22));
  66. g2.drawString(time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE), screenwidth/2-10 , 28);
  67. g2.drawImage(t4, screenwidth/4*3 -60, 7, null );
  68. g2.drawImage(t5, screenwidth/4*3 -150, 7, null );
  69. g2.setFont(new Font("Sans-serif", Font.PLAIN, 18));
  70. g2.drawString(people + "", screenwidth/4*3 - 120, 27);
  71. }
  72. public void setTime(GregorianCalendar t){
  73. time = t;
  74. }
  75. public void setPeople(int p){
  76. people = p;
  77. }
  78. public Rectangle[] getButtons(){
  79. return buttons;
  80. }
  81. @Override
  82. public void mouseClicked(MouseEvent e) {
  83. int button = clickedButton(e.getPoint());
  84. if(button >=0){
  85. if(button == 3) terrain.toggleGrid();
  86. }
  87. }
  88. @Override
  89. public void mouseEntered(MouseEvent e) {
  90. }
  91. @Override
  92. public void mouseExited(MouseEvent e) {
  93. }
  94. @Override
  95. public void mousePressed(MouseEvent e) {
  96. }
  97. @Override
  98. public void mouseReleased(MouseEvent e) {
  99. }
  100. public boolean containsMouse(Point point){
  101. if(clickedButton(point)>=0)
  102. return true;
  103. return false;
  104. }
  105. private int clickedButton(Point point){
  106. for(int i = 0; i < getButtons().length; i++){
  107. if(getButtons()[i].contains(point)){
  108. return i;
  109. }
  110. }
  111. return -1;
  112. }
  113. public GeneralPath getBackground() {
  114. return background;
  115. }
  116. }