This is Java programing. Modify the balls.java program so that all the five up-and-down motion
balls have different size and different colors. Also, add 5 left-and-right motion balls that have
different size, color, and speed. You have to use the random class and the DrawingPanelclass.
use DrawingPanel.java.
import java.awt.*;
import java.util.*;
public class balls {
public static int width = 800;
public static int height = 600;
public static int b1x;
public static int b1y;
public static int b1s;
public static int b2x;
public static int b2y;
public static int b2s;
public static int b3x;
public static int b3y;
public static int b3s;
public static int b4x;
public static int b4y;
public static int b4s;
public static int b5x;
public static int b5y;
public static int b5s;
public static String b1d=\"down\";
public static String b2d=\"down\";
public static String b3d=\"down\";
public static String b4d=\"down\";
public static String b5d=\"down\";
public static int ballSize=50;
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(width, height);
panel.setBackground(Color.LIGHT_GRAY);
Graphics g = panel.getGraphics();
g.setColor(Color.GREEN);
getInitialStatus();
while (true){
for (int i=1; i<=5; i++) {
switch(i) {
case 1:
if (b1d==\"up\") {
b1y-=b1s;
if (b1y<=0) {
b1d=\"down\";
}
} else {
b1y+=b1s;
if (b1y>=height-ballSize) {
b1d=\"up\";
}
}
g.fillOval(b1x,b1y, ballSize, ballSize);
break;
case 2:
if (b2d==\"up\") {
b2y-=b2s;
if (b2y<=0) {
b2d=\"down\";
}
} else {
b2y+=b2s;
if (b2y>=height-ballSize) {
b2d=\"up\";
}
}
g.fillOval(b2x,b2y, ballSize, ballSize);
break;
case 3:
if (b3d==\"up\") {
b3y-=b3s;
if (b3y<=0) {
b3d=\"down\";
}
} else {
b3y+=b3s;
if (b3y>=height-ballSize) {
b3d=\"up\";
}
}
g.fillOval(b3x,b3y, ballSize, ballSize);
break;
case 4:
if (b4d==\"up\") {
b4y-=b4s;
if (b4y<=0) {
b4d=\"down\";
}
} else {
b4y+=b4s;
if (b4y>=height-ballSize) {
b4d=\"up\";
}
}
g.fillOval(b4x,b4y, ballSize, ballSize);
break;
case 5:
if (b5d==\"up\") {
b5y-=b5s;
if (b5y<=0) {
b5d=\"down\";
}
} else {
b5y+=b5s;
if (b5y>=height-ballSize) {
b5d=\"up\";
}
}
g.fillOval(b5x,b5y, ballSize, ballSize);
break;
}
}
panel.sleep(100);
g.clearRect(0,0,width,height);
}
}
public static void getInitialStatus(){
Random rand = new Random();
b1x=rand.nextInt(width-ballSize);
b2x=rand.nextInt(width-ballSize);
b3x=rand.nextInt(width-ballSize);
b4x=rand.nextInt(width-ballSize);
b5x=rand.nextInt(width-ballSize);
b1y=0;
b2y=0;
b3y=0;
b4y=0;
b5y=0;
b1s=rand.nextInt(30)+1;
b2s=rand.nextInt(30)+1;
b3s=rand.nextInt(30)+1;
b4s=rand.nextInt(30)+1;
b5s=rand.nextInt(30)+1;
}
}
Solution
import java.awt.*;
import java.util.*;
public class balls {
public static int width = 800;
public static int height = 600;
public static int b1x;
public static int b1y;
public static int b1s;
public static int b1z;
public static int b2x;
public static int b2y;
public static int b2s;
public static int b2z;
public static int b3x;
public static int b3y;
public static int b3s;
public static int .
This is Java programing. Modify the balls.java program so that all t.pdf
1. This is Java programing. Modify the balls.java program so that all the five up-and-down motion
balls have different size and different colors. Also, add 5 left-and-right motion balls that have
different size, color, and speed. You have to use the random class and the DrawingPanelclass.
use DrawingPanel.java.
import java.awt.*;
import java.util.*;
public class balls {
public static int width = 800;
public static int height = 600;
public static int b1x;
public static int b1y;
public static int b1s;
public static int b2x;
public static int b2y;
public static int b2s;
public static int b3x;
public static int b3y;
public static int b3s;
public static int b4x;
public static int b4y;
public static int b4s;
public static int b5x;
public static int b5y;
public static int b5s;
public static String b1d="down";
public static String b2d="down";
public static String b3d="down";
public static String b4d="down";
public static String b5d="down";
public static int ballSize=50;
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(width, height);
panel.setBackground(Color.LIGHT_GRAY);
2. Graphics g = panel.getGraphics();
g.setColor(Color.GREEN);
getInitialStatus();
while (true){
for (int i=1; i<=5; i++) {
switch(i) {
case 1:
if (b1d=="up") {
b1y-=b1s;
if (b1y<=0) {
b1d="down";
}
} else {
b1y+=b1s;
if (b1y>=height-ballSize) {
b1d="up";
}
}
g.fillOval(b1x,b1y, ballSize, ballSize);
break;
case 2:
if (b2d=="up") {
b2y-=b2s;
if (b2y<=0) {
b2d="down";
}
} else {
b2y+=b2s;
if (b2y>=height-ballSize) {
b2d="up";
}
}
g.fillOval(b2x,b2y, ballSize, ballSize);
break;
case 3:
3. if (b3d=="up") {
b3y-=b3s;
if (b3y<=0) {
b3d="down";
}
} else {
b3y+=b3s;
if (b3y>=height-ballSize) {
b3d="up";
}
}
g.fillOval(b3x,b3y, ballSize, ballSize);
break;
case 4:
if (b4d=="up") {
b4y-=b4s;
if (b4y<=0) {
b4d="down";
}
} else {
b4y+=b4s;
if (b4y>=height-ballSize) {
b4d="up";
}
}
g.fillOval(b4x,b4y, ballSize, ballSize);
break;
case 5:
if (b5d=="up") {
b5y-=b5s;
if (b5y<=0) {
b5d="down";
}
} else {
b5y+=b5s;
if (b5y>=height-ballSize) {
5. import java.util.*;
public class balls {
public static int width = 800;
public static int height = 600;
public static int b1x;
public static int b1y;
public static int b1s;
public static int b1z;
public static int b2x;
public static int b2y;
public static int b2s;
public static int b2z;
public static int b3x;
public static int b3y;
public static int b3s;
public static int b3z;
public static int b4x;
public static int b4y;
public static int b4s;
public static int b4z;
public static int b5x;
public static int b5y;
public static int b5s;
public static int b5z;
public static int b6x;
public static int b6y;
public static int b6s;
public static int b6z;
public static int b7x;
public static int b7y;
public static int b7s;
public static int b7z;
public static int b8x;
public static int b8y;
public static int b8s;
public static int b8z;
6. public static int b9x;
public static int b9y;
public static int b9s;
public static int b9z;
public static int b10x;
public static int b10y;
public static int b10s;
public static int b10z;
public static String b1d="down";
public static String b2d="down";
public static String b3d="down";
public static String b4d="down";
public static String b5d="down";
public static String b6d="right";
public static String b7d="right";
public static String b8d="right";
public static String b9d="right";
public static String b10d="right";
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(width, height);
panel.setBackground(Color.LIGHT_GRAY);
Graphics g = panel.getGraphics();
g.setColor(Color.GREEN);
getInitialStatus();
while (true){
for (int i=1; i<=10; i++) {
switch(i) {
case 1:
if (b1d=="up") {
b1y-=b1s;
if (b1y<=0) {
b1d="down";
}
}
else {
b1y+=b1s;
7. if (b1y>=height-b1z) {
b1d="up";
}
}
g.setColor(Color.RED);
g.fillOval(b1x,b1y, b1z, b1z);
break;
case 2:
if (b2d=="up") {
b2y-=b2s;
if (b2y<=0) {
b2d="down";
}
}
else {
b2y+=b2s;
if (b2y>=height-b2z) {
b2d="up";
}
}
g.setColor(Color.BLUE);
g.fillOval(b2x,b2y, b2z, b2z);
break;
case 3:
if (b3d=="up") {
b3y-=b3s;
if (b3y<=0) {
b3d="down";
}
}
else{
b3y+=b3s;
if (b3y>=height-b3z) {
b3d="up";
}
}
8. g.setColor(Color.BLACK);
g.fillOval(b3x,b3y, b3z, b3z);
break;
case 4:
if (b4d=="up") {
b4y-=b4s;
if (b4y<=0) {
b4d="down";
}
}
else {
b4y+=b4s;
if (b4y>=height-b4z) {
b4d="up";
}
}
g.setColor(Color.CYAN);
g.fillOval(b4x,b4y, b4z, b4z);
break;
case 5:
if (b5d=="up") {
b5y-=b5s;
if (b5y<=0) {
b5d="down";
}
}
else {
b5y+=b5s;
if (b5y>=height-b5z) {
b5d="up";
}
}
g.setColor(Color.YELLOW);
g.fillOval(b5x,b5y, b5z, b5z);
break;
case 6:
9. if (b6d=="left") {
b6x-=b6s;
if (b6x<=0) {
b6d="right";
}
}
else {
b6x+=b6s;
if (b6x>=width-b6z) {
b6d="left";
}
}
g.setColor(Color.GREEN);
g.fillOval(b6x,b6y, b6z, b6z);
break;
case 7:
if (b7d=="left") {
b7x-=b7s;
if (b7x<=0) {
b2d="right";
}
}
else {
b7x+=b7s;
if (b7x>=width-b7z) {
b7d="left";
}
}
g.setColor(Color.MAGENTA);
g.fillOval(b7x,b7y, b7z, b7z);
break;
case 8:
if (b8d=="left") {
b8x-=b8s;
if (b8x<=0) {
b8d="right";