All pastes #543467 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-06-06 02:45 UTC
rendered paste body
import java.applet.*;
import java.awt.*;
//import java.io.IOException;
import java.lang.Runnable;

@SuppressWarnings("serial")

public class bouncingBall extends Applet implements Runnable {

Graphics k;
Image bufferscreen;
	
int DELAYBALL = (int)((Math.random()*5)+5);

int x,y,x1,y1;
int dx=4;
int dy=4;
int dx1=4;
int dy1=4;
int radius = 49;
int radius1 = 49;


public void init()
{
	this.setSize(800, 600);
setBackground(Color.decode("0x0009F0"));

bufferscreen = createImage(800,600);  
k = bufferscreen.getGraphics(); 

y = (radius + ((int)(Math.random()*(301-radius))));
x = (radius + ((int)(Math.random()*(401-radius))));
y1 = (radius1 + ((int)((Math.random()*(301-radius1)+300))));
x1 = (radius1 + ((int)((Math.random()*(401-radius1)+400))));
}

public void run() // will run this thread
{
while (true)
{
repaint(); // ask JVM to call paint.
sleeper();
move();
}

} // run

private void sleeper()

{
try {
Thread.sleep(DELAYBALL); // requires a catch or throw
}
catch (InterruptedException e)
{
System.out.println("problem is " + e.getMessage());
}
}

public void start()
{
System.out.println(" start ");
Thread myThread = new Thread(this);
myThread.start();
System.out.println(" hello world ");

} // start
private void move() // rules for movement
{
int w = this.getWidth(); // get new window size
int h = this.getHeight();

if ( x> (w-radius) || x<radius )dx = -dx;
x+=dx;
if ( x1> (w-radius1) || x1<radius1 )dx1 = -dx1;
x1+=dx1;

//y = center + (int) (10* Math.cos(0.10* x));
if ( y>(h-radius) || y<radius ) dy=-dy;
y+=dy;
if ( y1>(h-radius1) || y1<radius1 ) dy1=-dy1;
y1+=dy1;
if(Math.sqrt(((x1*x1)-(2*x1*x)+(x*x)) + ((y1*y1)-(2*y1*y)+(y*y))) < (radius + radius1)){
	double xx= Math.sqrt(((x1*x1)-(2*x1*x)+(x*x)) + ((y1*y1)-(2*y1*y)+(y*y)));
	boolean lastresort= true;
	System.out.println("GREEN BALL @ X="+x+" Y="+y+"MOVING @ DX="+dx+" DY="+dy+" TEAL BALL @ X1="+x1+" Y1="+y1+"MOVING @ DX1="+dx1+" DY1="+dy1+ " SQRT = "+xx);
if (x<x1 && y<y1 && dx>0 && dy>0 && dx1<0 && dy1<0 ) { // TOP LEFT HITTING BOTTOM RIGHT
	System.out.println("hit:TOP LEFT HITTING BOTTOM RIGHT DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dy = -dy;
	dy1 = -dy1;
	dx1 = -dx1;
	lastresort = false;
}
if (x<x1 && dx>0 && dy>0 && dx1<0 && dy1>0 ) {
	System.out.println("hit:TL HITTING TR DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dx1 = -dx1;
	lastresort = false;
}
if (y<y1 && dx>0 && dy>0 && dx1>0 && dy1<0 ) {
	System.out.println("hit:TL HITTING BL DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dy = -dy;
	dy1 = -dy1;
	lastresort = false;
}

if (x>x1 && y<y1 && dx<0 && dy>0 && dx1>0 && dy1<0 ) { // TOP RIGHT HITTING BOTTOM LEFT
	System.out.println("hit:TOP RIGHT HITTING BOTTOM LEFT DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dy = -dy;
	dy1 = -dy1;
	dx1 = -dx1;
	lastresort = false;
}
if (x>x1 && dx<0 && dy>0 && dx1>0 && dy1>0 ) {
	System.out.println("hit:TR HITTING TL DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dx1 = -dx1;
	lastresort = false;
}
if (y<y1 && dx<0 && dy>0 && dx1<0 && dy1<0 ) {
	System.out.println("hit:TR HITTING BR DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dy = -dy;
	dy1 = -dy1;
	lastresort = false;
}

if (x>x1 && y>y1 && dx<0 && dy<0 && dx1>0 && dy1>0) { // BOTTOM RIGHT HITTING TOP LEFT
	System.out.println("hit:BOTTOM RIGHT HITTING TOP LEFT DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dy = -dy;
	dy1 = -dy1;
	dx1 = -dx1;
	lastresort = false;
}
if (x>x1 && dx<0 && dy<0 && dx1>0 && dy1<0 ) {
	System.out.println("hit:BR HITTING BL DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dx1 = -dx1;
	lastresort = false;
}
if (y>y1 && dx<0 && dy<0 && dx1<0 && dy1>0 ) {
	System.out.println("hit:BR HITTING TR DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dy = -dy;
	dy1 = -dy1;
	lastresort = false;
}

if (x<x1 && y>y1 && dx>0 && dy<0 && dx1<0 && dy1>0 ) { // BOTTOM LEFT HITTING TOP RIGHT
	System.out.println("hit:BOTTOM LEFT HITTING TOP RIGHT DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dy = -dy;
	dx1 = -dx1;
	dy1 = -dy1;
	lastresort = false;
}
if (x<x1 && dx>0 && dy<0 && dx1<0 && dy1<0 ) {
	System.out.println("hit:BL HITTING BR DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dx = -dx;
	dx1 = -dx1;
	lastresort = false;
}
if (y>y1 && dx>0 && dy<0 && dx1>0 && dy1>0 ) {
	System.out.println("hit:BL HITTING TL DX=" + dx+ " DY=" +dy+ " DX1=" + dx1 + " DY1="+ dy1+ " SQRT = "+xx);
	dy = -dy;
	dy1 = -dy1;
	lastresort = false;
}
//Special Zone Cases
if (x<x1 && y<y1 && dx>0 && dy<0 && dx1<0 && dy1>0) {
	System.out.println("TRUTH");
	dx = -dx;
	dy = -dy;
	dx1 = -dx1;
	dy1 = -dy1;
	lastresort = false;
}
if (x>x1 && y>y1 && dx>0 && dy<0 && dx1<0 && dy1>0) {
	System.out.println("TRUTH");
	dx = -dx;
	dy = -dy;
	dx1 = -dx1;
	dy1 = -dy1;
	lastresort = false;
}
if (lastresort) {
System.out.println("UNEXPECTED");
dx = -dx;
dy = -dy;
dx1 = -dx1;
dy1 = -dy1;
lastresort = false;
}

// watch x and y change
}}// end move

// The JVM calls paint.
public void paint(Graphics g)
{

	try {
k.setColor(Color.decode("0x0F0F"+((int)(Math.random()*100))));
	}
	catch (NumberFormatException e) {
		System.out.println("problem is " + e.getMessage());
	}
	//k.fillOval(((x-radius)-dx), ((y-radius)-dx), 2*radius, 2*radius);
	//k.setColor(Color.CYAN);
	k.fillOval(x-radius, y-radius, 2*radius, 2*radius);
	k.setColor(Color.RED);
	k.fillOval(x, y, 5, 5);
	k.setColor(Color.decode("0xFF"+((int)(Math.random()*100))+"00"));
	//k.fillOval(((x1-radius1)-dx1), ((y1-radius1)-dx1), 2*radius1, 2*radius1);
	//k.setColor(Color.ORANGE);
	k.fillOval(x1-radius1, y1-radius1, 2*radius1, 2*radius1);
	k.setColor(Color.RED);
	k.fillOval(x1, y1, 5, 5);
	g.drawImage(bufferscreen,0,0,this);
	

} // paint
public void update(Graphics g) {
	paint(g);
}

} // applet