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) {
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)
{
w.setVisible(false);
w.dispose();
System.exit(0);
}
});
+1 -1
View File
@@ -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();
+64 -41
View File
@@ -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<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);
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)
{
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;
}
}
}
+70 -42
View File
@@ -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<Point2D> points;
private ArrayList<Shape> 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<Shape> getPath() {
public ArrayList<Shape> 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<Point2D> getPoints() {
public ArrayList<Point2D> 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;
}
}
}