Draw.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package gui.simulator;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.geom.Rectangle2D;
  6. import java.io.IOException;
  7. /**
  8. * Created by gjoosen on 07/03/15.
  9. */
  10. public abstract class Draw {
  11. private Image image;
  12. private double x, y, rotation, scale;
  13. private double distanceToOtherObjects;
  14. public Draw(String image, int x, int y, double scale, double distanceToOtherObjects){
  15. try{
  16. //System.out.println(getClass().getResource(image));
  17. this.image = ImageIO.read(getClass().getResource(image));
  18. }catch(IOException ex){
  19. ex.printStackTrace();
  20. }
  21. this.x = x;
  22. this.y = y;
  23. this.scale = scale;
  24. this.distanceToOtherObjects = distanceToOtherObjects;
  25. }
  26. public AffineTransform getAffineTransform(){
  27. AffineTransform transform = new AffineTransform();
  28. transform.translate(this.x, this.y);
  29. transform.scale(this.scale, this.scale);
  30. transform.rotate(Math.toRadians(this.rotation), this.image.getWidth(null) /2 , this.image.getHeight(null) / 2);
  31. return transform;
  32. }
  33. public void draw(Graphics2D g){
  34. g.drawImage(this.image, this.getAffineTransform(), null);
  35. }
  36. public boolean contains(Point point){
  37. Shape shape = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));
  38. return this.getAffineTransform().createTransformedShape(shape).contains(point);
  39. }
  40. public double getX() {
  41. return x;
  42. }
  43. public double getY() {
  44. return y;
  45. }
  46. public void setX(double x){
  47. this.x = x;
  48. }
  49. public void setY(double y){
  50. this.y = y;
  51. }
  52. public void setRotation(double rotation) {
  53. this.rotation = rotation;
  54. }
  55. public double getScale() {
  56. return scale;
  57. }
  58. public void setScale(double scale) {
  59. this.scale = scale;
  60. }
  61. public int getWidth(){
  62. return this.image.getWidth(null);
  63. }
  64. public int getHeight(){
  65. return this.image.getHeight(null);
  66. }
  67. public double getRotation() {
  68. return rotation;
  69. }
  70. public double getDistanceToOtherObjects() {
  71. return distanceToOtherObjects;
  72. }
  73. }