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.Window; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.geom.GeneralPath; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Calendar; import java.util.GregorianCalendar; import javax.imageio.ImageIO; public class Topbar implements MouseListener { private int oldwidth, people; private GeneralPath background; BufferedImage t1,t2,t3,t4,t5; private GregorianCalendar time; private Rectangle[] buttons; private Terrain terrain; public Topbar(Terrain terrain){ background = new GeneralPath(); time = new GregorianCalendar(); buttons = new Rectangle[5]; this.terrain = terrain; try { t2 = ImageIO.read(Window.class.getResource("/topbar/play.png")); t1 = ImageIO.read(Window.class.getResource("/topbar/stop.png")); t3 = ImageIO.read(Window.class.getResource("/topbar/faster.png")); t4 = ImageIO.read(Window.class.getResource("/topbar/grid.png")); t5 = ImageIO.read(Window.class.getResource("/topbar/people.png")); } catch (IOException e) { e.printStackTrace(); } } 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(t1, screenwidth/4 + 40, 7, null ); g2.drawImage(t2, screenwidth/4 + 90, 7, null ); g2.drawImage(t3, screenwidth/4 + 130, 7, null ); g2.setFont(new Font("Sans-serif", Font.BOLD, 22)); g2.drawString(time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE), screenwidth/2-10 , 28); g2.drawImage(t4, screenwidth/4*3 -60, 7, null ); g2.drawImage(t5, screenwidth/4*3 -150, 7, null ); g2.setFont(new Font("Sans-serif", Font.PLAIN, 18)); g2.drawString(people + "", screenwidth/4*3 - 120, 27); } public void setTime(GregorianCalendar t){ time = t; } public void setPeople(int p){ people = p; } public Rectangle[] getButtons(){ return buttons; } @Override public void mouseClicked(MouseEvent e) { int button = clickedButton(e.getPoint()); if(button >=0){ if(button == 3) terrain.toggleGrid(); } } @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; } }