package net.jaitec.p1;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
public class Ball extends BufferedImage{
private static int sequence = 1;
private int number;
private int x;
private int y;
private int xDest;
private int yDest;
private double rotation;
private int radius;
private int speed;
private Color color;
private double angle;
private boolean pass;
private MotorBall.AnimateThread animateThread;
public Ball(Color color, int radius)
{
super(radius*2, radius*2, BufferedImage.TYPE_INT_ARGB);
Graphics g = this.getGraphics();
// contorno del sprite, modelo de caja
if(Config.DEBUG)
{
g.setColor(Color.gray);
g.drawRect(0, 0, 2*radius-1, 2*radius-1);
}
// relleno de la bola
g.setColor(color);
g.fillArc(0, 0, 2*radius-1, 2*radius-1, 0, 360);
// contorno
g.setColor(new Color(0,0,0,80));
g.drawArc(0, 0, 2*radius-1, 2*radius-1, 0, 360);
// brillo
g.setColor(new Color(250,250,250,80));
int width = (int) (radius/2);
int x = radius + (int) (radius / 4);
int y = radius - (int) (2 * radius / 3);
g.fillArc(x, y, width, width, 0, 360);
// marquita con el centro de la bola
g.setColor(new Color(0,0,0,80));
g.drawLine(radius - 5, radius, radius + 5, radius);
g.drawLine(radius, radius - 5, radius, radius + 5);
// numero de bola
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 14));
FontMetrics f = g.getFontMetrics();
String str = ""+sequence;
int w = f.stringWidth(str);
int h = f.getHeight();
g.drawString(str, radius - (int) (w/2), radius - (int) (h/2));
//--- guardar datos en la instancia
this.radius = radius;
this.color = color;
this.speed = 5;
number = sequence;
// incremento el valor de sequence para la siguiente instancia
sequence++;
}
public boolean isPass() {
return pass;
}
public void setPass(boolean pass) {
this.pass = pass;
}
public int getxDest() {
return xDest;
}
public void setxDest(int xDest) {
this.xDest = xDest;
}
public int getyDest() {
return yDest;
}
public void setyDest(int yDest) {
this.yDest = yDest;
}
public int getNumber() {
return number;
}
public Point getPos()
{
return new Point(x,y);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setCoords(int x, int y){
this.x = x;
this.y = y;
}
public void setCoords(Point p){
this.x = p.x;
this.y = p.y;
}
public void setCoordsDest(int x, int y){
this.xDest = x;
this.yDest = y;
}
public void setCoordsDest(Point p){
if(p!=null)
{
this.xDest = p.x;
this.yDest = p.y;
}
}
public int getCenterX(){
return x+radius;
}
public int getCenterY(){
return y+radius;
}
public double getRotation() {
return rotation;
}
public void setRotation(double rotation) {
this.rotation = rotation;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getAngle() {
/*double inRads = Math.atan2(yDest-y, xDest-x);
if (inRads < 0)
inRads = Math.abs(inRads);
else
inRads = 2*Math.PI - inRads;
return inRads;*/
return angle;
}
public void setAngle(double angle) {
if(angle<0) angle=Math.PI*2+angle;
this.angle = angle;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public MotorBall.AnimateThread getAnimateThread() {
return animateThread;
}
public void setAnimateThread(MotorBall.AnimateThread animateThread) {
this.animateThread = animateThread;
}
public void draw(Graphics2D graphic) {
graphic.rotate(rotation);
graphic.drawImage(this, x, y, null);
}
public int distance(Ball ball){
Point center1 = new Point(x+radius,y+radius);
Point center2 = new Point(ball.getCenterX(),ball.getCenterY());
return Utils.distance(center1, center2);
}
}