SlideShare a Scribd company logo
1 of 12
Download to read offline
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 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;
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;
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";
}
}
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:
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";
}
}
else{
b8x+=b8s;
if (b8x>=width-b8z) {
b8d="left";
}
}
g.setColor(Color.PINK);
g.fillOval(b8x,b8y, b8z, b8z);
break;
case 9:
if (b9d=="left") {
b9x-=b9s;
if (b9x<=0) {
b9d="right";
}
}
else {
b9x+=b9s;
if (b9x>=width-b9z) {
b9d="left";
}
}
g.setColor(Color.WHITE);
g.fillOval(b9x,b9y, b9z, b9z);
break;
case 10:
if (b10d=="left") {
b10x-=b10s;
if (b10x<=0) {
b10d="right";
}
}
else {
b10x+=b10s;
if (b10x>=width-b10z) {
b10d="left";
}
}
g.fillOval(b10x,b10y, b10z, b10z);
break;
}
}
panel.sleep(100);
g.clearRect(0,0,width,height);
}
}
public static void getInitialStatus(){
Random rand = new Random();
b1x=rand.nextInt(width-b1z);
b2x=rand.nextInt(width-b2z);
b3x=rand.nextInt(width-b3z);
b4x=rand.nextInt(width-b4z);
b5x=rand.nextInt(width-b5z);
b1y=0;
b2y=0;
b3y=0;
b4y=0;
b5y=0;
b6y=rand.nextInt(width-b6z);
b7y=rand.nextInt(width-b7z);
b8y=rand.nextInt(width-b8z);
b9y=rand.nextInt(width-b9z);
b10y=rand.nextInt(width-b10z);
b6x=0;
b7x=0;
b8x=0;
b9x=0;
b10x=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;
b6s=rand.nextInt(30)+1;
b7s=rand.nextInt(30)+1;
b8s=rand.nextInt(30)+1;
b9s=rand.nextInt(30)+1;
b10s=rand.nextInt(30)+1;
b1z = rand.nextInt(30)+20;
b2z = rand.nextInt(30)+20;
b3z = rand.nextInt(30)+20;
b4z = rand.nextInt(30)+20;
b5z = rand.nextInt(30)+20;
b6z = rand.nextInt(30)+20;
b7z = rand.nextInt(30)+20;
b8z = rand.nextInt(30)+20;
b9z = rand.nextInt(30)+20;
b10z = rand.nextInt(30)+20;
}
}

More Related Content

More from boothlynntur11512

