Question: package hw 4 ; import java.awt.Rectangle; import api.AbstractElement; / * * * An attached element is one that is associated with another base element *

package hw4;
import java.awt.Rectangle;
import api.AbstractElement;
/**
* An attached element is one that is associated with another "base" element
* such as a PlatformElement or a LiftElement. Specifically, the attached
* element's movement is determined by the movement of the base element, the
* element always remains a fixed distance away.
*
* @author YOUR NAME HERE
*/
//TODO: This class must directly or indirectly extend AbstractElement
public class AttachedElement extends PlatformElement{
private int frameCount;
private int height;
private int width;
private double y;
private double x;
private AbstractElement baseElement;
private double newY;
private double offset;
private double hover;
private double newX;
/**
* Constructs a new AttachedElement. Before being added to an associated "base"
* element such as a PlatformElement or LiftElement, the x and y coordinates are
* initialized to zero. When the base object is set (not in this constructor),
* the x-coordinate will be calculated as the base object's x-coordinate, plus
* the given offset, and the y-coordinate will become the base object's
* y-coordinate, minus this element's height, minus the hover amount.
*
* @param width element's width
* @param height element's height
* @param offset when added to a base object, this amount will be added to the
* other object's x-coordinate to calculate this element's
* x-coordinate
* @param hover when added to a base object, this element's y-coordinate is the
* other object's y-coordinate, minus this element's height, minus
* the hover amount
*/
public AttachedElement(int width, int height, int offset, int hover){
super(width, height, offset, hover);
this.offset =0;
this.hover =0;
this.x =0;
this.y =0;
this.newX =0;
this.newY =0;
}
@Override
public boolean collides(AbstractElement other){
return (this.x < other.getXInt()+ other.getWidth() &&
this.x + this.width > other.getXInt() &&
this.y < other.getYInt()+ other.getHeight() &&
this.y + this.height > other.getYInt());
}
@Override
public int getFrameCount(){
return frameCount;
}
@Override
public int getHeight(){
return height;
}
@Override
public int getWidth(){
return width;
}
@Override
public java.awt.Rectangle getRect(){
return new Rectangle((int) Math.round(x),(int) Math.round(y), width, height);
}
@Override
public int getXInt(){
return (int) Math.round(x);
}
@Override
public int getYInt(){
return (int) Math.round(y);
}
@Override
public double getXReal(){
return x;
}
@Override
public double getYReal(){
return y;
}
@Override
public boolean isMarked(){
return markedForDeletion;
}
@Override
public void markForDeletion(){
markedForDeletion = true;
}
public void setBase(AbstractElement b){
baseElement = b;
}
@Override
public void setPosition(double newX, double newY){
this.newX = newX;
this.newY = newY;
}
/**
* Constructs a new AttachedElement. Before being added to an associated "base"
* element such as a PlatformElement or LiftElement, the x and y coordinates are
* initialized to zero. When the base object is set (not in this constructor),
* the x-coordinate will be calculated as the base object's x-coordinate, plus
* the given offset, and the y-coordinate will become the base object's
* y-coordinate, minus this element's height, minus the hover amount.
*
* @param width element's width
* @param height element's height
* @param offset when added to a base object, this amount will be added to the
* other object's x-coordinate to calculate this element's
* x-coordinate
* @param hover when added to a base object, this element's y-coordinate is the
* other object's y-coordinate, minus this element's height, minus
* the hover amount
*/
/**
* Updates this element's position to remain stationary
* relative to the parent element (provided that a parent has been set).
*/
@Override
public void update(){
frameCount++;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!