
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class BouncingBall extends JApplet
{
public void paint(Graphics graphics)
{
int x = 101,
y = 100, //initial position of ball
xMax = 300, //max horizontal size of panel
xMin = 20,
yMax = 265,
yMin = 20,
xInc = 5, //amount to increment x,y
yInc = 5,
radius = 40; //radius of ball
double delay = 0.02; // in seconds
//draw border for ball to bounce in
graphics.setColor(Color.red);
graphics.drawRect(xMin-5,yMin-1,xMax-xMin+radius+6,yMax-yMin+radius+1);
final Color ballColor = Color.blue;
graphics.setColor(ballColor);
while(true)
{
//cover blue ball with a white one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);
//increment x, y coordinates
x+=xInc;
y+=yInc;
//(re)set color of ball and draw it
graphics.setColor(ballColor);
graphics.fillOval(x,y,radius,radius);
//pause
Wait.pause(delay);
//change direction of ball when it reaches the border
if (x<=xMin || x>=xMax)
xInc*=-1;
if (y<=yMin || y>=yMax)
yInc*=-1;
//draw a white ball to cover the previous red one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);
}
}
}

public class Wait{
public static void pause(double sec)
{
//Causes the terminal to pause for SEC seconds
int msec = (int)(1000.0*sec + 0.5);
final int max = 3000; //pause no more than 4 seconds
msec = msec> max ? max : msec;
try
{
Thread.currentThread().sleep(msec);
}
catch (InterruptedException e)
{
//e.printStackTrace();
}
}
}

PICHU.

import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class BouncingBall extends JApplet
{
public void paint(Graphics graphics)
{
int x = 101,
y = 100, //initial position of ball
xMax = 300, //max horizontal size of panel
xMin = 20,
yMax = 265,
yMin = 20,
xInc = 5, //amount to increment x,y
yInc = 5,
radius = 40; //radius of ball
double delay = 0.02; // in seconds
//draw border for ball to bounce in
graphics.setColor(Color.red);
graphics.drawRect(xMin-5,yMin-1,xMax-xMin+radius+6,yMax-yMin+radius+1);
final Color ballColor = Color.blue;
graphics.setColor(ballColor);
while(true)
{
//cover blue ball with a white one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);
//increment x, y coordinates
x+=xInc;
y+=yInc;
//(re)set color of ball and draw it
graphics.setColor(ballColor);
graphics.fillOval(x,y,radius,radius);
//pause
Wait.pause(delay);
//change direction of ball when it reaches the border
if (x<=xMin || x>=xMax)
xInc*=-1;
if (y<=yMin || y>=yMax)
yInc*=-1;
//draw a white ball to cover the previous red one
graphics.setColor(Color.white);
graphics.fillOval(x,y,radius,radius);
}
}
}

public class Wait{
public static void pause(double sec)
{
//Causes the terminal to pause for SEC seconds
int msec = (int)(1000.0*sec + 0.5);
final int max = 3000; //pause no more than 4 seconds
msec = msec> max ? max : msec;
try
{
Thread.currentThread().sleep(msec);
}
catch (InterruptedException e)
{
//e.printStackTrace();
}
}
}


Copyright © 2026, NextGenUpdate.
All Rights Reserved.