Describe five robot application areas for vision systems. (Select fiv.pdf
Describe five robot application areas for vision systems. (Select fiv.pdfDescribe five robot application areas for vision systems. (Select fiv.pdf
Describe five robot application areas for vision systems. (Select fiv.pdfboothlynntur11512
 
Cousin Vinnie is a shareholder and an employee in the family busines.pdf
Cousin Vinnie is a shareholder and an employee in the family busines.pdfCousin Vinnie is a shareholder and an employee in the family busines.pdf
Cousin Vinnie is a shareholder and an employee in the family busines.pdfboothlynntur11512
 
Create two linked lists each having eight nodes…..the first list has.pdf
Create two linked lists each having eight nodes…..the first list has.pdfCreate two linked lists each having eight nodes…..the first list has.pdf
Create two linked lists each having eight nodes…..the first list has.pdfboothlynntur11512
 
Chrome File Edit View History Bookmarks People Window He.pdf
Chrome File Edit View History Bookmarks People Window He.pdfChrome File Edit View History Bookmarks People Window He.pdf
Chrome File Edit View History Bookmarks People Window He.pdfboothlynntur11512
 
Chapter 12 Information Systems, Read this book Computer Science Illu.pdf
Chapter 12 Information Systems, Read this book Computer Science Illu.pdfChapter 12 Information Systems, Read this book Computer Science Illu.pdf
Chapter 12 Information Systems, Read this book Computer Science Illu.pdfboothlynntur11512
 
×y Connect nect Securehttpsnewcon connect.html 11.pdf
×y Connect nect Securehttpsnewcon connect.html 11.pdf×y Connect nect Securehttpsnewcon connect.html 11.pdf
×y Connect nect Securehttpsnewcon connect.html 11.pdfboothlynntur11512
 
You are instructing a class of high school students on different wir.pdf
You are instructing a class of high school students on different wir.pdfYou are instructing a class of high school students on different wir.pdf
You are instructing a class of high school students on different wir.pdfboothlynntur11512
 
Write a Java program that creates a drawing area of appropriate size.pdf
Write a Java program that creates a drawing area of appropriate size.pdfWrite a Java program that creates a drawing area of appropriate size.pdf
Write a Java program that creates a drawing area of appropriate size.pdfboothlynntur11512
 
Why is it that bacterial ribosomes can begin translation before mRNA .pdf
Why is it that bacterial ribosomes can begin translation before mRNA .pdfWhy is it that bacterial ribosomes can begin translation before mRNA .pdf
Why is it that bacterial ribosomes can begin translation before mRNA .pdfboothlynntur11512
 
Which of the statements below is FALSE 1 Common stocks ownership c.pdf
Which of the statements below is FALSE 1 Common stocks ownership c.pdfWhich of the statements below is FALSE 1 Common stocks ownership c.pdf
Which of the statements below is FALSE 1 Common stocks ownership c.pdfboothlynntur11512
 
Which of the following is NOT part of the information about foreign o.pdf
Which of the following is NOT part of the information about foreign o.pdfWhich of the following is NOT part of the information about foreign o.pdf
Which of the following is NOT part of the information about foreign o.pdfboothlynntur11512
 
What is the yield curve Why is the yield curve considered a leading.pdf
What is the yield curve Why is the yield curve considered a leading.pdfWhat is the yield curve Why is the yield curve considered a leading.pdf
What is the yield curve Why is the yield curve considered a leading.pdfboothlynntur11512
 
Think back to the (last) time you got a start. That is, somebody sta.pdf
Think back to the (last) time you got a start. That is, somebody sta.pdfThink back to the (last) time you got a start. That is, somebody sta.pdf
Think back to the (last) time you got a start. That is, somebody sta.pdfboothlynntur11512
 
Should companies continue to use EDI systems Why or why notSol.pdf
Should companies continue to use EDI systems Why or why notSol.pdfShould companies continue to use EDI systems Why or why notSol.pdf
Should companies continue to use EDI systems Why or why notSol.pdfboothlynntur11512
 
Since HIV targets the T cell of the body, when an individual gets a N.pdf
Since HIV targets the T cell of the body, when an individual gets a N.pdfSince HIV targets the T cell of the body, when an individual gets a N.pdf
Since HIV targets the T cell of the body, when an individual gets a N.pdfboothlynntur11512
 
1.The anticipated impact of the Practice Standards is considered and.pdf
1.The anticipated impact of the Practice Standards is considered and.pdf1.The anticipated impact of the Practice Standards is considered and.pdf
1.The anticipated impact of the Practice Standards is considered and.pdfboothlynntur11512
 
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdf
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdfQuestion 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdf
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdfboothlynntur11512
 
Q2 Write a C program called myupper4.c to take as command line inpu.pdf
Q2 Write a C program called myupper4.c to take as command line inpu.pdfQ2 Write a C program called myupper4.c to take as command line inpu.pdf
Q2 Write a C program called myupper4.c to take as command line inpu.pdfboothlynntur11512
 
Part A Input the missing value. Title Amount Equity Owners Capital.pdf
Part A Input the missing value. Title Amount Equity Owners Capital.pdfPart A Input the missing value. Title Amount Equity Owners Capital.pdf
Part A Input the missing value. Title Amount Equity Owners Capital.pdfboothlynntur11512
 
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdf
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdfPlease explain in 1-2 sentences. Why do transmembrane domains of pro.pdf
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdfboothlynntur11512
 

More from boothlynntur11512 (20)

Describe five robot application areas for vision systems. (Select fiv.pdf
Describe five robot application areas for vision systems. (Select fiv.pdfDescribe five robot application areas for vision systems. (Select fiv.pdf
Describe five robot application areas for vision systems. (Select fiv.pdf
 
Cousin Vinnie is a shareholder and an employee in the family busines.pdf
Cousin Vinnie is a shareholder and an employee in the family busines.pdfCousin Vinnie is a shareholder and an employee in the family busines.pdf
Cousin Vinnie is a shareholder and an employee in the family busines.pdf
 
Create two linked lists each having eight nodes…..the first list has.pdf
Create two linked lists each having eight nodes…..the first list has.pdfCreate two linked lists each having eight nodes…..the first list has.pdf
Create two linked lists each having eight nodes…..the first list has.pdf
 
Chrome File Edit View History Bookmarks People Window He.pdf
Chrome File Edit View History Bookmarks People Window He.pdfChrome File Edit View History Bookmarks People Window He.pdf
Chrome File Edit View History Bookmarks People Window He.pdf
 
Chapter 12 Information Systems, Read this book Computer Science Illu.pdf
Chapter 12 Information Systems, Read this book Computer Science Illu.pdfChapter 12 Information Systems, Read this book Computer Science Illu.pdf
Chapter 12 Information Systems, Read this book Computer Science Illu.pdf
 
×y Connect nect Securehttpsnewcon connect.html 11.pdf
×y Connect nect Securehttpsnewcon connect.html 11.pdf×y Connect nect Securehttpsnewcon connect.html 11.pdf
×y Connect nect Securehttpsnewcon connect.html 11.pdf
 
You are instructing a class of high school students on different wir.pdf
You are instructing a class of high school students on different wir.pdfYou are instructing a class of high school students on different wir.pdf
You are instructing a class of high school students on different wir.pdf
 
Write a Java program that creates a drawing area of appropriate size.pdf
Write a Java program that creates a drawing area of appropriate size.pdfWrite a Java program that creates a drawing area of appropriate size.pdf
Write a Java program that creates a drawing area of appropriate size.pdf
 
Why is it that bacterial ribosomes can begin translation before mRNA .pdf
Why is it that bacterial ribosomes can begin translation before mRNA .pdfWhy is it that bacterial ribosomes can begin translation before mRNA .pdf
Why is it that bacterial ribosomes can begin translation before mRNA .pdf
 
Which of the statements below is FALSE 1 Common stocks ownership c.pdf
Which of the statements below is FALSE 1 Common stocks ownership c.pdfWhich of the statements below is FALSE 1 Common stocks ownership c.pdf
Which of the statements below is FALSE 1 Common stocks ownership c.pdf
 
Which of the following is NOT part of the information about foreign o.pdf
Which of the following is NOT part of the information about foreign o.pdfWhich of the following is NOT part of the information about foreign o.pdf
Which of the following is NOT part of the information about foreign o.pdf
 
What is the yield curve Why is the yield curve considered a leading.pdf
What is the yield curve Why is the yield curve considered a leading.pdfWhat is the yield curve Why is the yield curve considered a leading.pdf
What is the yield curve Why is the yield curve considered a leading.pdf
 
Think back to the (last) time you got a start. That is, somebody sta.pdf
Think back to the (last) time you got a start. That is, somebody sta.pdfThink back to the (last) time you got a start. That is, somebody sta.pdf
Think back to the (last) time you got a start. That is, somebody sta.pdf
 
Should companies continue to use EDI systems Why or why notSol.pdf
Should companies continue to use EDI systems Why or why notSol.pdfShould companies continue to use EDI systems Why or why notSol.pdf
Should companies continue to use EDI systems Why or why notSol.pdf
 
Since HIV targets the T cell of the body, when an individual gets a N.pdf
Since HIV targets the T cell of the body, when an individual gets a N.pdfSince HIV targets the T cell of the body, when an individual gets a N.pdf
Since HIV targets the T cell of the body, when an individual gets a N.pdf
 
1.The anticipated impact of the Practice Standards is considered and.pdf
1.The anticipated impact of the Practice Standards is considered and.pdf1.The anticipated impact of the Practice Standards is considered and.pdf
1.The anticipated impact of the Practice Standards is considered and.pdf
 
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdf
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdfQuestion 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdf
Question 1 What updates have been brought by snmpv2 to SNMPv1 commun.pdf
 
Q2 Write a C program called myupper4.c to take as command line inpu.pdf
Q2 Write a C program called myupper4.c to take as command line inpu.pdfQ2 Write a C program called myupper4.c to take as command line inpu.pdf
Q2 Write a C program called myupper4.c to take as command line inpu.pdf
 
Part A Input the missing value. Title Amount Equity Owners Capital.pdf
Part A Input the missing value. Title Amount Equity Owners Capital.pdfPart A Input the missing value. Title Amount Equity Owners Capital.pdf
Part A Input the missing value. Title Amount Equity Owners Capital.pdf
 
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdf
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdfPlease explain in 1-2 sentences. Why do transmembrane domains of pro.pdf
Please explain in 1-2 sentences. Why do transmembrane domains of pro.pdf
 

Recently uploaded

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

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) {
  • 4. 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.*;
  • 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";
  • 10. } } else{ b8x+=b8s; if (b8x>=width-b8z) { b8d="left"; } } g.setColor(Color.PINK); g.fillOval(b8x,b8y, b8z, b8z); break; case 9: if (b9d=="left") { b9x-=b9s; if (b9x<=0) { b9d="right"; } } else { b9x+=b9s; if (b9x>=width-b9z) { b9d="left"; } } g.setColor(Color.WHITE); g.fillOval(b9x,b9y, b9z, b9z); break; case 10: if (b10d=="left") { b10x-=b10s; if (b10x<=0) { b10d="right"; } } else { b10x+=b10s;
  • 11. if (b10x>=width-b10z) { b10d="left"; } } g.fillOval(b10x,b10y, b10z, b10z); break; } } panel.sleep(100); g.clearRect(0,0,width,height); } } public static void getInitialStatus(){ Random rand = new Random(); b1x=rand.nextInt(width-b1z); b2x=rand.nextInt(width-b2z); b3x=rand.nextInt(width-b3z); b4x=rand.nextInt(width-b4z); b5x=rand.nextInt(width-b5z); b1y=0; b2y=0; b3y=0; b4y=0; b5y=0; b6y=rand.nextInt(width-b6z); b7y=rand.nextInt(width-b7z); b8y=rand.nextInt(width-b8z); b9y=rand.nextInt(width-b9z); b10y=rand.nextInt(width-b10z); b6x=0; b7x=0; b8x=0; b9x=0; b10x=0; b1s=rand.nextInt(30)+1; b2s=rand.nextInt(30)+1;
  • 12. b3s=rand.nextInt(30)+1; b4s=rand.nextInt(30)+1; b5s=rand.nextInt(30)+1; b6s=rand.nextInt(30)+1; b7s=rand.nextInt(30)+1; b8s=rand.nextInt(30)+1; b9s=rand.nextInt(30)+1; b10s=rand.nextInt(30)+1; b1z = rand.nextInt(30)+20; b2z = rand.nextInt(30)+20; b3z = rand.nextInt(30)+20; b4z = rand.nextInt(30)+20; b5z = rand.nextInt(30)+20; b6z = rand.nextInt(30)+20; b7z = rand.nextInt(30)+20; b8z = rand.nextInt(30)+20; b9z = rand.nextInt(30)+20; b10z = rand.nextInt(30)+20; } }