Java 在框架上移动形状
基本上我的整个程序就是让形状在一个框架上移动, 我现在的问题是我不能让我的物体移动 我有一个叫做“生物”的类,构造函数创建了我的鱼的身体和尾巴。 (我正在做一个水族馆)。该类扩展到另一个具有move()方法的类,因此每当发生计时器事件时,图片都会移动 我的问题是,我无法使图片/形状/对象移动。 如果有人能伸出援手,我将不胜感激 这是完整的生物种类Java 在框架上移动形状,java,swing,graphics,Java,Swing,Graphics,基本上我的整个程序就是让形状在一个框架上移动, 我现在的问题是我不能让我的物体移动 我有一个叫做“生物”的类,构造函数创建了我的鱼的身体和尾巴。 (我正在做一个水族馆)。该类扩展到另一个具有move()方法的类,因此每当发生计时器事件时,图片都会移动 我的问题是,我无法使图片/形状/对象移动。 如果有人能伸出援手,我将不胜感激 这是完整的生物种类 import java.awt.geom.Ellipse2D; import java.awt.Color; import java.awt.Poly
import java.awt.geom.Ellipse2D;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Graphics2D;
public class Creatures extends Moveable2DShape{
private Color color;
private int delay, number;
private int xPos2, yPos2;
private final int POINTS = 3;
private int []x = new int[POINTS];
private int []y = new int[POINTS];
private double width, height;
Ellipse2D.Double body;
Polygon tail;
public Creatures(int xPos, int yPos, int shapeWidth, int shapeHeight, int bwidth, int bheight, double xSpeed, double ySpeed, Color c){
super(xPos, yPos, shapeWidth, shapeHeight, bwidth, bheight, xSpeed, ySpeed);
color = c;
xPos2 = xPos;
yPos2 = yPos;
width = shapeWidth;
height = shapeHeight;
}
public Polygon triangle(int xPos, int yPos){
x[0] = xPos + 100;
x[1] = x[0];
x[2] = (int)(xPos + width/2);
y[0] = (int)(yPos + (height));
y[1] = yPos;
y[2] = (int)(yPos + height/2);
tail = new Polygon(x,y,POINTS);
return tail;
}
//public void move(){
}
public void draw(Graphics2D g){
body = new Ellipse2D.Double(xPos2, yPos2, width, height);
//System.out.println(xPos2 + " " + yPos2 + " " + width + " " + height);
triangle(xPos2, yPos2);
g.setColor(color);
g.fill(body);
g.fill(tail);
}
}
我应该重写从Moveabled2DShape类继承的move()方法,但我还没有重写,因为我只是想在重写之前测试该方法。但现在我的问题是,我需要更新xPos2和yPos2,我的biates类的完整解决方案
import java.awt.geom.Ellipse2D;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Graphics2D;
public class Creatures extends Moveable2DShape{
private Color color;
private int delay, number, t;
private int xPos, yPos;
private double xSpeed, ySpeed;
private final int POINTS = 3;
private int []x = new int[POINTS];
private int []y = new int[POINTS];
private double width, height;
private boolean dead;
Ellipse2D.Double body;
Polygon tail;
public Creatures(int xPos, int yPos, int shapeWidth, int shapeHeight, int bwidth, int bheight, double xSpeed, double ySpeed, Color c, int type, boolean l){
super(xPos, yPos, shapeWidth, shapeHeight, bwidth, bheight, xSpeed, ySpeed);
color = c;
this.xPos = xPos;
this.yPos = yPos;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
width = shapeWidth;
height = shapeHeight;
t = type;
dead = l;
}
/*@x[3] calculate the x position points of the tail
*@y[3] calculate the y position points of the tail
*@tail create the polygon using the points and a defined constant of 3
*@return the polygon @tail*/
public Polygon triangle(int xP, int yP){
x[0] = xP + 100;
x[1] = x[0];
x[2] = (int)(xP + width/2);
y[0] = (int)(yP + (height));
y[1] = yP;
y[2] = (int)(yP + height/2);
tail = new Polygon(x,y,POINTS);
return tail;
}
/*@newBW will determine the new width of the bounding box
*@return the new bounding box width*/
public int newBWidth(){
int newBW;
newBW = (int)(x[0] - getBoundingBoxMaxX()) + (int)getBoundingBoxWidth();
return newBW;
}
/*when called, the move method of Creatures will move along the x-values\
*set the new bounding box with the new calculated width */
public void move(){
this.xPos=(int) (getXPos() - xSpeed);
int overShoot = (int) (xPos + getBoundingBoxWidth() - environmentWidth);
if (xPos <= 0)
{
xPos = 0;
xSpeed = -xSpeed ;
}
else if (overShoot > 0)
{
xPos = (int)(environmentWidth - getBoundingBoxWidth());
xSpeed = - xSpeed ;
}
/*
Reflect the object if it goes too far
*/
overShoot = (int) (yPos + getBoundingBoxHeight() - environmentHeight);
if (yPos <= 0)
{
yPos = 0;
ySpeed = -ySpeed ;
}
else if (overShoot > 0)
{
yPos = (int) (environmentHeight - getBoundingBoxHeight());
ySpeed = - ySpeed ;
}
if(xPos == 0 || xPos == 880){
this.yPos =+ (int)(getYPos() + ySpeed);
}
if(yPos == 650){
this.yPos =+ (int)(getYPos() + ySpeed);
}
// move the shape to the new position
moveTo(xPos,yPos);
setBoundingBox(getXPos() , getYPos() , newBWidth(), height);
}
/*Detect if two bounding boxes intersects with one another if two intersects
*call the collision response method and @return true, else @return false. */
public boolean collisionDetect(Moveable2DShape other){
if(getBoundingBox().intersects(other.getBoundingBox())){
return true;
}
return false;
}
/*@other2 cast the Moveable2DShape object into a Creatures object and compare
*if @other2 object type is greater than the current object then the current object dies
*if the two objects are equal to each other then change the color of both objects randomly
*otherwise @other2 dies. */
public void collisionResponse(Moveable2DShape other){
int []c2 = new int[3];
int []c3 = new int[3];
Creatures other2 = (Creatures) other;
if (getType() < other2.getType()){
fishiesDie();
}else if(getType() == other2.getType()){
for(int a = 0; a < 3; a++){
c2[a] = (int)(Math.random()*255) + 1;
c3[a] = (int)(Math.random()*255) + 1;
}
color = new Color(c2[0], c2[1], c2[2]);
other2.color = new Color(c3[0], c3[1], c3[2]);
}else{
other2.fishiesDie();
}
}
/*@return the type of the fish */
public int getType(){
return t;
}
/*@dead equals to true when called */
public void fishiesDie(){
dead = true;
}
/*@return dead */
public boolean isDead(){
return dead;
}
/*@body draw the general body ellipse
*@g set the colour and draw the general fish shape(body and tail)*/
public void draw(Graphics2D g){
body = new Ellipse2D.Double((int)getXPos(), (int)getYPos() , width, height);
triangle((int)getXPos(), (int)getYPos());
//g.draw(getBoundingBox());
g.setColor(color);
g.fill(body);
g.fill(tail);
}
}
导入java.awt.geom.Ellipse2D;
导入java.awt.Rectangle;
导入java.awt.Color;
导入java.awt.Polygon;
导入java.awt.Graphics2D;
公共类生物扩展了Moveable2DShape{
私人色彩;
专用整数延迟,数字,t;
私人int XPO、YPO;
私人双X速度,Y速度;
私人最终积分=3;
私有整数[]x=新整数[点数];
私有整数[]y=新整数[点数];
私人双宽,高;
私人布尔死亡;
椭圆形2d。双体;
多边形尾;
公共生物(int xPos、int yPos、int shapeWidth、int shapeHeight、int bwidth、int bheight、double xSpeed、double ySpeed、Color c、int type、boolean l){
超级(XPO、YPO、形宽、形高、宽、宽、宽、X速、Y速);
颜色=c;
this.xPos=xPos;
this.yPos=yPos;
this.xSpeed=xSpeed;
this.ySpeed=ySpeed;
宽度=形状宽度;
高度=形高;
t=类型;
死亡=l;
}
/*@x[3]计算尾部的x位置点
*@y[3]计算尾部的y位置点
*@使用点和定义的常数3创建多边形
*@返回多边形@tail*/
公共多边形三角形(int xP,int yP){
x[0]=xP+100;
x[1]=x[0];
x[2]=(int)(xP+width/2);
y[0]=(int)(yP+(高度));
y[1]=yP;
y[2]=(int)(yP+高度/2);
尾部=新多边形(x、y、点);
返回尾;
}
/*@newBW将确定边界框的新宽度
*@返回新的边界框宽度*/
公共宽度(){
int NEBW;
newBW=(int)(x[0]-getBoundingBoxMaxX())+(int)getBoundingBoxWidth();
返回新窗口;
}
/*调用时,生物的移动方法将沿x值移动\
*使用新的计算宽度设置新的边界框*/
公开作废动议(){
this.xPos=(int)(getXPos()-xSpeed);
int超调=(int)(xPos+getBoundingBoxWidth()-environmentWidth);
如果(xPos 0)
{
xPos=(int)(environmentWidth-getBoundingBoxWidth());
xSpeed=-xSpeed;
}
/*
如果物体走得太远,反射它
*/
超调=(int)(yPos+getBoundingBoxHeight()-environmentHeight);
如果(yPos 0)
{
yPos=(int)(environmentHeight-getBoundingBoxHeight());
ySpeed=-ySpeed;
}
如果(xPos==0 | | xPos==880){
this.yPos=+(int)(getYPos()+ySpeed);
}
如果(yPos==650){
this.yPos=+(int)(getYPos()+ySpeed);
}
//将形状移动到新位置
移动到(XPO、YPO);
setBoundingBox(getXPos(),getYPos(),newBWidth(),height);
}
/*如果两个边界框相交,则检测两个边界框是否相交
*调用冲突响应方法并@return true,否则@return false*/
公共布尔冲突检测(Moveable2DShape其他){
如果(getBoundingBox().与(其他.getBoundingBox())相交){
返回true;
}
返回false;
}
/*@other2将Moveable2DShape对象强制转换为一个生物对象并进行比较
*如果@other2对象类型大于当前对象,则当前对象死亡
*如果两个对象彼此相等,则随机更改两个对象的颜色
*否则@other2就会死亡*/
公共无效冲突响应(可移动2DShape其他){
int[]c2=新的int[3];
int[]c3=新的int[3];
其他生物2=(生物)其他;
if(getType()
我认为我们需要更多的信息才能以任何有意义的方式帮助您。您不会显示任何代码表明这是问题的根源。您是否在代码中添加了调试语句?关于我们需要什么的更多建议,请查看:生物是否源自某些JComponent?@MeBigFatGuy我有一个