import java.awt.*;
public class interactingletters extends fallingletterrefreshes //has all the methods inherited from fallingletterrefreshes
{
public static final Font FONT= new Font("Serif", Font.BOLD, 20);
protected int xmax, ymax, x2ago, y2ago; protected float vx; //vx is the velocity left or right
public final int UP=1, DOWN=2, LEFT=4, RIGHT=8; //I chose the values as I did for bitmasking, just constants for direction
protected static Color background; //now I can set the background staticly for all the letters
//This time I let the applet pass in the right boundry of the animation (where it should bounce off the walls, since it will move left and right now) and the 'ground' (maximum y coordinate)
public interactingletters(char newletter, int newx, int newy, int rightbound, int ground)
{
super(newletter, newx, newy);
xmax=rightbound;
ymax=ground;
background = Color.black;
}
//method that returns the centerpoint, called by the interact() method so it can know the location of the other letter
public Point getPoint()
{
return (new Point(x, y));
}
//Set the background the letters refresh to something other than black
public void setBackground(Color color)
{
background = color;
}
//overrides update- now if the letter is at one of the sides, it will bounce it in the other direction
public void update()
{
x+=(int)vx; //now I have to track the x position, too
y+=v;
v+=1;
if (vx<=-1)
vx+=.1;
else if (vx>=1)
vx-=.1; //vx goes toward 0 by .1 each frame
if (y>ymax)
{
y=ymax; //this helps make sure the letters don't get grounded
bounce(UP);
}
else if (y<0)
bounce(DOWN);
if (x>xmax-10)
bounce(LEFT);
else if (x<0)
bounce(RIGHT);
}
//I've always wanted to make a method do this, it might be nicer if it took in a strength, too, though.
//Basically it sets the direction in the direction of the bounce and if it's bouncing up, it reduces it slightly to simulate imperfect elasticity (so it settles a little at a time)
public void bounce(int direction)
{
if ((direction&UP)>0)
v=-9*Math.abs(v)/10;
else if ((direction&DOWN)>0)
v=Math.abs(v);
if ((direction&LEFT)>0)
vx=-Math.abs(vx);
else if ((direction&RIGHT)>0)
vx=Math.abs(vx);
}
//checks if two letters are close enough to bounce off each other and calls bounce in the appropriate direction
public void interact(interactingletters letter2)
{
int x2 = letter2.getPoint().x;
int y2 = letter2.getPoint().y;
int bounce1=0, bounce2=0;
if (Math.abs(x-x2)<=10 && Math.abs(y-y2)<=20 &! Character.isWhitespace(letter) &! Character.isWhitespace(letter2.getLetter())) //if the letters are 'touching' each other and neither is whitespace (no one wants spaces bouncing off other letters)
{
if (xy2) //if the current letter is under letter2
{
bounce1 |=DOWN;
bounce2 |=UP;
}
else if (y2>y) //if the current letter is above letter2
{
bounce1 |=UP;
bounce2 |=DOWN;
}
bounce(bounce1);
letter2.bounce(bounce2);
}
}
//overridden to set vx to 0 while dragging so it doesn't remember the old velocity
public void update(int x, int y)
{
this.x=x+xoffset;
this.y=y+yoffset;
v=0;
vx=0;
}
//decides on a direction for v and vx based on where the mouse was two animation frames ago
public void release(int x, int y)
{
vx = (x-x2ago)/2;
v = (y-y2ago)/2;
}
//sets the power of vx in the object, used to set the 'power' of the bounce in interact()
public void setVX(int newvx)
{
vx=newvx;
}
//overriden to keep track of the x,y coordinates 2 frames ago for use in release()
public void draw(Graphics g)
{
g.setColor(background);
g.drawString(new String("") + letter, lastx, lasty);
g.setColor(color);
g.drawString(new String("") + letter, x, y);
x2ago=lastx; //stores the location 2 frames ago for release()
y2ago=lasty;
lastx=x;
lasty=y;
}
//returns the value of the letter, used by interact() to see if it's whitespace
public char getLetter()
{
return letter;
}
}