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

QUESTION Create an application that provides a solution for problem 23.11 by mod

ID: 3671699 • Letter: Q

Question

QUESTION

Create an application that provides a solution for problem 23.11 by modifying the solution implemented from the BouncingBall.doc file (located in the "Project 6/Bouncing Ball Original Solution"). The modifications are as follows for the new solution.

Remove the "Runnable" interface from "BouncingBallPanel" class.

Add the "Runnable" interface to "Ball" class

Modify the "mouseClicked" method in the "BouncingBallPanel"class to start the thread after each ball is created. The "Ball" class must receive a reference to the "Bouncing BallPanel" (i.e., to call the "repaint" method)

Remove the "run" method from the "BouncingBallpanel" note. The "run" method must be implemented in the "Ball" class. The inner loop of the "run" method must be removed and the single "move" method call should remain. Also modify the method so that the delay for each ball is different (i.e., a way to simulate ball moving at different rates)

PLEASE CAN I GET THE SAMPLE OUT PUT TO THIS QUESTION.THANK YOU

PROBLEM 23.11

PROBLEM 23.11(Java how to Program (Early Objects) 10th Edition)

23.11 (Bouncing Balls) Modify the program in Exercise 23.10 to add a new ball each time the user
clicks the mouse. Provide for a minimum of 20 balls. Randomly choose the color for each new ball.

23.10 (Bouncing Ball) Write a program that bounces a blue ball inside a JPanel. The ball should
begin moving with a mousePressed event. When the ball hits the edge of the JPanel, it should bounce
off the edge and continue in the opposite direction. The ball should be updated using a Runnable.

Explanation / Answer

BouncingBall.java


import javax.swing.JFrame;

public class BouncingBall {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       BouncingBallFrame bouncingBallFrame = new BouncingBallFrame();
       bouncingBallFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       bouncingBallFrame.setSize(500, 500);
       bouncingBallFrame.setVisible(true);
       //test
   }

}

BouncingBallFrame.java

import java.awt.GridLayout;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFrame;


public class BouncingBallFrame extends JFrame {
   private BouncingBallPanel paneel;
  
   public BouncingBallFrame() {
       // TODO Auto-generated constructor stub
       BouncingBallRun bal1;
      
      
       paneel = new BouncingBallPanel();
       bal1 = new BouncingBallRun(paneel);
       setLayout(new GridLayout(1, 1));
       add(paneel);
       ExecutorService exec = Executors.newCachedThreadPool();
       exec.execute(bal1);
       exec.shutdown();
      
      
  
  
      
   }
}

BouncingBallPanel.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JPanel;

public class BouncingBallPanel extends JPanel {
   private BouncingBallRun bal;
   private int[] ax = new int[100];
   private int[] ay = new int[100];
   private int[] deltax = new int[100];
   private int[] deltay = new int[100];
   private Color[] kleur = new Color[100];
   private int diameter = 40;
   private int aantal = 0;
   private Random r = new Random();
   private float shadow;
  
   public BouncingBallPanel() {
       // TODO Auto-generated constructor stub
       MouseHandler handler = new MouseHandler();
       addMouseListener(handler);
   }
  
   @Override
   public void paintComponent(Graphics g) {
       super.paintComponent(g);
       //System.out.println("tekenen");
      
       for (int i = 0; i < aantal; i++) {
           g.setColor(Color.LIGHT_GRAY);
          
           g.fillOval(ax[i], this.getHeight() - 10, diameter, 10);
       }
      
       for (int i = 0; i < aantal; i++) {
          
           g.setColor(kleur[i]);
           g.fillOval(ax[i], ay[i], diameter, diameter);
           g.setColor(new Color(255 - kleur[i].getRed(), 255 - kleur[i].getGreen(), 255 - kleur[i].getBlue()));
           g.drawString(String.valueOf(i), ax[i] + diameter / 2 - 5, ay[i] + 25 );
          
       }
      
   }
  
   public void moveBall() {
      
       for (int i = 0; i < aantal; i++) {
           ax[i] += deltax[i];
           ay[i] += deltay[i];
          
           if (ax[i] >= this.getWidth() - diameter) {
               deltax[i] = - r.nextInt(6);
           }
           if (ay[i] >= this.getHeight() - diameter) {
               deltay[i] = - r.nextInt(6);
           }
           if (ax[i] <= 0) {
               deltax[i] = r.nextInt(6);
           }
           if (ay[i] <= 0) {
               deltay[i] = r.nextInt(6);
           }
           //System.out.println(ax[i]);
       }
       this.repaint();
   }
  
   public void addball(int x, int y) {
       if (aantal < 100) {
           ax[aantal] = x;
           ay[aantal] = y;
           do {
               deltax[aantal] = r.nextInt(12) - 6;
               System.out.println(deltax[aantal]);
               deltay[aantal] = r.nextInt(12) - 6;
               System.out.println(deltay[aantal]);
           } while (deltax[aantal] == 0 && deltay[aantal] == 0);
           kleur[aantal] = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
           aantal += 1;
       }
   }
  
   private class MouseHandler implements MouseListener {

       @Override
       public void mouseClicked(MouseEvent e) {
           // TODO Auto-generated method stub
          
       }

       @Override
       public void mouseEntered(MouseEvent e) {
           // TODO Auto-generated method stub
          
       }

       @Override
       public void mouseExited(MouseEvent e) {
           // TODO Auto-generated method stub
          
       }

       @Override
       public void mousePressed(MouseEvent e) {
           // TODO Auto-generated method stub
           addball(e.getX(), e.getY());
       }

       @Override
       public void mouseReleased(MouseEvent e) {
           // TODO Auto-generated method stub
          
       }
      
   }
}

BouncingBallRun.java
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;

public class BouncingBallRun implements Runnable {

   private BouncingBallPanel paneel;
   public BouncingBallRun(BouncingBallPanel panel) {
       // TODO Auto-generated constructor stub
       paneel = panel;
   }
  
   @Override
   public void run() {
       // TODO Auto-generated method stub
       while (true) {
           paneel.moveBall();
           try {
               Thread.sleep(25);
           } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
   }

}