package gui.simulator; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.GeneralPath; import java.io.Serializable; import java.util.Calendar; import java.util.GregorianCalendar; import javax.swing.JDialog; import agenda.Agenda; import gui.simulator.facilities.ImageType; public class Topbar implements MouseListener, Serializable { private int oldwidth; private GeneralPath background; private GregorianCalendar time; private Rectangle[] buttons; private boolean[] buttonsState; private Terrain terrain; private SimulatorPane simulator; private Agenda agenda; public Topbar(Terrain terrain, SimulatorPane simulator, Agenda agenda){ background = new GeneralPath(); time = new GregorianCalendar(); time.setTimeInMillis(agenda.firstActTime().getBeginTime().getTimeInMillis()-60*60*1000); buttons = new Rectangle[5]; buttonsState = new boolean[5]; this.terrain = terrain; this.simulator = simulator; this.agenda = agenda; } public void draw(Graphics2D g2, int screenwidth){ screenwidth += 100; if(oldwidth != screenwidth){ //only create a new general path when the window is resized background.reset(); background.moveTo(screenwidth/4, 0); background.curveTo(screenwidth/4, 0, screenwidth/4 -20, 20, screenwidth/4 +30, 40); background.lineTo(screenwidth/4*3-30, 40); background.curveTo(screenwidth/4*3-30, 40, screenwidth/4*3 + 20, 20, screenwidth/4*3, 0); background.closePath(); oldwidth = screenwidth; buttons[0] = new Rectangle(screenwidth/4+40,7,26,26); buttons[1] = new Rectangle(screenwidth/4+90,7,26,26); buttons[2] = new Rectangle(screenwidth/4+130,7,26,30); buttons[3] = new Rectangle(screenwidth/4*3-60,7,26,26); buttons[4] = new Rectangle(screenwidth/4*3-150,7,48,26); } g2.setColor(Color.GRAY); g2.fill(background); g2.setColor(Color.BLACK); g2.setStroke(new BasicStroke(2)); g2.draw(background); g2.drawImage(Images.getImage(ImageType.TopbarStopButton), screenwidth/4 + 40, 7, null ); g2.drawImage(Images.getImage((!buttonsState[1]) ? ImageType.TopbarPlayButton : ImageType.TopbarPauzeButton), screenwidth/4 + 90, 7, null ); g2.drawImage(Images.getImage(ImageType.TopbarFasterButton), screenwidth/4 + 130, 7, null ); g2.setFont(new Font("Sans-serif", Font.BOLD, 22)); g2.drawString(time.get(Calendar.HOUR_OF_DAY) + ":" + getMins(), screenwidth/2-10 , 28); g2.drawImage(Images.getImage(ImageType.TopbarGridButton), screenwidth/4*3 -60, 7, null ); g2.drawImage(Images.getImage(ImageType.TopbarPeopleButton), screenwidth/4*3 -150, 7, null ); g2.setFont(new Font("Sans-serif", Font.PLAIN, 18)); g2.drawString(terrain.getMaxVisitors() + "", screenwidth/4*3 - 120, 27); } public GregorianCalendar getTime() { return time; } public Rectangle[] getButtons(){ return buttons; } public String getMins(){ String minutesString = ""+time.get(Calendar.MINUTE); if(Integer.parseInt(minutesString) < 10){ minutesString = "0" + time.get(Calendar.MINUTE); } return minutesString; } @Override public void mouseClicked(MouseEvent e) { int button = clickedButton(e.getPoint()); if(button >=0){ if(button == 3){ terrain.toggleGrid(); }else if(button == 0){ simulator.changeSpeed(SimulatorPane.Time.STOP); time.setTimeInMillis(agenda.firstActTime().getBeginTime().getTimeInMillis()-60*60*1000); buttonsState[1] = false; }else if(button == 1) { simulator.changeSpeed((!buttonsState[1] ? SimulatorPane.Time.PLAY : SimulatorPane.Time.PAUZE)); buttonsState[1] = !buttonsState[1]; }else if(button == 2){ simulator.changeSpeed((!buttonsState[2] ? SimulatorPane.Time.FASTER : SimulatorPane.Time.FASTEST)); }else if(button == 4){ JDialog dialog = new VisitorsDialogPanel(terrain); dialog.pack(); dialog.setVisible(true); } } } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } public boolean containsMouse(Point point){ if(clickedButton(point)>=0) return true; return false; } private int clickedButton(Point point){ for(int i = 0; i < getButtons().length; i++){ if(getButtons()[i].contains(point)){ return i; } } return -1; } public GeneralPath getBackground() { return background; } public void recalculate(){ time.setTimeInMillis(time.getTimeInMillis()+3000); } }