halverwege

This commit is contained in:
Yorick
2015-04-07 19:32:10 +02:00
parent d6b511bab5
commit 32de28204c
5 changed files with 138 additions and 87 deletions
+2 -1
View File
@@ -82,7 +82,8 @@ public class MenuBar extends JMenuBar {
public void actionPerformed(ActionEvent e) { 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) 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();
} }
} }
}); });
+1 -2
View File
@@ -65,8 +65,7 @@ public class MenuBar extends JMenuBar
{ {
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
{ {
w.setVisible(false); System.exit(0);
w.dispose();
} }
}); });
+1 -1
View File
@@ -542,7 +542,7 @@ public class Panel extends JPanel implements ActionListener
for (Visitor v : visitors) for (Visitor v : visitors)
{ {
v.update(objects, currentTime, visitors); v.update(objects, currentTime, visitors, paths);
} }
repaint(); repaint();
+64 -41
View File
@@ -10,6 +10,7 @@ import java.util.ArrayList;
import Agenda.Agenda; import Agenda.Agenda;
import Agenda.Event; import Agenda.Event;
import Objects.DrawObject; import Objects.DrawObject;
import Objects.Path;
public class Visitor public class Visitor
{ {
@@ -49,12 +50,12 @@ public class Visitor
AffineTransform tx = new AffineTransform(); AffineTransform tx = new AffineTransform();
tx.scale(scale, scale); tx.scale(scale, scale);
tx.translate(position.getX(), position.getY()); 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); tx.rotate(rotation, image.getWidth(null) / 2, image.getHeight(null) / 2);
return tx; return tx;
} }
public void update(ArrayList<DrawObject> objects, int currentTime, ArrayList<Visitor> visitors) public void update(ArrayList<DrawObject> objects, int currentTime, ArrayList<Visitor> visitors, ArrayList<Path> paths)
{ {
Point2D target = new Point(500, 500); Point2D target = new Point(500, 500);
for (Action a : actions) 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<DrawObject> objects, ArrayList<Visitor> visitors) public void moveToTarget(Point2D target, ArrayList<DrawObject> objects, ArrayList<Visitor> visitors, ArrayList<Path> paths)
{ {
boolean hasPathBelow = false;
double newRot = Math.atan2(target.getY() - position.getY(), target.getX() - position.getX()); for (Path p : paths)
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; System.out.println(position);
} System.out.println(p.getPoints());
else if (rotation < newRot && distance > 10) if (p.containsPoint(position))
{
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; 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; for(Path p : paths)
rotation += 0.2; {
// System.out.println(p.getPoints());
break;
}
} }
} }
+70 -42
View File
@@ -16,103 +16,131 @@ import javax.imageio.ImageIO;
/** /**
* Create's a path. * Create's a path.
*
* @author Wesley de Hek * @author Wesley de Hek
* @version 1.2 * @version 1.2
*/ */
public class Path { public class Path
{
private ArrayList<Point2D> points; private ArrayList<Point2D> points;
private ArrayList<Shape> lines; private ArrayList<Shape> lines;
private Point2D tempPoint; private Point2D tempPoint;
private BufferedImage pathBackground; private BufferedImage pathBackground;
/** /**
* Constructor. * Constructor.
*/ */
public Path() { public Path()
{
points = new ArrayList<>(); points = new ArrayList<>();
lines = new ArrayList<>(); lines = new ArrayList<>();
try { try
{
pathBackground = ImageIO.read(new File("images/newPath.png")); pathBackground = ImageIO.read(new File("images/newPath.png"));
} catch (IOException e) { }
catch (IOException e)
{
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* Draws the path. * Draws the path.
* @param g2 - the Graphics2D object. *
* @param g2
* - the Graphics2D object.
*/ */
public void draw(Graphics2D g2) { public void draw(Graphics2D g2)
TexturePaint paint = new TexturePaint(pathBackground,new Rectangle2D.Double(0,0,128,128)); {
TexturePaint paint = new TexturePaint(pathBackground, new Rectangle2D.Double(0, 0, 128, 128));
g2.setPaint(paint); g2.setPaint(paint);
g2.setStroke(new BasicStroke(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); g2.setStroke(new BasicStroke(30, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
if(!points.isEmpty()) { if (!points.isEmpty())
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); 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: // Drawing line that follows the mouse:
if(tempPoint != null) { if (tempPoint != null)
Point2D lastPoint = points.get(points.size()-1); {
g2.drawLine((int) lastPoint.getX(),(int) lastPoint.getY(),(int) tempPoint.getX(),(int) tempPoint.getY()); 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. * 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. * @return if the path contains the point.
*/ */
public boolean containsPoint(Point2D point) { public boolean containsPoint(Point2D point)
for(Shape line : getPath()) { {
if(line.contains(point)) { for (Shape line : getPath())
{
if (line.contains(point))
{
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Get array with line's from the points. * Get array with line's from the points.
*
* @return a array with all the lines of the path. * @return a array with all the lines of the path.
*/ */
public ArrayList<Shape> getPath() { public ArrayList<Shape> getPath()
{
lines.clear(); lines.clear();
for(int i = 1; i < points.size(); i++) { for (int i = 1; i < points.size(); i++)
Point2D previousPoint = points.get(i-1); {
Point2D previousPoint = points.get(i - 1);
Point2D point = points.get(i); Point2D point = points.get(i);
Line2D line = new Line2D.Double((int) previousPoint.getX(),(int) previousPoint.getY(),(int) point.getX(),(int) point.getY()); 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); BasicStroke stroke = new BasicStroke(30, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
lines.add(stroke.createStrokedShape(line)); lines.add(stroke.createStrokedShape(line));
} }
return lines; return lines;
} }
/** /**
* Add a point to the path. * 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); points.add(point);
} }
/** /**
* Get all the path points. * Get all the path points.
*
* @return all the points of the path. * @return all the points of the path.
*/ */
public ArrayList<Point2D> getPoints() { public ArrayList<Point2D> getPoints()
{
return points; return points;
} }
/** /**
* Set the temporary point, useful for following the mouse while drawing the path. * Set the temporary point, useful for following the mouse while drawing the
* @param point - the point of the mouse. * path.
*
* @param point
* - the point of the mouse.
*/ */
public void setTempPoint(Point2D point) { public void setTempPoint(Point2D point)
{
tempPoint = point; tempPoint = point;
} }
} }