Topbar.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.awt.geom.GeneralPath;
  11. import java.io.Serializable;
  12. import java.util.Calendar;
  13. import java.util.GregorianCalendar;
  14. import javax.swing.JDialog;
  15. import agenda.Agenda;
  16. import gui.simulator.facilities.ImageType;
  17. public class Topbar implements MouseListener, Serializable {
  18. private int oldwidth;
  19. private GeneralPath background;
  20. private GregorianCalendar time;
  21. private Rectangle[] buttons;
  22. private boolean[] buttonsState;
  23. private Terrain terrain;
  24. private SimulatorPane simulator;
  25. private Agenda agenda;
  26. public Topbar(Terrain terrain, SimulatorPane simulator, Agenda agenda){
  27. background = new GeneralPath();
  28. time = new GregorianCalendar();
  29. time.setTimeInMillis(agenda.firstActTime().getBeginTime().getTimeInMillis()-60*60*1000);
  30. buttons = new Rectangle[5];
  31. buttonsState = new boolean[5];
  32. this.terrain = terrain;
  33. this.simulator = simulator;
  34. this.agenda = agenda;
  35. }
  36. public void draw(Graphics2D g2, int screenwidth){
  37. screenwidth += 100;
  38. if(oldwidth != screenwidth){
  39. //only create a new general path when the window is resized
  40. background.reset();
  41. background.moveTo(screenwidth/4, 0);
  42. background.curveTo(screenwidth/4, 0, screenwidth/4 -20, 20, screenwidth/4 +30, 40);
  43. background.lineTo(screenwidth/4*3-30, 40);
  44. background.curveTo(screenwidth/4*3-30, 40, screenwidth/4*3 + 20, 20, screenwidth/4*3, 0);
  45. background.closePath();
  46. oldwidth = screenwidth;
  47. buttons[0] = new Rectangle(screenwidth/4+40,7,26,26);
  48. buttons[1] = new Rectangle(screenwidth/4+90,7,26,26);
  49. buttons[2] = new Rectangle(screenwidth/4+130,7,26,30);
  50. buttons[3] = new Rectangle(screenwidth/4*3-60,7,26,26);
  51. buttons[4] = new Rectangle(screenwidth/4*3-150,7,48,26);
  52. }
  53. g2.setColor(Color.GRAY);
  54. g2.fill(background);
  55. g2.setColor(Color.BLACK);
  56. g2.setStroke(new BasicStroke(2));
  57. g2.draw(background);
  58. g2.drawImage(Images.getImage(ImageType.TopbarStopButton), screenwidth/4 + 40, 7, null );
  59. g2.drawImage(Images.getImage((!buttonsState[1]) ? ImageType.TopbarPlayButton : ImageType.TopbarPauzeButton), screenwidth/4 + 90, 7, null );
  60. g2.drawImage(Images.getImage(ImageType.TopbarFasterButton), screenwidth/4 + 130, 7, null );
  61. g2.setFont(new Font("Sans-serif", Font.BOLD, 22));
  62. g2.drawString(time.get(Calendar.HOUR_OF_DAY) + ":" + getMins(), screenwidth/2-10 , 28);
  63. g2.drawImage(Images.getImage(ImageType.TopbarGridButton), screenwidth/4*3 -60, 7, null );
  64. g2.drawImage(Images.getImage(ImageType.TopbarPeopleButton), screenwidth/4*3 -150, 7, null );
  65. g2.setFont(new Font("Sans-serif", Font.PLAIN, 18));
  66. g2.drawString(terrain.getMaxVisitors() + "", screenwidth/4*3 - 120, 27);
  67. }
  68. public GregorianCalendar getTime() {
  69. return time;
  70. }
  71. public Rectangle[] getButtons(){
  72. return buttons;
  73. }
  74. public String getMins(){
  75. String minutesString = ""+time.get(Calendar.MINUTE);
  76. if(Integer.parseInt(minutesString) < 10){
  77. minutesString = "0" + time.get(Calendar.MINUTE);
  78. }
  79. return minutesString;
  80. }
  81. @Override
  82. public void mouseClicked(MouseEvent e) {
  83. int button = clickedButton(e.getPoint());
  84. if(button >=0){
  85. if(button == 3){
  86. terrain.toggleGrid();
  87. }else if(button == 0){
  88. simulator.changeSpeed(SimulatorPane.Time.STOP);
  89. time.setTimeInMillis(agenda.firstActTime().getBeginTime().getTimeInMillis()-60*60*1000);
  90. buttonsState[1] = false;
  91. }else if(button == 1) {
  92. simulator.changeSpeed((!buttonsState[1] ? SimulatorPane.Time.PLAY : SimulatorPane.Time.PAUZE));
  93. buttonsState[1] = !buttonsState[1];
  94. }else if(button == 2){
  95. simulator.changeSpeed((!buttonsState[2] ? SimulatorPane.Time.FASTER : SimulatorPane.Time.FASTEST));
  96. }else if(button == 4){
  97. JDialog dialog = new VisitorsDialogPanel(terrain);
  98. dialog.pack();
  99. dialog.setVisible(true);
  100. }
  101. }
  102. }
  103. @Override
  104. public void mouseEntered(MouseEvent e) {
  105. }
  106. @Override
  107. public void mouseExited(MouseEvent e) {
  108. }
  109. @Override
  110. public void mousePressed(MouseEvent e) {
  111. }
  112. @Override
  113. public void mouseReleased(MouseEvent e) {
  114. }
  115. public boolean containsMouse(Point point){
  116. if(clickedButton(point)>=0)
  117. return true;
  118. return false;
  119. }
  120. private int clickedButton(Point point){
  121. for(int i = 0; i < getButtons().length; i++){
  122. if(getButtons()[i].contains(point)){
  123. return i;
  124. }
  125. }
  126. return -1;
  127. }
  128. public GeneralPath getBackground() {
  129. return background;
  130. }
  131. public void recalculate(){
  132. time.setTimeInMillis(time.getTimeInMillis()+3000);
  133. }
  134. }