From ea7ecfcd562e3366e1b4877387e24dd526a4f495 Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 7 Apr 2015 13:12:02 +0200 Subject: [PATCH] Saven werkt van guus --- Agenda/Agenda.java | 221 +++++++++++++++++++++++++++++++++++++++ Agenda/Artist.java | 90 ++++++++++++++++ Agenda/Event.java | 211 +++++++++++++++++++++++++++++++++++++ Agenda/Stage.java | 43 ++++++++ Applicatie/Action.java | 38 +++++++ Applicatie/Images.java | 37 +++++++ Applicatie/MenuBar.java | 46 +++++--- Applicatie/Panel.java | 132 ++++++++++++++++------- Applicatie/SaveLoad.java | 144 +++++++++++++++++++++++++ Applicatie/Visitor.java | 219 ++++++++++++++++++++++++++++++++++++++ Objects/DrawObject.java | 68 +++++------- Objects/Entrance.java | 2 +- Objects/Food.java | 2 +- Objects/OldPath.java | 2 +- Objects/Stage.java | 2 +- Objects/Toilet.java | 2 +- Objects/Wall.java | 2 +- 17 files changed, 1163 insertions(+), 98 deletions(-) create mode 100644 Agenda/Agenda.java create mode 100644 Agenda/Artist.java create mode 100644 Agenda/Event.java create mode 100644 Agenda/Stage.java create mode 100644 Applicatie/Action.java create mode 100644 Applicatie/Images.java create mode 100644 Applicatie/SaveLoad.java create mode 100644 Applicatie/Visitor.java diff --git a/Agenda/Agenda.java b/Agenda/Agenda.java new file mode 100644 index 0000000..65e5a6d --- /dev/null +++ b/Agenda/Agenda.java @@ -0,0 +1,221 @@ +package Agenda; + +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.ArrayList; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; + +public class Agenda implements Serializable { + + private static final long serialVersionUID = 1; + + private ArrayList stages; + private ArrayList events; + private ArrayList artists; + + public Agenda() { + stages = new ArrayList(); + events = new ArrayList(); + artists = new ArrayList(); + } + + public void addStage(Stage stage){ + stages.add(stage); + } + + public void addEvent(Event event) { + events.add(event); + } + + public void addArtist(Artist artist) { + artists.add(artist); + } + + public ArrayList getEvents() + { + return events; + } + public ArrayList getArtists() + { + return artists; + } + public ArrayList getStages() + { + return stages; + } + + public void saveAgenda() + { + JFileChooser fileChooser = new JFileChooser(); + + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + fileChooser.setDialogTitle("Choose save location"); + int userSelection = fileChooser.showSaveDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.getName().endsWith(".agn")) + { + file = new File(file.getAbsolutePath() + ".agn"); + } + + if(file.exists()) + { + if(JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite: " + file.getName(), "Overwrite", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) + { + saveAgenda(file); + } + } + else + { + saveAgenda(file); + } + } + } + + public void loadAgenda() + { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + + fileChooser.setDialogTitle("Choose file"); + int userSelection = fileChooser.showOpenDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.exists()) + { + JOptionPane.showMessageDialog(null, "This file does not exist: " + file.getName()); + } + else + { + clearAgenda(); + fillAgenda(file); + JOptionPane.showMessageDialog(null, "Loaded succesfully"); + } + } + } + + public void fillAgenda(File file) { + FileInputStream fis = null; + ObjectInputStream ois = null; + + try { + + fis = new FileInputStream(file); + ois = new ObjectInputStream(fis); + + Object object; + object = ois.readObject(); + Agenda a = (Agenda) object; + events = a.getEvents(); + stages = a.getStages(); + artists = a.getArtists(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public void fillArtists() + { + for (Event e : events) + { + boolean exists = false; + String name = e.getArtist().getName(); + for (Artist a : artists) + { + if ( a.getName().equals(name) ) { + exists = true; + } + } + if ( exists == false) { + addArtist(e.getArtist()); + } + } + } + + public void fillStages() + { + for (Event e : events) + { + boolean exists = false; + String name = e.getStage().getName(); + for (Stage s : stages) + { + if ( s.getName().equals(name) ) { + exists = true; + } + } + if ( exists == false) { + addStage(e.getStage()); + } + } + } + + public void saveAgenda(File file) { + + try { + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos); + oos.writeObject(this); + oos.close(); + JOptionPane.showMessageDialog(null, "Saved succesfully"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void clearAgenda() + { + events.clear(); + stages.clear(); + artists.clear(); + } + + public void fillAllLists() + { + loadAgenda(); + fillStages(); + fillArtists(); + } +} diff --git a/Agenda/Artist.java b/Agenda/Artist.java new file mode 100644 index 0000000..b74f3e2 --- /dev/null +++ b/Agenda/Artist.java @@ -0,0 +1,90 @@ +package Agenda; + + +import java.io.Serializable; + +import javax.swing.ImageIcon; + + +public class Artist implements Serializable{ + + private static final long serialVersionUID = 1; + + private String name; + private String genre; + private ImageIcon image; + private String description; + private String background; + + public Artist(String name, String genre, String image, String description, String background) + { + this.name = name; + this.genre = genre; + this.description = description; + this.background = background; + + if ( !image.equals("null")) { + this.image = new ImageIcon(image); + } + } + + public String getName() + { + return this.name; + } + + public String getGenre() + { + return this.genre; + } + + public ImageIcon getImage() + { + return this.image; + } + + public String getDescription() + { + return this.description; + } + + public String getBackground() + { + return this.background; + } + + public void setName(String name) + { + this.name = name; + } + + public void setGenre(String genre) + { + this.genre = genre; + } + + public void setImageIconString(String image) + { + this.image = new ImageIcon(image); + } + + public void setImageIcon(ImageIcon image) + { + this.image = image; + } + + public void setDescription(String description) + { + this.description = description; + } + + public void setBackground(String background) + { + this.background = background; + } + + public String toString() + { + return name; + } +} diff --git a/Agenda/Event.java b/Agenda/Event.java new file mode 100644 index 0000000..cc89824 --- /dev/null +++ b/Agenda/Event.java @@ -0,0 +1,211 @@ +package Agenda; + + +import java.io.Serializable; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.GregorianCalendar; + +public class Event implements Serializable { + + private static final long serialVersionUID = 1; + + private String eventName; + private GregorianCalendar startDate; + private GregorianCalendar endDate; + private int startHour; + private int startMinute; + private int stopHour; + private int stopMinute; + private Artist artist; + private Stage stage; + private String description; + private int expectedPopularity; + + public Event(String eventName, int startYear, int startMonth, int startDay, + int startHour, int startMinute, int endYear, int endMonth, + int endDay, int endHour, int endMinute, Artist artist, Stage stage, + String description, int expectedPopularity) { + this.eventName = eventName; + this.startDate = new GregorianCalendar(startYear, startMonth - 1, + startDay, startHour, startMinute); + this.endDate = new GregorianCalendar(endYear, endMonth - 1, endDay, + endHour, endMinute); + this.startHour = startHour; + this.startMinute = startMinute; + this.stopHour = stopHour; + this.stopMinute = stopMinute; + this.stage = stage; + this.artist = artist; + this.description = description; + this.expectedPopularity = expectedPopularity; + } + + public Event(String eventName, GregorianCalendar startDate, + GregorianCalendar endDate, Artist artist, Stage stage, + String description, int expectedPopularity) { + this.eventName = eventName; + this.startDate = startDate; + this.endDate = endDate; + this.artist = artist; + this.stage = stage; + this.description = description; + this.expectedPopularity = expectedPopularity; + } + + public int getStart(){ + return (startHour * 100) + startMinute; + } + + public int getStop(){ + return (stopHour * 100) + stopMinute; + } + + public int getStopHour(){ + return this.stopHour; + } + + public String getEventName() { + return this.eventName; + } + + public GregorianCalendar getStartDate() { + return this.startDate; + } + + public GregorianCalendar getEndDate() { + return this.endDate; + } + + public Artist getArtist() { + return this.artist; + } + + public String getDescription() { + return this.description; + } + + public Stage getStage() { + return this.stage; + } + + public int getExpectedPopularity() { + return this.expectedPopularity; + } + + public void setEventName(String name) { + this.eventName = name; + } + + public void setStartDate(int startYear, int startMonth, int startDay, + int startHour, int startMinute) { + startDate.set(startYear, startMonth - 1, startDay, startHour, + startMinute); + } + + public void setStartDate(GregorianCalendar startDate) { + this.startDate = startDate; + } + + public void setEndDate(int endYear, int endMonth, int endDay, int endHour, + int endMinute) { + endDate.set(endYear, endMonth - 1, endDay, endHour, endMinute); + } + + public void setEndDate(GregorianCalendar endDate) { + this.endDate = endDate; + } + + public void setArtist(Artist artist) { + this.artist.setName(artist.getName()); + this.artist.setGenre(artist.getGenre()); + this.artist.setImageIcon(artist.getImage()); + this.artist.setDescription(artist.getDescription()); + } + + public void setDescription(String description) { + this.description = description; + } + + public void setExpectedPopularity(int popularity) { + this.expectedPopularity = popularity; + } + + public void setStage(Stage stage) { + this.stage = stage; + } + + public int getLength() + { + return getEndTime() - getStartTime(); + } + + public int getStartTime() + { + DateFormat format = new SimpleDateFormat("kkmm"); + format.setLenient(false); +// System.out.println(format.format(startDate.getTime())); + +// System.out.println(date.get(Calendar.HOUR_OF_DAY)); + int time = Integer.valueOf(format.format(startDate.getTime())); + int hours = time/100; + int minutes = time%100; + int finaltime; + if(minutes != 0) + { + finaltime = hours*60 + minutes; + } + else + { + finaltime = hours*60; + } + return finaltime; + } + + public int getEndTime() + { + DateFormat format = new SimpleDateFormat("kkmm"); + format.setLenient(false); +// System.out.println(format.format(endDate.getTime())); + int time = Integer.valueOf(format.format(endDate.getTime())); + int hours = time/100; + int minutes = time%100; + int finaltime; + if(minutes != 0) + { + finaltime = hours*60 + minutes; + } + else + { + finaltime = hours*60; + } + + return finaltime; + } + + public void setStartTime(int hours, int minutes) + { +// System.out.println("Start:" + hours + ":" + minutes); + startDate.set(Calendar.HOUR_OF_DAY, hours); + startDate.set(Calendar.MINUTE, minutes); +// System.out.println(startDate.getTime()); + } + + public void setEndTime(int hours, int minutes) + { +// System.out.println("End:" + hours + ":" + minutes); + endDate.set(Calendar.HOUR_OF_DAY, hours); + endDate.set(Calendar.MINUTE, minutes); +// System.out.println(endDate.getTime()); + } + + public String toString() { + DateFormat format = new SimpleDateFormat("dd-MM-yyyy"); + format.setLenient(false); + + return artist.getName() + " plays from " + + format.format(startDate.getTime()) + " to " + + format.format(endDate.getTime()) + "."; + } +} diff --git a/Agenda/Stage.java b/Agenda/Stage.java new file mode 100644 index 0000000..a7d123b --- /dev/null +++ b/Agenda/Stage.java @@ -0,0 +1,43 @@ +package Agenda; + + +import java.io.Serializable; + +public class Stage implements Serializable { + + private static final long serialVersionUID = 1; + + private String name; + private String description; + + public Stage(String name, String description) + { + this.name = name; + this.description = description; + } + + public String getName() + { + return this.name; + } + + public String getDescription() + { + return this.description; + } + + public void setDescription(String description) + { + this.description = description; + } + + public void setName(String name) + { + this.name = name; + } + + public String toString() + { + return name; + } +} diff --git a/Applicatie/Action.java b/Applicatie/Action.java new file mode 100644 index 0000000..089755a --- /dev/null +++ b/Applicatie/Action.java @@ -0,0 +1,38 @@ +package Applicatie; + +import java.awt.geom.Point2D; + +public class Action +{ + + private Point2D position; + private int starttime; + private int duration; + + public Action(Point2D position, int starttime, int duration) + { + this.position = position; + this.starttime = starttime; + this.duration = duration; + } + + public Point2D getPosition() + { + return position; + } + + public int getStartTime() + { + return starttime; + } + + public int getStoptime() + { + return (starttime + duration); + } + + public int getDuration() + { + return duration; + } +} \ No newline at end of file diff --git a/Applicatie/Images.java b/Applicatie/Images.java new file mode 100644 index 0000000..4bb45ff --- /dev/null +++ b/Applicatie/Images.java @@ -0,0 +1,37 @@ +package Applicatie; + +import java.awt.Image; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.ImageIcon; + +public class Images +{ + + private static Map images; + + public Images() + { + images = new HashMap(); + + images.put("entrance", loadImage("images/entrance.png")); + images.put("wc", loadImage("images/wc.png")); + images.put("path", loadImage("images/path.jpg")); + images.put("stage", loadImage("images/stage4.png")); + images.put("wall", loadImage("images/wall.png")); + images.put("stage1", loadImage("images/stage3.png")); + images.put("visitor", loadImage("images/visitor.png")); + images.put("food", loadImage("images/food.png")); + } + + public static Image loadImage(String path) + { + return new ImageIcon(path).getImage(); + } + + public static Image getImage(String getter) + { + return images.get(getter); + } +} \ No newline at end of file diff --git a/Applicatie/MenuBar.java b/Applicatie/MenuBar.java index 343dbf9..0f172c1 100644 --- a/Applicatie/MenuBar.java +++ b/Applicatie/MenuBar.java @@ -6,10 +6,10 @@ import java.awt.event.ActionListener; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; public class MenuBar extends JMenuBar { + private static final long serialVersionUID = 1L; public MenuBar(Window w) { @@ -17,17 +17,6 @@ public class MenuBar extends JMenuBar JMenu file = new JMenu("File"); - JMenuItem newPath = new JMenuItem("New Path"); - newPath.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - w.getPanel().startPath(); - } - }); - - file.add(newPath); - JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { @@ -37,8 +26,39 @@ public class MenuBar extends JMenuBar } }); + JMenuItem save = new JMenuItem("Save"); + save.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + SaveLoad.save(w.getPanel()); + } + }); + + JMenuItem load = new JMenuItem("Load terrain"); + load.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + SaveLoad.load(w.getPanel()); + } + }); + + JMenuItem loadagenda = new JMenuItem("Load agenda"); + loadagenda.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + w.getPanel().getAgenda().loadAgenda(); + w.getPanel().addVisitors(); + } + }); + + file.add(load); + file.add(loadagenda); + file.add(save); file.add(exit); add(file); } -} +} \ No newline at end of file diff --git a/Applicatie/Panel.java b/Applicatie/Panel.java index 2b57635..43c4154 100644 --- a/Applicatie/Panel.java +++ b/Applicatie/Panel.java @@ -21,6 +21,7 @@ import java.util.Iterator; import javax.imageio.ImageIO; import javax.swing.JPanel; +import Agenda.Agenda; import Listeners.Mouse; import Listeners.MouseMotion; import Listeners.MouseWheel; @@ -44,6 +45,7 @@ public class Panel extends JPanel private ArrayList panelInfo = new ArrayList(); // private ArrayList panelTypes = new ArrayList(); + ArrayList visitors = new ArrayList<>(); ArrayList objects = new ArrayList<>(); ArrayList paths = new ArrayList<>(); @@ -54,24 +56,25 @@ public class Panel extends JPanel Point2D cameraPoint = new Point2D.Double(getWidth() / 2, getHeight() / 2); float cameraScale = 1; - PropertiesPanel pp; - + Agenda agenda; Point2D lastClickPosition = new Point(0, 0); Point lastMousePosition = new Point(0, 0); - Point2D selectionPosition = new Point(0,0); - + Point2D selectionPosition = new Point(0, 0); + Images images = new Images(); // how to nieuwe dingen aan het panel toe te voegen: // maak bufferedimage global aan, voeg er een image aan toe, en voeg de // image aan panelInfo toe en het object aan panelTypes. // en maak een case statement die een new Object returnt in // createNewDrawObject. - public Point2D getSelectionPosition() { + public Point2D getSelectionPosition() + { return selectionPosition; } - public void setSelectionPosition(Point2D selectionPosition) { + public void setSelectionPosition(Point2D selectionPosition) + { this.selectionPosition = selectionPosition; } @@ -88,7 +91,8 @@ public class Panel extends JPanel pathImage = ImageIO.read(new File("images/pathIcon.png")); wallImage = ImageIO.read(new File("images/wallIcon.png")); foodImage = ImageIO.read(new File("images/foodIcon.png")); - } catch (IOException e) + } + catch (IOException e) { e.printStackTrace(); } @@ -98,6 +102,8 @@ public class Panel extends JPanel panelInfo.add(pathImage); panelInfo.add(wallImage); panelInfo.add(foodImage); + + agenda = new Agenda(); panelInfox = 0; panelInfoy = 0; @@ -152,6 +158,11 @@ public class Panel extends JPanel objects.add(dragObject); } + public void addVisitors() + { + visitors.add(new Visitor("visitor", new Point(100, 300), agenda, objects)); + } + public void paintComponent(Graphics g) { super.paintComponent(g); @@ -178,8 +189,9 @@ public class Panel extends JPanel BasicStroke stroke = new BasicStroke(10); g2.setStroke(stroke); - - for(Path path : paths) { + + for (Path path : paths) + { path.draw(g2); } for (DrawObject o : objects) @@ -187,14 +199,15 @@ public class Panel extends JPanel o.draw(g2); } g2.setTransform(oldTransform); - if(currentPath != null) { //Display text while in path making modes. + if (currentPath != null) + { // Display text while in path making modes. g2.setColor(Color.WHITE); - g2.setFont(new Font("Verdana",Font.ITALIC,25)); - g2.drawString("Press enter to finish the path.",20,185); + g2.setFont(new Font("Verdana", Font.ITALIC, 25)); + g2.drawString("Press enter to finish the path.", 20, 185); } g2.setClip(null); - + } public AffineTransform getCamera() @@ -210,7 +223,8 @@ public class Panel extends JPanel try { return getCamera().inverseTransform(point, null); - } catch (NoninvertibleTransformException e1) + } + catch (NoninvertibleTransformException e1) { e1.printStackTrace(); } @@ -272,6 +286,11 @@ public class Panel extends JPanel this.lastClickPosition = lastClickPosition; } + public void clearObjects() + { + objects.clear(); + } + public Point getLastMousePosition() { return lastMousePosition; @@ -354,7 +373,9 @@ public class Panel extends JPanel /** * Remove a DrawObject from the list. - * @param o - the DrawObject you want to remove from the list. + * + * @param o + * - the DrawObject you want to remove from the list. */ public void removeObject(DrawObject o) { @@ -370,61 +391,77 @@ public class Panel extends JPanel repaint(); } } - - public void clearObjectSelection() { - for(DrawObject o : objects) { + + public void clearObjectSelection() + { + for (DrawObject o : objects) + { o.setSelected(false); } } - - public void checkCollision() { + + public void checkCollision() + { boolean collision = false; - for(DrawObject o : getObjects()) { - if(getSelectedObject().collision(o) && o != getSelectedObject()) { + for (DrawObject o : getObjects()) + { + if (getSelectedObject().collision(o) && o != getSelectedObject()) + { collision = true; getSelectedObject().setRectangleColor(Color.RED); break; } - if(!collision) + if (!collision) getSelectedObject().setRectangleColor(Color.BLACK); } } - + /** * Begin making a new path. */ - public void startPath() { + public void startPath() + { setClickedOption("Path"); currentPath = new Path(); paths.add(currentPath); pp.setSelectedPath(currentPath); } - + /** * Get the current selected Path. + * * @return - the selected Path. */ - public Path getCurrentPath() { + public Path getCurrentPath() + { return currentPath; } - + /** * Set the selected Path object. - * @param path - the selected Path object. + * + * @param path + * - the selected Path object. */ - public void setcurrentPath(Path path) { + public void setcurrentPath(Path path) + { currentPath = path; pp.setSelectedPath(currentPath); } - + /** * Checks if the point is contained in one of the path's. - * @param point - The point to check for. + * + * @param point + * - The point to check for. * @return if the point is contained in one of the path's. */ - public boolean checkPath(Point2D point) { - for(Path path : paths) { - if(path.containsPoint(point)) { + public boolean checkPath(Point2D point) + { + for (Path path : paths) + { + if (path.containsPoint(point)) + { setcurrentPath(path); setClickedOption("Path"); return true; @@ -432,15 +469,20 @@ public class Panel extends JPanel } return false; } - + /** * Remove a path from the list. - * @param path - the path you want to remove from the list. + * + * @param path + * - the path you want to remove from the list. */ - public void removePath(Path path) { + public void removePath(Path path) + { Iterator it = paths.iterator(); - while(it.hasNext()) { - if(it.next().equals(path)) { + while (it.hasNext()) + { + if (it.next().equals(path)) + { pp.clearSelected(); setClickedOption("drag"); currentPath = null; @@ -449,4 +491,14 @@ public class Panel extends JPanel repaint(); } } + + public Agenda getAgenda() + { + return agenda; + } + + public void setAgenda(Agenda agenda) + { + this.agenda = agenda; + } } diff --git a/Applicatie/SaveLoad.java b/Applicatie/SaveLoad.java new file mode 100644 index 0000000..a1e22c5 --- /dev/null +++ b/Applicatie/SaveLoad.java @@ -0,0 +1,144 @@ +package Applicatie; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; + +import Objects.DrawObject; + +public class SaveLoad { + + + public static void save(Panel panel) + { + JFileChooser fileChooser = new JFileChooser(); + + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + fileChooser.setDialogTitle("Choose save location"); + int userSelection = fileChooser.showSaveDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.getName().endsWith(".agn")) + { + file = new File(file.getAbsolutePath() + ".agn"); + } + + if(file.exists()) + { + if(JOptionPane.showConfirmDialog(null, "Are you sure you want to overwrite: " + file.getName(), "Overwrite", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) + { + saveTerrain(file, panel); + } + } + else + { + saveTerrain(file, panel); + } + } + } + + + public static void saveTerrain(File file, Panel panel) { + + try { + FileOutputStream fos = new FileOutputStream(file); + ObjectOutputStream oos = new ObjectOutputStream(fos); + for (DrawObject object : panel.objects) { + oos.writeObject(object); + } + oos.close(); + JOptionPane.showMessageDialog(null, "Saved succesfully"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void load(Panel panel) + { + JFileChooser fileChooser = new JFileChooser(); + fileChooser.setFileFilter(new FileFilter() { + @Override + public boolean accept(File pathname) { + if(pathname.isFile() && pathname.getName().endsWith(".agn")) + { + return true; + }else if(pathname.isDirectory()){return true;}else{return false;} + } + + @Override + public String getDescription() { + return ".agn"; + } + + }); + + fileChooser.setDialogTitle("Choose file"); + int userSelection = fileChooser.showOpenDialog(null); + + if(userSelection == JFileChooser.APPROVE_OPTION) + { + File file = fileChooser.getSelectedFile(); + if(!file.exists()) + { + JOptionPane.showMessageDialog(null, "This file does not exist: " + file.getName()); + } + else + { + fillTerrain(file, panel); + JOptionPane.showMessageDialog(null, "Loaded succesfully"); + } + } + } + + + public static void fillTerrain(File file, Panel panel) { + FileInputStream fis = null; + ObjectInputStream ois = null; + + try { + + fis = new FileInputStream(file); + ois = new ObjectInputStream(fis); + + Object object; + object = ois.readObject(); + try { + panel.clearObjects(); + while (object != null) { + panel.objects.add((DrawObject) object); + object = ois.readObject(); + } + } catch (EOFException e) { + } + } catch (Exception e) { + e.printStackTrace(); + } + panel.update(); + } + + + +} diff --git a/Applicatie/Visitor.java b/Applicatie/Visitor.java new file mode 100644 index 0000000..026d15f --- /dev/null +++ b/Applicatie/Visitor.java @@ -0,0 +1,219 @@ +package Applicatie; + +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Point; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.util.ArrayList; + +import Agenda.Agenda; +import Agenda.Event; +import Objects.DrawObject; + +public class Visitor +{ + + Point2D position; + double rotation; + double speed; + String filename; + ArrayList actions; + Agenda agenda; + ArrayList objects; + + public Visitor(String filename, Point2D position, Agenda agenda, ArrayList objects) + { + this.position = position; + this.filename = filename; + this.rotation = 0; + this.speed = 1 + Math.random() * 4; + actions = new ArrayList(); + this.agenda = agenda; + this.objects = objects; + fillActions(); + } + + public void draw(Graphics2D g2) + { + AffineTransform tx = getTransform(); + Image image = Images.getImage(filename); + g2.drawImage(image, tx, null); + } + + private AffineTransform getTransform() + { + AffineTransform tx = new AffineTransform(); + tx.translate(position.getX(), position.getY()); + Image image = Images.getImage(filename); + tx.rotate(rotation, image.getWidth(null) / 2, image.getHeight(null) / 2); + return tx; + } + + public void update(ArrayList objects, int currentTime, ArrayList visitors) + { + Point2D target = new Point(-100, -100); + for (Action a : actions) + { + if (currentTime >= a.getStartTime() && currentTime < a.getStoptime()) + { + target = a.getPosition(); + } + + } + moveToTarget(target, objects, visitors); + } + + public void moveToTarget(Point2D target, ArrayList objects, ArrayList visitors) + { + + double newRot = Math.atan2(target.getY() - position.getY(), target.getX() - position.getX()); + + int difx = (int) (target.getX() - position.getX()); + int dify = (int) (target.getY() - position.getY()); + int distance = (int) Math.sqrt((difx * difx) + (dify * dify)); + + if (rotation > newRot && distance > 10) + { + rotation -= 0.15; + } + else if (rotation < newRot && distance > 10) + { + rotation += 0.15; + } + + Point2D oldPosition = position; + + // face direction + float directionX = (float) Math.cos(rotation); + float directionY = (float) Math.sin(rotation); + + if (distance > 10) + { + position = new Point2D.Double((position.getX() + directionX * speed), (position.getY() + directionY * speed)); + } + + boolean possible = true; + for (DrawObject object : objects) + { + if (hitTest(object)) + { + possible = false; + } + } + for (Visitor object : visitors) + { + if (hitTestVisitor(object) && object != this) + { + possible = false; + } + } + if (possible == false) + { + position = oldPosition; + rotation += 0.2; + } + } + + public void fillActions() + { + int startTime = 540; + int stopTime = 1260; + + while (startTime < stopTime) + { + int random = (int) Math.floor(Math.random() * 51); + if (random < 30) + { + Point2D position = null; + for (Event e : agenda.getEvents()) + { + if (convertMinutesToHours(startTime) > e.getStart() && convertMinutesToHours(startTime) < e.getStop()) + { + for (DrawObject d : objects) + { + if (d.getFileName().equals(e.getStage().getName())) + { + position = d.getPosition(); + } + } + } + } + int randomtime = (int) (40 + (Math.random() * (60 - 40))); + if (position != null) + { + actions.add(new Action(position, startTime, randomtime)); + } + startTime = startTime + randomtime; + + } + else if (random >= 30 && random < 35) + { + Point2D position = null; + for (DrawObject d : objects) + { + if (d.getFileName().equals("entrance")) + { + position = d.getPosition(); + } + } + if (position != null) + { + actions.add(new Action(position, startTime, 35)); + startTime = startTime + 35; + } + startTime = startTime + 35; + + } + else if (random > 35) + { + Point2D position = null; + for (DrawObject d : objects) + { + if (d.getFileName().equals("wc")) + { + position = d.getPosition(); + } + } + if (position != null) + { + actions.add(new Action(position, startTime, 35)); + } + startTime = startTime + 35; + + } + } + } + + public int convertMinutesToHours(int startTime) + { + int time = 0; + int hours = startTime / 60; + int minutes = startTime % 60; + time = (hours * 100) + minutes; + return time; + } + + public int getEndX() + { + Image image = Images.getImage(filename); + return (int) (position.getX() + image.getWidth(null)); + } + + public int getEndY() + { + Image image = Images.getImage(filename); + return (int) (position.getY() + image.getHeight(null)); + } + + public boolean hitTest(DrawObject to2) + { + return (getEndX() >= to2.getPosition().getX() && getEndY() >= to2.getPosition().getY() && to2.getEndX() >= position.getX() && to2.getEndY() >= position.getY()); + + } + + public boolean hitTestVisitor(Visitor v) + { + return (getEndX() >= v.position.getX() && getEndY() >= v.position.getY() && v.getEndX() >= position.getX() && v.getEndY() >= position.getY()); + } +} \ No newline at end of file diff --git a/Objects/DrawObject.java b/Objects/DrawObject.java index 22e5c7c..9f33115 100644 --- a/Objects/DrawObject.java +++ b/Objects/DrawObject.java @@ -10,18 +10,22 @@ import java.awt.geom.Ellipse2D; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; +import java.io.Serializable; import javax.swing.ImageIcon; -public abstract class DrawObject +import Applicatie.Images; + +public abstract class DrawObject implements Serializable { Point2D position; double rotation; double scale; int width; int height; - private Image image; +// private Image image; protected boolean selected; + private String filename; protected int areaTop; protected int areaBottom; @@ -43,11 +47,12 @@ public abstract class DrawObject public DrawObject(String filename, Point2D position) { - image = new ImageIcon(filename).getImage(); +// image = new ImageIcon(filename).getImage(); + Image image = Images.getImage(filename); width = image.getWidth(null); height = image.getHeight(null); - + this.filename = filename; scale = 1; rotation = 0; this.position = position; @@ -64,6 +69,7 @@ public abstract class DrawObject public void draw(Graphics2D g) { AffineTransform tx = getTransform(); + Image image = Images.getImage(filename); g.drawImage(image, tx, null); if (selected) { @@ -156,31 +162,7 @@ public abstract class DrawObject } /** - * Method that gets all the corner - * coordinates index: 0 -> top left 1 -> top right 3 -> bottom left 4 -> - * bottom right - * - * @return corner coordinates - */ - public Point2D[] getCorners() - { - Rectangle2D tempRekt = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null)); - tempRekt.setFrame(getTransform().createTransformedShape(tempRekt).getBounds()); - Point2D[] points = new Point2D[4]; - Point2D point = new Point2D.Double(tempRekt.getX(), tempRekt.getY()); - points[0] = point; - point = new Point2D.Double(tempRekt.getX() + tempRekt.getHeight(), tempRekt.getY()); - points[1] = point; - point = new Point2D.Double(tempRekt.getX(), tempRekt.getY() + tempRekt.getHeight()); - points[2] = point; - point = new Point2D.Double(tempRekt.getX() + tempRekt.getHeight(), tempRekt.getY() + tempRekt.getHeight()); - points[3] = point; - return points; - } - - /** - * Checking - * collision. + * Checking collision. * * @param object * . the drawObject to check collision with. @@ -269,16 +251,6 @@ public abstract class DrawObject this.scale = scale; } - public Image getImage() - { - return image; - } - - public void setImage(Image image) - { - this.image = image; - } - public boolean isSelected() { return selected; @@ -306,6 +278,7 @@ public abstract class DrawObject public Rectangle2D getImageRectangle() { + Image image = Images.getImage(filename); return new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null)); } @@ -349,4 +322,21 @@ public abstract class DrawObject this.areaRight = areaRight; } + public int getEndX() + { + Image image = Images.getImage(filename); + return (int) (position.getX() + image.getWidth(null)); + } + + public int getEndY() + { + Image image = Images.getImage(filename); + return (int) (position.getY() + image.getHeight(null)); + } + + public String getFileName() + { + return filename; + } + } diff --git a/Objects/Entrance.java b/Objects/Entrance.java index eab28db..f4de61b 100644 --- a/Objects/Entrance.java +++ b/Objects/Entrance.java @@ -7,7 +7,7 @@ public class Entrance extends DrawObject public Entrance(Point2D position) { - super("images/entrance.png", position); + super("entrance", position); } @Override diff --git a/Objects/Food.java b/Objects/Food.java index d6bdb65..3ef3973 100644 --- a/Objects/Food.java +++ b/Objects/Food.java @@ -5,7 +5,7 @@ import java.awt.geom.Point2D; public class Food extends DrawObject { public Food(Point2D position) { - super("images/food.png", position); + super("food", position); } public String getName() { diff --git a/Objects/OldPath.java b/Objects/OldPath.java index 3acdf12..c966ee4 100644 --- a/Objects/OldPath.java +++ b/Objects/OldPath.java @@ -7,7 +7,7 @@ public class OldPath extends DrawObject public OldPath(Point2D position) { - super("images/path.jpg", position); + super("path", position); } @Override diff --git a/Objects/Stage.java b/Objects/Stage.java index 2c3b82c..c006d71 100644 --- a/Objects/Stage.java +++ b/Objects/Stage.java @@ -6,7 +6,7 @@ public class Stage extends DrawObject { public Stage(Point2D position) { - super("images/stage4.png", position); + super("stage", position); } @Override diff --git a/Objects/Toilet.java b/Objects/Toilet.java index 3a8708a..f15000f 100644 --- a/Objects/Toilet.java +++ b/Objects/Toilet.java @@ -7,7 +7,7 @@ public class Toilet extends DrawObject public Toilet(Point2D position) { - super("images/wc.png", position); + super("wc", position); } @Override diff --git a/Objects/Wall.java b/Objects/Wall.java index e79b3c1..a1341f1 100644 --- a/Objects/Wall.java +++ b/Objects/Wall.java @@ -7,7 +7,7 @@ public class Wall extends DrawObject public Wall(Point2D position) { - super("images/wall.png", position); + super("wall", position); } @Override