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

Can someone comment this code for me, This is Paddle.java class for a Ping Pong

ID: 3677253 • Letter: C

Question

Can someone comment this code for me, This is Paddle.java class for a Ping Pong game:

/**
* @authors: Hunter Lai, Isaac Fehr, Raghad Marta
* @about: Class that creates a paddle and accounts for it's moving activity
*/

import java.awt.event.KeyListener;
import objectdraw.DrawingCanvas;
import objectdraw.FilledRect;

public class Paddle extends Thread implements KeyListener {
private final double START_HEIGHT = .15;
private final double START_WIDTH = .01;

private final double INCREASE_FACTOR = .2;
private final double DECREASE_FACTOR = .2;
private final double MAX_HEIGHT_FACT = .5;
private final double MIN_HEIGHT_FACT = .1;

private int PADDLE_HEIGHT, PADDLE_WIDTH;
  
private int x, y;
public FilledRect paddle;
private DrawingCanvas canvas;

private char upKey;
private char downKey;
private int dir;

private final double SPEED = 1;
  
public Paddle(char side, DrawingCanvas canvas, char up, char down){
PADDLE_WIDTH = (int) ((double)canvas.getWidth() * START_WIDTH);
PADDLE_HEIGHT = (int)((double)canvas.getHeight()* START_HEIGHT);
y = canvas.getHeight() / 2;

if(side == 'l'){
x = (int)(0.05 * canvas.getWidth());
}
else{
x = canvas.getWidth() - PADDLE_WIDTH - (int)(0.05 * canvas.getWidth());
}
  
paddle = new FilledRect(x, y, PADDLE_WIDTH, PADDLE_HEIGHT, canvas);

this.canvas = canvas;
this.canvas.addKeyListener(this);
this.canvas.requestFocus();
  
upKey = up;
downKey = down;
dir = 0;
}
  
public void increase() {
int newHeight = (int) (PADDLE_HEIGHT * (1 + INCREASE_FACTOR));
if(newHeight < (int)(canvas.getHeight() * MAX_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void decrease() {
int newHeight = (int) (PADDLE_HEIGHT * (1 - DECREASE_FACTOR));
  
if(newHeight > (int)(canvas.getHeight() * MIN_HEIGHT_FACT)) {
PADDLE_HEIGHT = newHeight;
paddle.setHeight(PADDLE_HEIGHT);
}
}
  
public void vibrate() {
for(int i = 0; i< 5; i++) {
paddle.move(5,5);
  
try {
this.sleep(100);
} catch(Exception e) {
e.printStackTrace();
}
  
paddle.move(-5, -5);
}
}
  
public void run() {
while(true){
paddle.move(0, dir * (SPEED * (double)canvas.getHeight()) * ((double)10 / 1000.0));
  
if(paddle.getY() < 0){
paddle.move(0, paddle.getY() * -1);
}

else if(paddle.getY() + PADDLE_HEIGHT > canvas.getHeight()){
paddle.move(0, canvas.getHeight() - (paddle.getY() + paddle.getHeight()));
}
  
TrippyPong.catchSleep(10);
}
}

@Override
public void keyPressed(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();

if(key == upKey){
dir = -1;
}
  
else if(key == downKey){
dir = 1;
}
}

@Override
public void keyReleased(java.awt.event.KeyEvent e) {
char key = e.getKeyChar();
  
if(key == upKey && dir == -1){
dir = -0;
}
  
else if(key == downKey && dir == 1){
dir = 0;
}
}

@Override
public void keyTyped(java.awt.event.KeyEvent e) {
}

public int getX() {
   return x;
}
}

2) THIS IS THE MAIN CLASS:

/**
* @authors: Hunter Lai, Isaac Fehr, Raghad Marta
* @about: Class that creates the TrippyPong game and adds
* the components to the canvas. Also keeps track of score.
*/


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import objectdraw.*;

public class TrippyPong extends WindowController{
  
private static DrawingCanvas myCanvas;
  
private static final int START_SIZE = 1000;
private static final int MENU_BAR_SIZE = 50;
private static final double TEXT_Y = .20;
private static final double TEXT_X = .25;
private static final int TEXT_SIZE = 60;
  
private static final int WIN_SCORE = 5;
private static int winnerPlayer;
private static Text p1Text, p2Text;
private static int p1Score, p2Score;
  
private static Paddle player1;
private static Paddle player2;
private static Ball mainBall;
private static VisibleImage scaryImage;
private static TripMeter tripMeter;
  
public static TrippyPong trippyPong;
  
public void begin(){
   myCanvas = canvas;  
      
   tripMeter = new TripMeter(myCanvas, this);
   tripMeter.start();
  
   player1 = new Paddle('l', myCanvas, 'w', 's');
   player1.start();
   player2 = new Paddle('r', myCanvas, 'i', 'k');
   player2.start();
  
   p1Score = 0;
   p2Score = 0;
   winnerPlayer = 0;
  
   p1Text = new Text(p1Score, myCanvas.getWidth() / 2 - myCanvas.getWidth() * TEXT_X, myCanvas.getHeight() * TEXT_Y, myCanvas);
   p1Text.setFontSize(TEXT_SIZE);
   p2Text = new Text(p2Score, myCanvas.getWidth() / 2 + myCanvas.getWidth() * TEXT_X, myCanvas.getHeight() * TEXT_Y, myCanvas);
   p2Text.setFontSize(TEXT_SIZE);
  
   mainBall = new Ball(canvas.getWidth() / 2, canvas.getHeight() / 2, myCanvas, player1, player2, 2, 2, tripMeter, this);
   mainBall.start();
  
   //   mainBall.setRandomColor(true); //puts the flashing rainbow thing on and off
   //   mainBall.setSizeChange(true);
}

public static void catchSleep(int time) {
   try {
   Thread.sleep(time);
   } catch (InterruptedException e) {}
}
  
public void score(int player){
   if(player == 1){
p1Score++;
   p1Text.setText(p1Score);
player1.increase();
player2.decrease();
player2.vibrate();      
}
  
   if(player == 2){
p2Score++;
   p2Text.setText(p2Score);
player2.increase();
player1.decrease();
player1.vibrate();
}
   if(p1Score >= WIN_SCORE){
       winnerPlayer = 1;
   }
   else if(p2Score >= WIN_SCORE){
       winnerPlayer = 2;
   }
   if(!(winnerPlayer == 0)){
   Text winText = new Text("PLAYER 1 WINS!", myCanvas.getWidth() / 2, myCanvas.getHeight() / 2, myCanvas);
   catchSleep(1000);
   myCanvas.clear();
          
   BufferedImage img = null;
   try{
       URL url = new URL("https://i.ytimg.com/vi/a_FP2KJH6Jk/hqdefault.jpg");
       img = ImageIO.read(url);      
   }catch (IOException e){
   }
  
scaryImage = new VisibleImage(img,0,0, START_SIZE, START_SIZE, myCanvas);
}
}
  
public static void main(String[] args) {
   TrippyPong trippyPong = new TrippyPong();
   trippyPong.startController(START_SIZE, START_SIZE + MENU_BAR_SIZE);
}
}

Explanation / Answer

* * File: Paddle.java * Author: Java, Java, Java * Description: This class represents a paddle for the Pong game. * The paddle is a vertical line segment located at the right boundary. * Its motion is controlled by the user via up/down arrow keys. Therefore * the paddle needs a reference to the applet. The Paddle class contains * move() and draw() methods. */ public class Paddle { public static final int HEIGHT = 50; // Size of Paddle public static final int WIDTH = 10; // Size of Paddle private static final int DELTA = HEIGHT/2; // Size of each move private static final int BORDER = 0; private int gameAreaHeight; private int locationX, locationY; private PongApplet applet; /** * Paddle() constructor is passed a reference to the applet, which it * uses to determine the dimensions of the game area. */ public Paddle (PongApplet a) { applet = a; gameAreaHeight = a.getHeight(); locationX = a.getWidth()-WIDTH; locationY = gameAreaHeight/2; } // Paddle() public void resetLocation() { gameAreaHeight = applet.getHeight(); locationX = applet.getWidth()-WIDTH; } /** * Return the x coordinate of the paddle's location */ public int getX() { return locationX; } /** * Return the x coordinate of the paddle's location */ public int getY() { return locationY; } /** * moveUp() moves the paddle up by a fixed DELTA */ public void moveUp () { if (locationY > BORDER ) locationY -= DELTA; } // moveUp() /** * moveDown() moves the paddle down by a fixed DELTA */ public void moveDown() { if (locationY + HEIGHT