如何将食物对象(椭圆形)放入ArrayList中,以便在与其他鱼类物体碰撞时将其移除?我查了一些在线教程,但我不确定如何调用我食品类中的鱼的半径" /大小来测量碰撞检测的距离。 / p>
这是我食物类的代码:
import java.awt.Color;
import java.awt.Graphics2D;
import processing.core.PVector;
import java.util.ArrayList;
public class Food {
private int size;
private PVector pos, speed;
private Color color;
private double scale;
public Food (int x, int y, int size, Color c) {
this.size = size;
this.pos = new PVector (x,y);
this.color = c;
}
public void draw(Graphics2D g) {
g.setColor(color);
g.fillOval((int)pos.x, (int)pos.y, 15, 15);
}
这是我的鱼类代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import processing.core.PVector;
public class Fish {
private static final int PI = 0;
private static final Polygon Polygon = null;
private int size;
private PVector pos, speed;
private Color color;
private double scale;
private double scaleone;
Food [] foodList = new Food[10];
public Fish(int x, int y, int size, int speedx, int speedy, Color c) {
this.pos = new PVector(x,y);
this.size = size;
this.speed = new PVector(speedx,speedy);
this.color = c;
this.scale = 2;// give value to the scale variable from the top;
this.scaleone = 1;
}
public void draw(Graphics2D g) {
AffineTransform af = g.getTransform();
g.translate((int)pos.x, (int)pos.y);
g.rotate(speed.heading());
g.scale(scale,scale);
if (speed.x < 0) g.scale(1, -1);
int [] xp = {-20, -50,-50};
int [] yp= {-15,-25,-5};
//DRAW TAIL
g.setColor(Color.RED);
Polygon p = new Polygon();
p.addPoint(xp[0], yp[0]);
p.addPoint(xp[1], yp[1]);
p.addPoint(xp[2], yp[2]);
g.fillPolygon(p);
//drawBody
g.setColor(color);
g.fillOval(-size/2, -size/2, 45, 30);
//eye
g.setColor(Color.BLACK);
g.fillOval(0, -size/3, size/10, size/10);
g.setTransform(af); // this is popMatrix();
}
public void move() {
pos.add(speed);
}
public void checkCollision(Dimension panelSize) {
if ((pos.x < size/2 * scale) || (pos.x > panelSize.width - size/2 * scale)) speed.x *= -1;
if ((pos.y < size/2 * scale) || (pos.y > panelSize.height - size/2 * scale)) speed.y *= -1;
}
}