SlideShare a Scribd company logo
1 of 2
Download to read offline
Write a program that moves the ball in a pane. You should define a pane class for displaying the
ball and provide the methods for moving the ball left, right, up. and down, as shown in Figure
Check the boundary to prevent the ball from moving out of sight completely. Exercise uses the
buttons to move the ball.
Solution
public class Exe extends Application
{
public void start(Stage primaryStage) throws Exception
{
double width = 400;
double height = 400;
BallPane ballPane = new BallPane(width / 2,height / 2, Math.min(width,height) / 15);
Button btUp = new Button("Up");
btUp.setOnAction(e -> ballPane.moveUp());
Button btDown = new Button("Down");
btDown.setOnAction(e -> ballPane.moveDown());
Button btLeft = new Button("Left");
btLeft.setOnAction(e -> ballPane.moveLeft());
Button btRight = new Button("Right");
btRight.setOnAction(e -> ballPane.moveRight());
HBox buttons = new HBox(btUp, btDown, btLeft, btRight);
buttons.setAlignment(Pos.BOTTOM_CENTER);
buttons.setSpacing(10);
buttons.setPadding(new Insets(10, 10, 10, 10));
BorderPane pane = new BorderPane();
pane.setCenter(ballPane);
pane.setBottom(buttons);
Scene scene = new Scene(pane, width, height);
primaryStage.setScene(scene);
primaryStage.setTitle("Click to move ball");
primaryStage.show();
}
private class BallPane extends Pane
{
private Circle mCircle;
public BallPane()
{
this(0, 0, 10);
}
public BallPane(double centerX, double centerY, double radius)
{
mCircle = new Circle(centerX, centerY, radius);
getChildren().add(mCircle);
}
public void moveUp()
{
if (mCircle.getCenterY() - mCircle.getRadius() - 10 < 0) return;
mCircle.setCenterY(mCircle.getCenterY() - 10);
}
public void moveDown()
{
if (mCircle.getCenterY() + mCircle.getRadius() + 10 > getHeight()) return;
mCircle.setCenterY(mCircle.getCenterY() + 10);
}
public void moveRight()
{
if (mCircle.getCenterX() + mCircle.getRadius() + 10 > getWidth()) return;
mCircle.setCenterX(mCircle.getCenterX() + 10);
}
public void moveLeft()
{
if (mCircle.getCenterX() - mCircle.getRadius() - 10 < 0) return;
mCircle.setCenterX(mCircle.getCenterX() - 10);
}
}
public static void main(String[] args)
{
Application.launch(args);
}
}

More Related Content

More from lohithkart

Describe and explain how sound travels in each Solids Liquids G.pdf
Describe and explain how sound travels in each  Solids  Liquids  G.pdfDescribe and explain how sound travels in each  Solids  Liquids  G.pdf
Describe and explain how sound travels in each Solids Liquids G.pdf
lohithkart
 
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
lohithkart
 
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
lohithkart
 
1. Answer the following questions about OSI modela.At which layer.pdf
1. Answer the following questions about OSI modela.At which layer.pdf1. Answer the following questions about OSI modela.At which layer.pdf
1. Answer the following questions about OSI modela.At which layer.pdf
lohithkart
 
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
lohithkart
 
The symbiotic relationship in which the symbiotic benefits from the r.pdf
The symbiotic relationship in which the symbiotic benefits from the r.pdfThe symbiotic relationship in which the symbiotic benefits from the r.pdf
The symbiotic relationship in which the symbiotic benefits from the r.pdf
lohithkart
 
What are brain ventricles. and what functions have been ascribed to t.pdf
What are brain ventricles. and what functions have been ascribed to t.pdfWhat are brain ventricles. and what functions have been ascribed to t.pdf
What are brain ventricles. and what functions have been ascribed to t.pdf
lohithkart
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
lohithkart
 
2. Using our sample data, construct a 95 confidence interval for th.pdf
2. Using our sample data, construct a 95 confidence interval for th.pdf2. Using our sample data, construct a 95 confidence interval for th.pdf
2. Using our sample data, construct a 95 confidence interval for th.pdf
lohithkart
 
Review diffusion and the function of cell membranes by matching each .pdf
Review diffusion and the function of cell membranes by matching each .pdfReview diffusion and the function of cell membranes by matching each .pdf
Review diffusion and the function of cell membranes by matching each .pdf
lohithkart
 
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
lohithkart
 

More from lohithkart (17)

Describe and explain how sound travels in each Solids Liquids G.pdf
Describe and explain how sound travels in each  Solids  Liquids  G.pdfDescribe and explain how sound travels in each  Solids  Liquids  G.pdf
Describe and explain how sound travels in each Solids Liquids G.pdf
 
Contrast apomixis with sexual reproduction in flowering plantsSo.pdf
Contrast apomixis with sexual reproduction in flowering plantsSo.pdfContrast apomixis with sexual reproduction in flowering plantsSo.pdf
Contrast apomixis with sexual reproduction in flowering plantsSo.pdf
 
Before dawn Josh hurriedly packed some clothes for a job-interview t.pdf
Before dawn Josh hurriedly packed some clothes for a job-interview t.pdfBefore dawn Josh hurriedly packed some clothes for a job-interview t.pdf
Before dawn Josh hurriedly packed some clothes for a job-interview t.pdf
 
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
5. A child’s toy oven uses an incandescent light bulb to bake cookie.pdf
 
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
8. Upon reviewing former CEO Jeff Swartz’s final blog post, do you a.pdf
 
