I'm in the early stages of learning Java, and I'm just trying to make a simple Pong type game. I've got 2 classes Wall and Ball. They are almost identical, except that they each have a different java.awt.geom sublcass in them (One had Ellipse2d and one Rectangle2D) but in almost all other ways they are identical (The Ball class will have a few more variables dealing with velocity, but otherwise the same).
My problem is that now that I see that there is redundancy I would like to make a superclass GameObject. I'm just not sure how to deal with the fact that the two subclasses refer to Ellipse2D and Rectangle 2D
class Wall {
private double posX, posY, width, height; // wall position and dimensions
Rectangle2D wall2D;
/**
*
*/
public Wall(double Xin, double Yin, double widthIn, double heightIn) { // passes in variables and creates a new Ellipse2D
posX = Xin;
posY = Yin;
width = widthIn;
height = heightIn;
wall2D = new Rectangle2D.Double(posX, posY, width, height);
}
private void setWall2D() { // makes the Wall2D have the same location/dimensions as the variables in this class
wall2D.setFrame(posX, posY, width, height);
}
Rectangle2D getWall2D() { // returns the actual Rectangle2D so it can get displayed on the screen when needed
return wall2D;
}
// getters
double getPositionX() {
return posX;
}
double getPositionY() {
return posY;
}
double getHeight() {
return height;
}
double getWidth() {
return width;
}
// setters (all have to call setWall2D after variable has been set! - to update the actual Rectangle2D shape)
void setPositionX(double xIn) {
posX = xIn;
setWall2D();
}
void setPositionY(double yIn) {
posY = yIn;
setWall2D();
}
void setHeight(double heightIn) {
height = heightIn;
setWall2D();
}
void setwidth(double widthIn) {
width = widthIn;
setWall2D();
}
}
And here is my ball class
class Ball {
private double posX, posY, width, height; // ball position and dimensions
Ellipse2D ball2D;
/**
*
*/
public Ball(double Xin, double Yin, double widthIn, double heightIn) { // passes in variables and creates a new Ellipse2D
posX = Xin;
posY = Yin;
width = widthIn;
height = heightIn;
ball2D = new Ellipse2D.Double(posX, posY, width, height);
}
private void setBall2D() { // makes the Ball2D have the same location/dimensions as the variables in this class
ball2D.setFrame(posX, posY, width, height);
}
Ellipse2D getBall2D() { // returns the actual Ellipse2D so it can get displayed on the screen when needed
return ball2D;
}
// getters
double getPositionX() {
return posX;
}
double getPositionY() {
return posY;
}
double getHeight() {
return height;
}
double getWidth() {
return width;
}
// setters (all have to call setBall2D after variable has been set! - to update the actual Ellipse2D shape)
void setPositionX(double xIn) {
posX = xIn;
setBall2D();
}
void setPositionY(double yIn) {
posY = yIn;
setBall2D();
}
void setHeight(double heightIn) {
height = heightIn;
setBall2D();
}
void setwidth(double widthIn) {
width = widthIn;
setBall2D();
}
}
Any suggestions would be appreciated. Thanks in advance.
Store RectangularShape
in the common superclass instead of Ellipse2D
/ Rectangle2D
? You can still assign the right subclasses in the corresponding constructors...
class GameObject {
RectangularShape shape;
...
}
class Ball extends GameObject {
public Ball(double Xin, double Yin, double widthIn, double heightIn) {
// passes in variables and creates a new Ellipse2D
posX = Xin;
posY = Yin;
width = widthIn;
height = heightIn;
shape = new Ellipse2D.Double(posX, posY, width, height);
}