瀏覽代碼

Rename, rotate collision detected.

Gilian Joosen 10 年之前
父節點
當前提交
6419952701

+ 12 - 14
src/gui/simulator/AccessPoint.java

@@ -1,8 +1,6 @@
 package gui.simulator;
 
-import gui.frames.MainFrame;
-
-public class AccessPoint extends Draw{
+public class AccessPoint extends DrawEngine {
 	
 	private static String imageLocation = "/simulator/ingang.png";
 	private static String facilityName = "Ingang";
@@ -28,15 +26,15 @@ public class AccessPoint extends Draw{
     	double oldPosY = this.getY();
     	if(x<0){
     		this.setRotation(90);
-    		this.x = 0; 
+    		this.setX(0);
     	} else if(x>9*terrain.getWidth()/10){
-    		this.x = 9*terrain.getWidth()/10;
+    		this.setX(9*terrain.getWidth()/10);
     		this.setRotation(90);
-    	} else if(x>0 && x<9*terrain.getWidth()/10 && this.y > 50 && this.y < terrain.getFestivalHeight()-150){
-    		this.x = oldPosX;
-    		this.y = oldPosY;
+    	} else if(x>0 && x<9*terrain.getWidth()/10 && this.getY() > 50 && this.getY() < terrain.getFestivalHeight()-150){
+    		this.setX(oldPosX);
+    		this.setY(oldPosY);
     	} else {
-    		this.x = x;
+    		this.setX(x);
     	}
     }
 
@@ -45,14 +43,14 @@ public class AccessPoint extends Draw{
     	double oldPosY = this.getY();
     	if(y<50){
     		this.setRotation(0);
-    		this.y = 50;     
+    		this.setY(50);
     	} else if(y>11*terrain.getFestivalHeight()/12){
     		this.setRotation(0);
-    		this.y = 11*terrain.getFestivalHeight()/12; 	
-    	} else if(x>terrain.getWidth()/10 && x<9*terrain.getWidth()/10 && this.y > 50 && this.y < terrain.getFestivalHeight()-150){
-    		this.y = oldPosY;
+    		this.setY(11*terrain.getFestivalHeight()/12);
+    	} else if(this.getX()>terrain.getWidth()/10 && this.getX() <9*terrain.getWidth()/10 && this.getY()> 50 && this.getY() < terrain.getFestivalHeight()-150){
+    		this.setY(oldPosY);
     	} else {
-    		this.y = y;
+    		this.setY(y);
     	}
     }
 }

+ 5 - 3
src/gui/simulator/Draw.java → src/gui/simulator/DrawEngine.java

@@ -12,13 +12,13 @@ import java.io.IOException;
  * Created by gjoosen on 07/03/15.
  * Edited by master D Mathijssen on 17-3-2015 and some other days
  */
-public abstract class Draw {
+public abstract class DrawEngine {
 
     private Image image;
-    public double x, y, rotation, scale;
+    private double x, y, rotation, scale;
     private double distanceToOtherObjects;
 
-    public Draw(String image, int x, int y, double scale, double distanceToOtherObjects){
+    public DrawEngine(String image, int x, int y, double scale, double distanceToOtherObjects){
         try{
             this.image = ImageIO.read(getClass().getResource(image));
         }catch(IOException ex){
@@ -98,4 +98,6 @@ public abstract class Draw {
     public Rectangle2D.Double getRect(){
         return new Rectangle2D.Double(-distanceToOtherObjects, - distanceToOtherObjects, image.getWidth(null)  + 2* distanceToOtherObjects, image.getHeight(null) + 2*distanceToOtherObjects);
     }
+
+
 }

+ 12 - 12
src/gui/simulator/ExitPoint.java

@@ -1,6 +1,6 @@
 package gui.simulator;
 
-public class ExitPoint extends Draw{
+public class ExitPoint extends DrawEngine {
 	
 	private static String imageLocation = "/simulator/uitgang.png";
 	private static String facilityName = "Uitgang";
@@ -22,15 +22,15 @@ public class ExitPoint extends Draw{
     	double oldPosY = this.getY();
     	if(x<0){
     		this.setRotation(90);
-    		this.x = 0; 
+    		this.setX(0);
     	} else if(x>9*terrain.getWidth()/10){
-    		this.x = 9*terrain.getWidth()/10;
+    		this.setX(9*terrain.getWidth()/10);
     		this.setRotation(90);
-    	} else if(x>0 && x<9*terrain.getWidth()/10 && this.y > 50 && this.y < terrain.getFestivalHeight()-150){
-    		this.x = oldPosX;
-    		this.y = oldPosY;
+    	} else if(x>0 && x<9*terrain.getWidth()/10 && this.getY() > 50 && this.getY() < terrain.getFestivalHeight()-150){
+			this.setX(oldPosX);
+			this.setY(oldPosY);
     	} else {
-    		this.x = x;
+			this.setX(x);
     	}
     }
 
@@ -39,14 +39,14 @@ public class ExitPoint extends Draw{
     	double oldPosY = this.getY();
     	if(y<50){
     		this.setRotation(0);
-    		this.y = 50;     
+    		this.setY(50);
     	} else if(y>11*terrain.getFestivalHeight()/12){
     		this.setRotation(0);
-    		this.y = 11*terrain.getFestivalHeight()/12; 	
-    	} else if(x>terrain.getWidth()/10 && x<9*terrain.getWidth()/10 && this.y > 50 && this.y < terrain.getFestivalHeight()-150){
-    		this.y = oldPosY;
+    		this.setY(11*terrain.getFestivalHeight()/12);
+    	} else if(this.getX()>terrain.getWidth()/10 && this.getX()<9*terrain.getWidth()/10 && this.getY() > 50 && this.getY() < terrain.getFestivalHeight()-150){
+    		this.setY(oldPosY);
     	} else {
-    		this.y = y;
+    		this.setY(y);
     	}
     }
 }

+ 6 - 6
src/gui/simulator/Sidebar.java

@@ -16,7 +16,7 @@ public class Sidebar {
 	private int sideBarWidth;
 	private int sideBarHeight;
 	private Terrain terrain;
-	private ArrayList<Draw> drawableFacilities = new ArrayList<Draw>();
+	private ArrayList<DrawEngine> drawableFacilities = new ArrayList<DrawEngine>();
 	
 	/*
 	 * Constructor for initializing all variables
@@ -46,23 +46,23 @@ public class Sidebar {
         g2.drawLine(199, 0, 199, sideBarHeight);
         
         //draw objects
-        Draw object1 = new Stage(20,100, 0.4, 10);
+        DrawEngine object1 = new Stage(20,100, 0.4, 10);
         object1.draw(g2);
         g2.drawString("stage", 80, 275);
-        Draw object2 = new RestRoom(60,350, 1 , 10);
+        DrawEngine object2 = new RestRoom(60,350, 1 , 10);
         object2.draw(g2);
         g2.drawString("toilettage", 70, 500);
-        Draw object3 = new AccessPoint(terrain, 0,550, 0.5, 10);
+        DrawEngine object3 = new AccessPoint(terrain, 0,550, 0.5, 10);
         object3.draw(g2);
         g2.drawString("ingang", 70, 675);
-        Draw object4 = new ExitPoint(terrain, 0,700, 0.5, 10);
+        DrawEngine object4 = new ExitPoint(terrain, 0,700, 0.5, 10);
         object4.draw(g2);
         g2.drawString("uitgang", 70, 800);
     }
 	
     private void getFacility(MouseEvent e){
     	Point2D clickPoint = terrain.getClickPoint(e.getPoint());
-    	Draw dragObject = null;
+    	DrawEngine dragObject = null;
     	if(e.getX() < sideBarWidth)
 		{
 			if(e.getY() < 300){

+ 21 - 17
src/gui/simulator/Terrain.java

@@ -24,7 +24,6 @@ import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
@@ -35,9 +34,10 @@ import javax.swing.SwingUtilities;
 
 public class Terrain extends JPanel {
 
-    private List<Draw> entities;
+    private List<DrawEngine> entities;
     private Point2D oldPosition;
-    private Draw dragObject, selectedObject;
+	private double oldRotation, oldScale;
+    private DrawEngine dragObject, selectedObject;
     private BufferedImage background;
     private Sidebar sidebar;
     
@@ -132,7 +132,7 @@ public class Terrain extends JPanel {
 				getFacility(e);
                 //else selected in terrain
                 if(e.getX() > sideBarWidth){
-	                for(Draw drawObject: entities){
+	                for(DrawEngine drawObject: entities){
 	                    if(drawObject.contains(clickPoint)){
 	                    	if(pathgenerate  == 1){
 	                    		currentpath = new WalkingPath();
@@ -150,6 +150,8 @@ public class Terrain extends JPanel {
 	                    		dragObject = drawObject;
 	                    		selectedObject = dragObject;	
 	                    		oldPosition = new Point2D.Double(selectedObject.getX(), selectedObject.getY());
+								oldRotation = selectedObject.getScale();
+								oldScale = selectedObject.getScale();
 	                    		return;
 	                    	}
 	                    }
@@ -164,7 +166,7 @@ public class Terrain extends JPanel {
             @Override
             public void mouseReleased(MouseEvent e) {
                 if(selectedObject != null){
-                    for(Draw drawObject: entities){
+                    for(DrawEngine drawObject: entities){
                         if(drawObject == selectedObject){
                             continue;
                         }
@@ -172,6 +174,8 @@ public class Terrain extends JPanel {
                             //move back
                             selectedObject.setX(oldPosition.getX());
                             selectedObject.setY(oldPosition.getY());
+							selectedObject.setRotation(oldRotation);
+							selectedObject.setScale(oldScale);
                         }
                     }
                 }
@@ -193,7 +197,7 @@ public class Terrain extends JPanel {
                     		dragObject.setX(dragObject.getX() - (lastClickPosition.getX() - clickPoint.getX()));
                     		dragObject.setY(dragObject.getY() - (lastClickPosition.getY() - clickPoint.getY()));
                     }else if(SwingUtilities.isRightMouseButton(e)){
-                    	dragObject.rotation += (lastClickPosition.getX() - clickPoint.getX() + lastClickPosition.getY() - clickPoint.getY());
+						dragObject.setRotation(dragObject.getRotation() + (lastClickPosition.getX() - clickPoint.getX() + lastClickPosition.getY() - clickPoint.getY()));
                     }
                 }
                 else
@@ -213,10 +217,10 @@ public class Terrain extends JPanel {
             @Override
             public void mouseWheelMoved(MouseWheelEvent e) {
             	Point2D clickPoint = getClickPoint(e.getPoint());
-                for(Draw object: entities){
+                for(DrawEngine object: entities){
                 	//if object selected, scale object
                     if(object.contains(clickPoint)){
-                    	object.scale *= 1 + (e.getPreciseWheelRotation()/10.0);
+						object.setScale(object.getScale() * 1 + (e.getPreciseWheelRotation()/10.0));
 						repaint();
 						return;
                     }
@@ -265,7 +269,7 @@ public class Terrain extends JPanel {
     	for(Visitor v:visitors){
     		v.paint(g2);
     	}
-		for(Draw drawObject: entities){
+		for(DrawEngine drawObject: entities){
             drawObject.draw(g2); 
             if(pathgenerate > 0){
             	g2.fill(drawObject.getAffineTransform().createTransformedShape(new Rectangle((int)(drawObject.getX()-(drawObject.getWidth()/2)-10),(int)(drawObject.getY()-(drawObject.getHeight()/2)-10), 20, 20)));
@@ -289,9 +293,9 @@ public class Terrain extends JPanel {
             g2.draw(this.selectedObject.getAffineTransform().createTransformedShape(shape));
 
             boolean collide = false;
-            for(Draw draw: entities){
-                if(!draw.equals(this.selectedObject)){
-                    if(intersection(this.selectedObject, draw)){
+            for(DrawEngine drawEngine : entities){
+                if(!drawEngine.equals(this.selectedObject)){
+                    if(intersection(this.selectedObject, drawEngine)){
                         collide = true;
                     }
                 }
@@ -308,13 +312,13 @@ public class Terrain extends JPanel {
         }
     }
 
-    private Rectangle drawDragRectangle(Draw shape){
+    private Rectangle drawDragRectangle(DrawEngine shape){
         Rectangle rectangle = null;
         rectangle = new Rectangle((int) (shape.getX() - shape.getDistanceToOtherObjects()), (int) (shape.getY() - shape.getDistanceToOtherObjects()), (int) (shape.getWidth() * shape.getScale() + (2 * shape.getDistanceToOtherObjects())), (int) (shape.getHeight() * shape.getScale() + (2 * shape.getDistanceToOtherObjects())));
         return rectangle;
     }
 
-    private boolean intersection(Draw d1, Draw d2){
+    private boolean intersection(DrawEngine d1, DrawEngine d2){
         Area area = new Area(d1.getAffineTransform().createTransformedShape(d1.getRect()));
         Area area2 = new Area(d2.getAffineTransform().createTransformedShape(d2.getRect()));
 
@@ -359,15 +363,15 @@ public class Terrain extends JPanel {
     }
     
     //getters and setters for sidebar
-    public void setDragObject(Draw dragObject){
+    public void setDragObject(DrawEngine dragObject){
     	this.dragObject = dragObject;
     }
     
-    public Draw getDragObject(){
+    public DrawEngine getDragObject(){
     	return dragObject;
     }
     
-    public void addEntity(Draw dragObject){
+    public void addEntity(DrawEngine dragObject){
     	entities.add(dragObject);
     }
 }

+ 5 - 5
src/gui/simulator/WalkingPath.java

@@ -14,7 +14,7 @@ import javax.imageio.ImageIO;
 
 public class WalkingPath {
 	private ArrayList<Point> path;
-	private Draw object1,object2;
+	private DrawEngine object1,object2;
 	private BufferedImage texture;
 	
 	public WalkingPath(){
@@ -37,16 +37,16 @@ public class WalkingPath {
 	public void setPath(ArrayList<Point> path) {
 		this.path = path;
 	}
-	public Draw getObject1() {
+	public DrawEngine getObject1() {
 		return object1;
 	}
-	public void setObject1(Draw object1) {
+	public void setObject1(DrawEngine object1) {
 		this.object1 = object1;
 	}
-	public Draw getObject2() {
+	public DrawEngine getObject2() {
 		return object2;
 	}
-	public void setObject2(Draw object2) {
+	public void setObject2(DrawEngine object2) {
 		this.object2 = object2;
 	}
 	public void reCalculate(){

+ 2 - 7
src/gui/simulator/facilities/RestRoom.java

@@ -1,13 +1,8 @@
 package gui.simulator.facilities;
 
-import gui.simulator.Draw;
+import gui.simulator.DrawEngine;
 
-import javax.imageio.ImageIO;
-import java.awt.*;
-import java.io.File;
-import java.io.IOException;
-
-public class RestRoom extends Draw{
+public class RestRoom extends DrawEngine {
 
 	private static String imageLocation = "/simulator/toilet.png";
 	private static String facilityName = "Plee";

+ 2 - 7
src/gui/simulator/facilities/Stage.java

@@ -1,13 +1,8 @@
 package gui.simulator.facilities;
 
-import gui.simulator.Draw;
+import gui.simulator.DrawEngine;
 
-import javax.imageio.ImageIO;
-import java.awt.*;
-import java.io.File;
-import java.io.IOException;
-
-public class Stage extends Draw{
+public class Stage extends DrawEngine {
 
 	private static String imageLocation = "/simulator/tent.png";
 	private static String facilityName = "Stage";