DrawEngine.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package gui.simulator;
  2. import gui.simulator.Terrain.Location;
  3. import java.awt.*;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Point2D;
  6. import java.awt.geom.Rectangle2D;
  7. /**
  8. *
  9. * Created by gjoosen on 07/03/15.
  10. * Edited by master D Mathijssen on 17-3-2015 and some other days
  11. */
  12. public abstract class DrawEngine {
  13. private Images.ImageType image;
  14. private double x, y, rotation, scale;
  15. private double distanceToOtherObjects;
  16. public SimulatorPane.Objects type;
  17. private Terrain terrain;
  18. private boolean drawWalkArea;
  19. private Rectangle2D.Double walkArea;
  20. public DrawEngine(){
  21. }
  22. public DrawEngine(Images.ImageType image, int x, int y, double scale, double distanceToOtherObjects, SimulatorPane.Objects objecttype, Terrain terrain, Rectangle2D.Double walkArea, boolean drawWalkArea){
  23. this.image = image;
  24. this.type = objecttype;
  25. this.x = x;
  26. this.y = y;
  27. this.scale = scale;
  28. this.distanceToOtherObjects = distanceToOtherObjects;
  29. this.terrain = terrain;
  30. this.walkArea = walkArea;
  31. this.drawWalkArea = drawWalkArea;
  32. }
  33. public AffineTransform getAffineTransform(){
  34. AffineTransform transform = new AffineTransform();
  35. transform.translate(this.x, this.y);
  36. transform.scale(this.scale, this.scale);
  37. transform.rotate(Math.toRadians(this.rotation), Images.getImage(image).getWidth(null) /2 , Images.getImage(image).getHeight(null) / 2);
  38. return transform;
  39. }
  40. public void draw(Graphics2D g) {
  41. g.drawImage(Images.getImage(image), this.getAffineTransform(), null);
  42. if(this.drawWalkArea){
  43. g.setColor(new Color(0, 255, 0, 100));
  44. g.fill(getAffineTransform().createTransformedShape(this.walkArea));
  45. }
  46. }
  47. public boolean contains(Point2D point){
  48. Shape shape = new Rectangle2D.Double(-distanceToOtherObjects, -distanceToOtherObjects, Images.getImage(image).getWidth(null)+2*distanceToOtherObjects,Images.getImage(image).getHeight(null)+2*distanceToOtherObjects);
  49. return this.getAffineTransform().createTransformedShape(shape).contains(point);
  50. }
  51. public boolean containsWalkArea(Point2D point){
  52. if(this.walkArea == null){
  53. return false;
  54. }
  55. return this.getAffineTransform().createTransformedShape(this.walkArea).contains(point);
  56. }
  57. public Image getImage(){
  58. return Images.getImage(image);
  59. }
  60. public Point2D.Double getCenter(){
  61. Point2D.Double center = new Point2D.Double(x + Images.getImage(image).getWidth(null)*scale / 2, y + Images.getImage(image).getHeight(null)*scale/2);
  62. return center;
  63. }
  64. //getters and setters down here
  65. public double getX() {
  66. return x;
  67. }
  68. public double getY() {
  69. return y;
  70. }
  71. public void setX(double x){
  72. this.x = x;
  73. }
  74. public void setY(double y){
  75. this.y = y;
  76. }
  77. public void setRotation(double rotation) {
  78. this.rotation = rotation;
  79. }
  80. public double getScale() {
  81. return scale;
  82. }
  83. public void setScale(double scale) {
  84. this.scale = scale;
  85. }
  86. public int getWidth(){
  87. return Images.getImage(image).getWidth(null);
  88. }
  89. public int getHeight(){
  90. return Images.getImage(image).getHeight(null);
  91. }
  92. public double getRotation() {
  93. return rotation;
  94. }
  95. public double getDistanceToOtherObjects() {
  96. return distanceToOtherObjects;
  97. }
  98. public Rectangle2D.Double getRect(){
  99. return new Rectangle2D.Double(-distanceToOtherObjects, - distanceToOtherObjects, Images.getImage(image).getWidth(null) + 2* distanceToOtherObjects, Images.getImage(image).getHeight(null) + 2*distanceToOtherObjects);
  100. }
  101. public void remove(){
  102. this.terrain.getEntities().remove(this);
  103. this.terrain.removeWalkingPaths(this);
  104. }
  105. public abstract String getFacilityName();
  106. public void changeLocation(Location loc){
  107. switch(loc){
  108. case NORTH:
  109. //Visitors spawn north
  110. case EAST:
  111. //Visitors spawn east
  112. case SOUTH:
  113. //Visitors spawn south
  114. case WEST:
  115. //Visitors spawn west
  116. default:
  117. break;
  118. }
  119. }
  120. }