DrawEngine.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package gui.simulator;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Point2D;
  6. import java.awt.geom.Rectangle2D;
  7. import java.io.IOException;
  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. public DrawEngine(Images.ImageType image, int x, int y, double scale, double distanceToOtherObjects, SimulatorPane.Objects objecttype){
  18. this.image = image;
  19. this.type = objecttype;
  20. this.x = x;
  21. this.y = y;
  22. this.scale = scale;
  23. this.distanceToOtherObjects = distanceToOtherObjects;
  24. }
  25. public AffineTransform getAffineTransform(){
  26. AffineTransform transform = new AffineTransform();
  27. transform.translate(this.x, this.y);
  28. transform.scale(this.scale, this.scale);
  29. transform.rotate(Math.toRadians(this.rotation), Images.getImage(image).getWidth(null) /2 , Images.getImage(image).getHeight(null) / 2);
  30. return transform;
  31. }
  32. public void draw(Graphics2D g){
  33. g.drawImage(Images.getImage(image), this.getAffineTransform(), null);
  34. }
  35. public boolean contains(Point2D point){
  36. Shape shape = new Rectangle2D.Double(0, 0, Images.getImage(image).getWidth(null),Images.getImage(image).getHeight(null));
  37. return this.getAffineTransform().createTransformedShape(shape).contains(point);
  38. }
  39. public Image getImage(){
  40. return Images.getImage(image);
  41. }
  42. public Point2D.Double getCenter(){
  43. Point2D.Double center = new Point2D.Double(x + Images.getImage(image).getWidth(null)*scale / 2, y + Images.getImage(image).getHeight(null)*scale/2);
  44. return center;
  45. }
  46. //getters and setters down here
  47. public double getX() {
  48. return x;
  49. }
  50. public double getY() {
  51. return y;
  52. }
  53. public void setX(double x){
  54. this.x = x;
  55. }
  56. public void setY(double y){
  57. this.y = y;
  58. }
  59. public void setRotation(double rotation) {
  60. this.rotation = rotation;
  61. }
  62. public double getScale() {
  63. return scale;
  64. }
  65. public void setScale(double scale) {
  66. this.scale = scale;
  67. }
  68. public int getWidth(){
  69. return Images.getImage(image).getWidth(null);
  70. }
  71. public int getHeight(){
  72. return Images.getImage(image).getHeight(null);
  73. }
  74. public double getRotation() {
  75. return rotation;
  76. }
  77. public double getDistanceToOtherObjects() {
  78. return distanceToOtherObjects;
  79. }
  80. public Rectangle2D.Double getRect(){
  81. return new Rectangle2D.Double(-distanceToOtherObjects, - distanceToOtherObjects, Images.getImage(image).getWidth(null) + 2* distanceToOtherObjects, Images.getImage(image).getHeight(null) + 2*distanceToOtherObjects);
  82. }
  83. }