From 32de28204cbc4ab3f896d3442ba412be3374e8ef Mon Sep 17 00:00:00 2001 From: Yorick Date: Tue, 7 Apr 2015 19:32:10 +0200 Subject: [PATCH] halverwege --- Agenda/MenuBar.java | 3 +- Applicatie/MenuBar.java | 3 +- Applicatie/Panel.java | 2 +- Applicatie/Visitor.java | 105 ++++++++++++++++++++++--------------- Objects/Path.java | 112 +++++++++++++++++++++++++--------------- 5 files changed, 138 insertions(+), 87 deletions(-) diff --git a/Agenda/MenuBar.java b/Agenda/MenuBar.java index 6448933..84f451f 100644 --- a/Agenda/MenuBar.java +++ b/Agenda/MenuBar.java @@ -82,7 +82,8 @@ public class MenuBar extends JMenuBar { public void actionPerformed(ActionEvent e) { if(JOptionPane.showConfirmDialog(null, "Are you sure you want to close this program?", "Close Agenda", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION) { - System.exit(0); + w.setVisible(false); + w.dispose(); } } }); diff --git a/Applicatie/MenuBar.java b/Applicatie/MenuBar.java index 81a69d4..4bff43f 100644 --- a/Applicatie/MenuBar.java +++ b/Applicatie/MenuBar.java @@ -65,8 +65,7 @@ public class MenuBar extends JMenuBar { public void actionPerformed(ActionEvent e) { - w.setVisible(false); - w.dispose(); + System.exit(0); } }); diff --git a/Applicatie/Panel.java b/Applicatie/Panel.java index 255b77f..86621d7 100644 --- a/Applicatie/Panel.java +++ b/Applicatie/Panel.java @@ -542,7 +542,7 @@ public class Panel extends JPanel implements ActionListener for (Visitor v : visitors) { - v.update(objects, currentTime, visitors); + v.update(objects, currentTime, visitors, paths); } repaint(); diff --git a/Applicatie/Visitor.java b/Applicatie/Visitor.java index e592ca7..2ddaf9f 100644 --- a/Applicatie/Visitor.java +++ b/Applicatie/Visitor.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import Agenda.Agenda; import Agenda.Event; import Objects.DrawObject; +import Objects.Path; public class Visitor { @@ -49,12 +50,12 @@ public class Visitor AffineTransform tx = new AffineTransform(); tx.scale(scale, scale); tx.translate(position.getX(), position.getY()); -// System.out.println(position.getX()); + // System.out.println(position.getX()); tx.rotate(rotation, image.getWidth(null) / 2, image.getHeight(null) / 2); return tx; } - public void update(ArrayList objects, int currentTime, ArrayList visitors) + public void update(ArrayList objects, int currentTime, ArrayList visitors, ArrayList paths) { Point2D target = new Point(500, 500); for (Action a : actions) @@ -65,57 +66,79 @@ public class Visitor } } - moveToTarget(target, objects, visitors); + moveToTarget(target, objects, visitors, paths); } - public void moveToTarget(Point2D target, ArrayList objects, ArrayList visitors) + public void moveToTarget(Point2D target, ArrayList objects, ArrayList visitors, ArrayList paths) { - - 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) + boolean hasPathBelow = false; + for (Path p : paths) { - 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)) + System.out.println(position); + System.out.println(p.getPoints()); + if (p.containsPoint(position)) { - possible = false; + hasPathBelow = true; + break; } } - for (Visitor object : visitors) + + if (hasPathBelow) { - if (hitTestVisitor(object) && object != this) + 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) { - possible = false; + 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; } } - if (possible == false) + else { - position = oldPosition; - rotation += 0.2; + for(Path p : paths) + { +// System.out.println(p.getPoints()); + break; + } } } diff --git a/Objects/Path.java b/Objects/Path.java index 67cbe12..72d1f1c 100644 --- a/Objects/Path.java +++ b/Objects/Path.java @@ -16,103 +16,131 @@ import javax.imageio.ImageIO; /** * Create's a path. + * * @author Wesley de Hek * @version 1.2 */ -public class Path { - +public class Path +{ private ArrayList points; private ArrayList lines; private Point2D tempPoint; private BufferedImage pathBackground; - + /** * Constructor. */ - public Path() { + public Path() + { points = new ArrayList<>(); lines = new ArrayList<>(); - try { + try + { pathBackground = ImageIO.read(new File("images/newPath.png")); - } catch (IOException e) { + } + catch (IOException e) + { e.printStackTrace(); } } - + /** * Draws the path. - * @param g2 - the Graphics2D object. + * + * @param g2 + * - the Graphics2D object. */ - public void draw(Graphics2D g2) { - TexturePaint paint = new TexturePaint(pathBackground,new Rectangle2D.Double(0,0,128,128)); + public void draw(Graphics2D g2) + { + TexturePaint paint = new TexturePaint(pathBackground, new Rectangle2D.Double(0, 0, 128, 128)); g2.setPaint(paint); - g2.setStroke(new BasicStroke(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); - if(!points.isEmpty()) { - for(int i = 1; i < points.size(); i++) { - Point2D previousPoint = points.get(i-1); + g2.setStroke(new BasicStroke(30, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); + if (!points.isEmpty()) + { + for (int i = 1; i < points.size(); i++) + { + Point2D previousPoint = points.get(i - 1); Point2D point = points.get(i); - g2.drawLine((int) previousPoint.getX(),(int) previousPoint.getY(),(int) point.getX(),(int) point.getY()); + g2.drawLine((int) previousPoint.getX(), (int) previousPoint.getY(), (int) point.getX(), (int) point.getY()); } - //Drawing line that follows the mouse: - if(tempPoint != null) { - Point2D lastPoint = points.get(points.size()-1); - g2.drawLine((int) lastPoint.getX(),(int) lastPoint.getY(),(int) tempPoint.getX(),(int) tempPoint.getY()); + // Drawing line that follows the mouse: + if (tempPoint != null) + { + Point2D lastPoint = points.get(points.size() - 1); + g2.drawLine((int) lastPoint.getX(), (int) lastPoint.getY(), (int) tempPoint.getX(), (int) tempPoint.getY()); } } } - + /** * Check if one of the lines contains the given point. - * @param point - the point you want to containment with. + * + * @param point + * - the point you want to containment with. * @return if the path contains the point. */ - public boolean containsPoint(Point2D point) { - for(Shape line : getPath()) { - if(line.contains(point)) { + public boolean containsPoint(Point2D point) + { + for (Shape line : getPath()) + { + if (line.contains(point)) + { return true; } } return false; } - + /** * Get array with line's from the points. + * * @return a array with all the lines of the path. */ - public ArrayList getPath() { + public ArrayList getPath() + { lines.clear(); - for(int i = 1; i < points.size(); i++) { - Point2D previousPoint = points.get(i-1); + for (int i = 1; i < points.size(); i++) + { + Point2D previousPoint = points.get(i - 1); Point2D point = points.get(i); - Line2D line = new Line2D.Double((int) previousPoint.getX(),(int) previousPoint.getY(),(int) point.getX(),(int) point.getY()); - BasicStroke stroke = new BasicStroke(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND); + Line2D line = new Line2D.Double((int) previousPoint.getX(), (int) previousPoint.getY(), (int) point.getX(), (int) point.getY()); + BasicStroke stroke = new BasicStroke(30, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); lines.add(stroke.createStrokedShape(line)); } - return lines; + return lines; } - + /** * Add a point to the path. - * @param point - the point you want the path to pass. + * + * @param point + * - the point you want the path to pass. */ - public void addPoint(Point2D point) { + public void addPoint(Point2D point) + { points.add(point); } - + /** * Get all the path points. + * * @return all the points of the path. */ - public ArrayList getPoints() { + public ArrayList getPoints() + { return points; - } - + } + /** - * Set the temporary point, useful for following the mouse while drawing the path. - * @param point - the point of the mouse. + * Set the temporary point, useful for following the mouse while drawing the + * path. + * + * @param point + * - the point of the mouse. */ - public void setTempPoint(Point2D point) { + public void setTempPoint(Point2D point) + { tempPoint = point; - } + } } \ No newline at end of file