Package net.jaitec.balls

Source Code of net.jaitec.balls.Ball

package net.jaitec.balls;

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.geom.Point2D;
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 radius;
  private int speed;
  private double angle;
 
  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.speed = 5;
    number = sequence;
    // incremento el valor de sequence para la siguiente instancia
    sequence++;
  }

 
  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 int getCenterX(){
    return x+radius;
  }
 
  public int getCenterY(){
    return y+radius;
  }
 
  public int getRadius() {
    return radius;
  }

  public double getAngle() {
    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 void draw(Graphics2D graphic) {
    graphic.drawImage(this, x, y, null);
  }
 
  public int distance(Ball ball){
    Point2D center1 = new Point(x+radius,y+radius);
    Point2D center2 = new Point(ball.getCenterX(),ball.getCenterY());
    return (int) center1.distance(center2);
  }
}
TOP

Related Classes of net.jaitec.balls.Ball

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.