Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use python3, develops GUI application to to animate a bouncing ball. Ball will s

ID: 3604170 • Letter: U

Question

use python3, develops GUI application to to animate a bouncing ball. Ball will start near the top left corner of an 800 by 400 canvas. The ball will move in a 45 degrees, where it moves 2 pixels horizontally and 2 pixels vertically every 15 milliseconds. When the ball hits any of of the four edges of the canvas, it will bounce in the opposite direction. The angle of reflection is always equal to the angle of incidence. So, if the ball is moving south-east and it hits, let us say, the bottom edge, it will bounce in the north-east direction. It will be easier to consider the east-west (ie., right-left) movement as independent from the north-south (i.e, up-down movement). Below, you are given code to deal with the east-west movement and need to add code to deal with the north-south movement.

To help you with this lab, write the code in stages as follows and always make sure your code is working before you move to the next stage. You may also need to draw things down on a piece of paper to help you visualize things.

1.Create a GUI application with an 800 pixels by 400 pixels

2.Create a small ball starting near the top left corner of the canvas. You can either draw a small circle, say with diameter 20 pixels, or use an image of a small ball. Identify the ball (i.e, give it a tags value) so that you can refer to it later

3.Use two variables, say top_x and top_y, to track the ball's location. This is needed to figure out when the ball hits an edge and, therefore, should change direction

4.Use two variables to track the ball's horizonal and vertical directions. Initially, the ball moves east-south. So in my code, I used horizontal_direction and vertical_direction and set them as follows:

5.Insert code for the animation loop to move the ball 2 pixels down (i.e. south) and 2 pixels right (i.e., east) every 15 miliseconds. This code will be adjusted in steps 6 and 7.  

6.Use the idea in the following code and insert appropriate code in the animation loop from the previous step to take care of the ball's horizontal movement

Add code to the animation loop to take care of the ball's vertical movemont.

Explanation / Answer

import java.awt.*; import java.util.Formatter; import javax.swing.*; /** * One ball bouncing inside a rectangular box. * All codes in one file. Poor design! */ // Extends JPanel, so as to override the paintComponent() for custom rendering codes. public class BouncingBallSimple extends JPanel { // Container box's width and height private static final int BOX_WIDTH = 640; private static final int BOX_HEIGHT = 480; // Ball's properties private float ballRadius = 200; // Ball's radius private float ballX = ballRadius + 50; // Ball's center (x, y) private float ballY = ballRadius + 20; private float ballSpeedX = 3; // Ball's speed for x and y private float ballSpeedY = 2; private static final int UPDATE_RATE = 30; // Number of refresh per second /** Constructor to create the UI components and init game objects. */ public BouncingBallSimple() { this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT)); // Start the ball bouncing (in its own thread) Thread gameThread = new Thread() { public void run() { while (true) { // Execute one update step // Calculate the ball's new position ballX += ballSpeedX; ballY += ballSpeedY; // Check if the ball moves over the bounds // If so, adjust the position and speed. if (ballX - ballRadius < 0) { ballSpeedX = -ballSpeedX; // Reflect along normal ballX = ballRadius; // Re-position the ball at the edge } else if (ballX + ballRadius > BOX_WIDTH) { ballSpeedX = -ballSpeedX; ballX = BOX_WIDTH - ballRadius; } // May cross both x and y bounds if (ballY - ballRadius < 0) { ballSpeedY = -ballSpeedY; ballY = ballRadius; } else if (ballY + ballRadius > BOX_HEIGHT) { ballSpeedY = -ballSpeedY; ballY = BOX_HEIGHT - ballRadius; } // Refresh the display repaint(); // Callback paintComponent() // Delay for timing control and give other threads a chance try { Thread.sleep(1000 / UPDATE_RATE); // milliseconds } catch (InterruptedException ex) { } } } }; gameThread.start(); // Callback run() } /** Custom rendering codes for drawing the JPanel */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); // Paint background // Draw the box g.setColor(Color.BLACK); g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT); // Draw the ball g.setColor(Color.BLUE); g.fillOval((int) (ballX - ballRadius), (int) (ballY - ballRadius), (int)(2 * ballRadius), (int)(2 * ballRadius)); // Display the ball's information g.setColor(Color.WHITE); g.setFont(new Font("Courier New", Font.PLAIN, 12)); StringBuilder sb = new StringBuilder(); Formatter formatter = new Formatter(sb); formatter.format("Ball @(%3.0f,%3.0f) Speed=(%2.0f,%2.0f)", ballX, ballY, ballSpeedX, ballSpeedY); g.drawString(sb.toString(), 20, 30); } /** main program (entry point) */ public static void main(String[] args) { // Run GUI in the Event Dispatcher Thread (EDT) instead of main thread. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { // Set up main window (using Swing's Jframe) JFrame frame = new JFrame("A Bouncing Ball"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new BouncingBallSimple()); frame.pack(); frame.setVisible(true); } }); } }