1. Answer the following questions about OSI modela.At which layer.pdf
1. Answer the following questions about OSI modela.At which layer.pdf1. Answer the following questions about OSI modela.At which layer.pdf
1. Answer the following questions about OSI modela.At which layer.pdf
 
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
17. Of these, which represents a heterozygote a. aa b. Aa c. .pdf
 
When a researcher is working with a large set of scale data, whic.pdf
When a researcher is working with a large set of scale data, whic.pdfWhen a researcher is working with a large set of scale data, whic.pdf
When a researcher is working with a large set of scale data, whic.pdf
 
Which functions of the lymphatic system are not assisted by another s.pdf
Which functions of the lymphatic system are not assisted by another s.pdfWhich functions of the lymphatic system are not assisted by another s.pdf
Which functions of the lymphatic system are not assisted by another s.pdf
 
The symbiotic relationship in which the symbiotic benefits from the r.pdf
The symbiotic relationship in which the symbiotic benefits from the r.pdfThe symbiotic relationship in which the symbiotic benefits from the r.pdf
The symbiotic relationship in which the symbiotic benefits from the r.pdf
 
What are brain ventricles. and what functions have been ascribed to t.pdf
What are brain ventricles. and what functions have been ascribed to t.pdfWhat are brain ventricles. and what functions have been ascribed to t.pdf
What are brain ventricles. and what functions have been ascribed to t.pdf
 
USING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdfUSING JAVAThere are at least two types of nearly sorted array.pdf
USING JAVAThere are at least two types of nearly sorted array.pdf
 
USE PYTHON ONLY USE DEF function. For number 68Solution#!.pdf
USE PYTHON ONLY USE DEF function. For number 68Solution#!.pdfUSE PYTHON ONLY USE DEF function. For number 68Solution#!.pdf
USE PYTHON ONLY USE DEF function. For number 68Solution#!.pdf
 
2. Using our sample data, construct a 95 confidence interval for th.pdf
2. Using our sample data, construct a 95 confidence interval for th.pdf2. Using our sample data, construct a 95 confidence interval for th.pdf
2. Using our sample data, construct a 95 confidence interval for th.pdf
 
Sasha thinks she may have been adopted. She has blood type O, but mot.pdf
Sasha thinks she may have been adopted. She has blood type O, but mot.pdfSasha thinks she may have been adopted. She has blood type O, but mot.pdf
Sasha thinks she may have been adopted. She has blood type O, but mot.pdf
 
Review diffusion and the function of cell membranes by matching each .pdf
Review diffusion and the function of cell membranes by matching each .pdfReview diffusion and the function of cell membranes by matching each .pdf
Review diffusion and the function of cell membranes by matching each .pdf
 
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
4 A common symptom of pharyngitis is swelling of the anterior cervica.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
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
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
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 Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 

Write a program that moves the ball in a pane. You should define a pa.pdf

  • 1. Write a program that moves the ball in a pane. You should define a pane class for displaying the ball and provide the methods for moving the ball left, right, up. and down, as shown in Figure Check the boundary to prevent the ball from moving out of sight completely. Exercise uses the buttons to move the ball. Solution public class Exe extends Application { public void start(Stage primaryStage) throws Exception { double width = 400; double height = 400; BallPane ballPane = new BallPane(width / 2,height / 2, Math.min(width,height) / 15); Button btUp = new Button("Up"); btUp.setOnAction(e -> ballPane.moveUp()); Button btDown = new Button("Down"); btDown.setOnAction(e -> ballPane.moveDown()); Button btLeft = new Button("Left"); btLeft.setOnAction(e -> ballPane.moveLeft()); Button btRight = new Button("Right"); btRight.setOnAction(e -> ballPane.moveRight()); HBox buttons = new HBox(btUp, btDown, btLeft, btRight); buttons.setAlignment(Pos.BOTTOM_CENTER); buttons.setSpacing(10); buttons.setPadding(new Insets(10, 10, 10, 10)); BorderPane pane = new BorderPane(); pane.setCenter(ballPane); pane.setBottom(buttons); Scene scene = new Scene(pane, width, height); primaryStage.setScene(scene); primaryStage.setTitle("Click to move ball"); primaryStage.show(); } private class BallPane extends Pane {
  • 2. private Circle mCircle; public BallPane() { this(0, 0, 10); } public BallPane(double centerX, double centerY, double radius) { mCircle = new Circle(centerX, centerY, radius); getChildren().add(mCircle); } public void moveUp() { if (mCircle.getCenterY() - mCircle.getRadius() - 10 < 0) return; mCircle.setCenterY(mCircle.getCenterY() - 10); } public void moveDown() { if (mCircle.getCenterY() + mCircle.getRadius() + 10 > getHeight()) return; mCircle.setCenterY(mCircle.getCenterY() + 10); } public void moveRight() { if (mCircle.getCenterX() + mCircle.getRadius() + 10 > getWidth()) return; mCircle.setCenterX(mCircle.getCenterX() + 10); } public void moveLeft() { if (mCircle.getCenterX() - mCircle.getRadius() - 10 < 0) return; mCircle.setCenterX(mCircle.getCenterX() - 10); } } public static void main(String[] args) { Application.launch(args); } }