乒乓球游戏:墙壁和球几乎完全一样 - 如何超类 Ellipse2D 和 Rectangle2D?
Pong Game: Walls and Balls almost exactly the same - How to superclass Ellipse2D and Rectangle2D?
我处于学习的早期阶段 Java,我只是想制作一个简单的 Pong 类游戏。我有 2 classes Wall 和 Ball。它们几乎是相同的,除了它们每个都有一个不同的 java.awt.geom sublcass(一个有 Ellipse2d 和一个 Rectangle2D)但在几乎所有其他方面它们都是相同的(The Ball class 将有更多处理速度的变量,但其他方面相同)。
我的问题是现在我看到有冗余我想制作一个超级class游戏对象。我只是不确定如何处理这两个 subclasses 指的是 Ellipse2D 和 Rectangle 2D
class墙{
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();
}
}
这是我的球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();
}
}
如有任何建议,我们将不胜感激。提前致谢。
在公共超类中存储 RectangularShape
而不是 Ellipse2D
/ Rectangle2D
?您仍然可以在相应的构造函数中分配正确的子类...
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);
}
我处于学习的早期阶段 Java,我只是想制作一个简单的 Pong 类游戏。我有 2 classes Wall 和 Ball。它们几乎是相同的,除了它们每个都有一个不同的 java.awt.geom sublcass(一个有 Ellipse2d 和一个 Rectangle2D)但在几乎所有其他方面它们都是相同的(The Ball class 将有更多处理速度的变量,但其他方面相同)。
我的问题是现在我看到有冗余我想制作一个超级class游戏对象。我只是不确定如何处理这两个 subclasses 指的是 Ellipse2D 和 Rectangle 2D
class墙{
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();
}
}
这是我的球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();
}
}
如有任何建议,我们将不胜感激。提前致谢。
在公共超类中存储 RectangularShape
而不是 Ellipse2D
/ Rectangle2D
?您仍然可以在相应的构造函数中分配正确的子类...
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);
}