| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package gui.simulator;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.Rectangle2D;
- import java.io.IOException;
- /**
- * Created by gjoosen on 07/03/15.
- */
- public abstract class Draw {
- private Image image;
- private double x, y, rotation, scale;
- private double distanceToOtherObjects;
- public Draw(String image, int x, int y, double scale, double distanceToOtherObjects){
- try{
- //System.out.println(getClass().getResource(image));
- this.image = ImageIO.read(getClass().getResource(image));
- }catch(IOException ex){
- ex.printStackTrace();
- }
- this.x = x;
- this.y = y;
- this.scale = scale;
- this.distanceToOtherObjects = distanceToOtherObjects;
- }
- public AffineTransform getAffineTransform(){
- AffineTransform transform = new AffineTransform();
- transform.translate(this.x, this.y);
- transform.scale(this.scale, this.scale);
- transform.rotate(Math.toRadians(this.rotation), this.image.getWidth(null) /2 , this.image.getHeight(null) / 2);
- return transform;
- }
- public void draw(Graphics2D g){
- g.drawImage(this.image, this.getAffineTransform(), null);
- }
- public boolean contains(Point point){
- Shape shape = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));
- return this.getAffineTransform().createTransformedShape(shape).contains(point);
- }
- public double getX() {
- return x;
- }
- public double getY() {
- return y;
- }
- public void setX(double x){
- this.x = x;
- }
- public void setY(double y){
- this.y = y;
- }
- public void setRotation(double rotation) {
- this.rotation = rotation;
- }
- public double getScale() {
- return scale;
- }
- public void setScale(double scale) {
- this.scale = scale;
- }
- public int getWidth(){
- return this.image.getWidth(null);
- }
- public int getHeight(){
- return this.image.getHeight(null);
- }
- public double getRotation() {
- return rotation;
- }
- public double getDistanceToOtherObjects() {
- return distanceToOtherObjects;
- }
- }
|