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

For the Following code in Java, I have to modify the code by making the ball big

ID: 3810561 • Letter: F

Question

For the Following code in Java, I have to modify the code by making the ball bigger and make the bounce slower.

How do I change the code?

Here is the code.

import java.awt.*;

importjava.awt.event.*;

import java.applet.*;

publicclassBall2extends Applet implements Runnable {

Image offscreenImage;

Graphics offscreenGraphics;

intx,y,dx,dy,diam,sizex,sizey,c,dc;

publicvoid init() {

    setBackground(Color.black);

x=y=0; dx=dy=1; diam=20; c=0; dc=1;

// Getting Parameters form the HTML

sizex=getSize().width;

sizey=getSize().height;

offscreenImage = createImage(sizex,sizey);

offscreenGraphics = offscreenImage.getGraphics();

// By running several threads the ball would go faster

    (new Thread(Ball2.this)).start();

    (new Thread(Ball2.this)).start();

    (new Thread(Ball2.this)).start();

    (new Thread(Ball2.this)).start();

    (new Thread(Ball2.this)).start();

}

publicvoid run() {

while (true) {

try {

Thread.currentThread().sleep(40);

      }

catch (InterruptedException e) {};

synchronized(this) {

x+=dx; y+=dy;

if ((x<=0)||(x+dx+diam>=sizex)) dx=-dx;

if ((y<=0)||(y+dy+diam>=sizey)) dy=-dy;

c+=dc;

if ((c==0)||(c==255)) dc=-dc;

      }

      repaint();

    }

}

publicvoid paint(Graphics g) {

offscreenGraphics.setColor(new Color(c,c,c));

offscreenGraphics.fillRect(0,0,sizex,sizey);

offscreenGraphics.setColor(new Color(255-c,255-c,255-c));

offscreenGraphics.fillArc(x,y,diam,diam,0,360);

// Converting double to string

offscreenGraphics.drawString(String.valueOf(x),20,20);

offscreenGraphics.drawString(String.valueOf(y),20,40);

offscreenGraphics.drawString(""+dx,80,20);

offscreenGraphics.drawString(""+dy,80,40);

g.drawImage(offscreenImage, 0, 0, this);

}

publicvoid update(Graphics g) {

    paint(g);

}

publicvoid destroy() {

offscreenGraphics.dispose();

}

}

Explanation / Answer

/*
Use diam variable to change the ball size
Change the sleep parameter in run method in order to vary the speed
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ball2 extends Applet implements Runnable {
Image offscreenImage;
Graphics offscreenGraphics;

int x,y,dx,dy,diam,sizex,sizey,c,dc;

public void init() {
setBackground(Color.black);
//Change the variable diameter to increase the size of the ball
x=y=0; dx=dy=1; diam=40; c=0; dc=1;

// Getting Parameters form the HTML
sizex=getSize().width;
sizey=getSize().height;

offscreenImage = createImage(sizex,sizey);
offscreenGraphics = offscreenImage.getGraphics();

// By running several threads the ball would go faster
(new Thread(Ball2.this)).start();
(new Thread(Ball2.this)).start();
(new Thread(Ball2.this)).start();
(new Thread(Ball2.this)).start();
(new Thread(Ball2.this)).start();
}

public void run() {
while (true) {
try {
//Increase the waiting time to move the ball from one position to the next in the below sleep parameter
Thread.currentThread().sleep(160);
}
catch (InterruptedException e) {};

synchronized(this) {
x+=dx; y+=dy;
if ((x<=0)||(x+dx+diam>=sizex)) dx=-dx;
if ((y<=0)||(y+dy+diam>=sizey)) dy=-dy;

c+=dc;
if ((c==0)||(c==255)) dc=-dc;
}

repaint();
}
}

public void paint(Graphics g) {

offscreenGraphics.setColor(new Color(c,c,c));
offscreenGraphics.fillRect(0,0,sizex,sizey);

offscreenGraphics.setColor(new Color(255-c,255-c,255-c));
offscreenGraphics.fillArc(x,y,diam,diam,0,360);

// Converting double to string
offscreenGraphics.drawString(String.valueOf(x),20,20);
offscreenGraphics.drawString(String.valueOf(y),20,40);
offscreenGraphics.drawString(""+dx,80,20);
offscreenGraphics.drawString(""+dy,80,40);

g.drawImage(offscreenImage, 0, 0, this);
}

public void update(Graphics g) {
paint(g);
}

public void destroy() {
offscreenGraphics.dispose();
}
}