// <applet code="FullBall3" width="400" height="400"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FullBall3 extends Applet implements Runnable
{
//position of the ball
int x_pos = 12;
int y_pos = 12;
//ball size
int radius = 5;
//ball direction
int x_speed = 6;
int y_speed = 8;
//position of the slider
int sliderx_pos = 50;
int slidery_pos = 150;
//size of slider
int swidth = 60;
int sheight = 1;
Rectangle box = new Rectangle(swidth, sheight);
//slider direcion
int sliderx_speed = 0;
int appletsize_top = 0;
int appletsize_bottom = 200;
int appletsize_left = 0;
int appletsize_right = 350;
//level stuff
int lx_pos = 100;
int ly_pos = appletsize_top +20;
int lwidth = 50;
int lheight = 1;
Rectangle brick = new Rectangle(lx_pos, ly_pos, lwidth, lheight);
//random movement
Random rnd = new Random();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
boolean allBricksRemoved = false;
public void init()
{
enableEvents(AWTEvent.KEY_EVENT_MASK);
setFocusable(true);
}
public void start ()
{
running = true;
thread = new Thread (this);
thread.start();
}
public void stop()
{
running = false;
if(thread != null)
thread.interrupt();
thread = null;
}
public void run ()
{
while (running)
{
//change direct if the ball is about to hit a wall
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right )
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if(y_pos + radius + y_speed > appletsize_bottom )
{
// Reposition ball to avoid overshooting boundry
// as a consequence of high speed value.
y_pos = appletsize_bottom - radius;
y_speed *= -1;
}
// What to do if we will hit the top of the slider on the
// next frame, ie, after the next trip through this method.
if(x_pos + radius + x_speed > sliderx_pos &&
x_pos - radius + x_speed < sliderx_pos + swidth)
{
// Ball is in vertical plane of slider.
// Check for possible contact.
if(y_pos + radius + y_speed > slidery_pos)
{
y_speed *= -1;
}
}
// What to do if we will hit the bottom of the
// brick the next time through.
if(x_pos + radius + x_speed > lx_pos &&
x_pos - radius + x_speed < lx_pos + lwidth)
{
// Ball is in vertical plane of brick.
// Check for contact on underside of brick.
if(y_pos - radius /*+ y_speed*/ < ly_pos + brick.height)
{
y_speed *= -1;
System.out.println("brick being removed");
lx_pos = 10000;
ly_pos = 10000;
brick.setLocation(lx_pos, ly_pos);
// When the last brick is gone:
allBricksRemoved = true;
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint();
if(allBricksRemoved)
{
resetBricks();
}
try
{
Thread.sleep (90);
}
catch (InterruptedException ex)
{
running = false;
break;
}
}
}
private void resetBricks()
{
stop();
new Thread(new Runnable()
{
public void run()
{
allBricksRemoved = false;
// Reset the brick(s) before we start up again.
int w = appletsize_right - appletsize_left - brick.width;
lx_pos = rnd.nextInt(w);
ly_pos = appletsize_top +20;
brick.setLocation(lx_pos, ly_pos);
repaint();
System.out.printf("brick location = [%d, %d]%n",
lx_pos, ly_pos);
start();
}
}).start();
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
// Show right and lower boundries to help in
// adjusting animation variables.
g.drawLine(appletsize_right, appletsize_top,
appletsize_right, appletsize_bottom);
g.drawLine(appletsize_left, appletsize_bottom,
appletsize_right, appletsize_bottom);
//DRAWING THE BALL
Graphics2D g2 = (Graphics2D) g;
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//DRAWING THE SLIDER
box.setLocation(sliderx_pos, slidery_pos);
g2.setColor (Color.blue);
g2.draw(box);
//DRAWING THE BRICKS
g2.setColor (Color.pink);
g2.draw(brick);
}
protected void processKeyEvent(KeyEvent e)
{
int keyCode = e.getKeyCode();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
if (sliderx_pos -10 > appletsize_left)
sliderx_pos = sliderx_pos -20;
}
//move if right key is pressed
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth +10 < appletsize_right)
sliderx_pos = sliderx_pos +20;
}
}
}