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

Imporove the below code in Java (ping pong game) so that there is a begining and

ID: 3728705 • Letter: I

Question

Imporove the below code in Java (ping pong game) so that there is a begining and end of game. Set the limit for the score, so that when a certain number (i.e. 10) is reached, the game resets.

Please makre the game landscape.

Optimize the code so the game looks professional.

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;

import java.util.HashMap;

import java.util.HashSet;

import javax.swing.*;

public class Game extends JPanel implements KeyListener, ActionListener {

  

   private int height, width;

   private Timer t = new Timer(5, this);

   private boolean first;

  

   private HashSet<String> keys = new HashSet<String>();

  

   // Setting up the initial state

   // pad

   private final int SPEED = 1; // set the spped of the computer pad to 1

   private int padH = 10, padW = 40; // set padding width and height to 10 and 40, respectively.

   private int bottomPadX, topPadX;

   private int inset = 10;

  

   // ball

   // Setting the ball size to 20, ball velocity to 1 along X and Y axis.

   private double ballX, ballY, velX = 1, velY = 1, ballSize = 20;

  

   // score

   private int scoreTop, scoreBottom;

  

   public Game() {

       addKeyListener(this);

       setFocusable(true);

       setFocusTraversalKeysEnabled(false);

       first = true;

       t.setInitialDelay(100);

       t.start();

   }

  

   @Override

   protected void paintComponent(Graphics g) {

       super.paintComponent(g);

       Graphics2D g2d = (Graphics2D) g;

       height = getHeight();

       width = getWidth();

       // initial positioning

       if (first) {

           bottomPadX = width / 2 - padW / 2;

           topPadX = bottomPadX;

           ballX = width / 2 - ballSize / 2;

           ballY = height / 2 - ballSize / 2;

           first = false;

       }

      

       // bottom pad

       Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH);

       g2d.fill(bottomPad);

      

       // top pad

       Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH);

       g2d.fill(topPad);

      

       // ball

       Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize);

       g2d.fill(ball);

      

       // scores

       // Utility – gives the payoff with numeric values for each player

       // Updating the score of the Top and the Bottom player

       String scoreB = "Bottom: " + new Integer(scoreBottom).toString();

       String scoreT = "Top: " + new Integer(scoreTop).toString();

      

       g2d.drawString(scoreB, 10, height / 2);

       g2d.drawString(scoreT, width - 50, height / 2);

   }

  

   @Override

   public void actionPerformed(ActionEvent e) {

       // Results – determines the outcome of a move

       // side walls

       if (ballX < 0 || ballX > width - ballSize) {

           velX = -velX;

       }

       // top / down walls

       if (ballY < 0) {

           velY = -velY;

           ++ scoreBottom;

       }

      

       if (ballY + ballSize > height) {

           velY = -velY;

           ++ scoreTop;

       }

       // bottom pad

       if (ballY + ballSize >= height - padH - inset && velY > 0)

           if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW)

               velY = -velY;

       // top pad

       if (ballY <= padH + inset && velY < 0)

           if (ballX + ballSize >= topPadX && ballX <= topPadX + padW)

               velY = -velY;

       ballX += velX;

       ballY += velY;

      

       // pressed keys

       if (keys.size() == 1) {

           if (keys.contains("LEFT")) {

               bottomPadX -= (bottomPadX > 0) ? SPEED : 0;

           }

           else if (keys.contains("RIGHT")) {

               bottomPadX += (bottomPadX < width - padW) ? SPEED : 0;

           }

       }

      

       // AI

       double delta = ballX - topPadX;

       if (delta > 0) {

           topPadX += (topPadX < width - padW) ? SPEED : 0;

       }

       else if (delta < 0) {

           topPadX -= (topPadX > 0) ? SPEED : 0;

       }

      

       repaint();

   }

   @Override

   public void keyTyped(KeyEvent e) {}

   @Override

   public void keyPressed(KeyEvent e) {

       int code = e.getKeyCode();

       // Actions – returns the set of legal moves in a state

       switch (code) {

       case KeyEvent.VK_LEFT:

           keys.add("LEFT");

           break;

       case KeyEvent.VK_RIGHT:

           keys.add("RIGHT");

           break;

       }

   }

   @Override

   public void keyReleased(KeyEvent e) {

       int code = e.getKeyCode();

       // Actions – returns the set of legal moves in a state

       switch (code) {

       case KeyEvent.VK_LEFT:

           keys.remove("LEFT");

           break;

       case KeyEvent.VK_RIGHT:

           keys.remove("RIGHT");

           break;

       }

   }

  

   public static void main(String[] args) {

       JFrame frm = new JFrame();

       frm.setTitle("Pong");

       Game g = new Game();

       frm.setContentPane(g);

       frm.setSize(300, 700);

       frm.setResizable(false);

       frm.setVisible(true);

       frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   }

}

Explanation / Answer

Hi ,

As per the problem statement, Please find the optimized the solution with , adding additional steps as your problem statement expecting like

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;

import java.util.HashSet;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

public class Game extends JPanel implements KeyListener, ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

private static final int SCORE_RANGE = 10; //Threshold of score

private int height, width;

private Timer t = new Timer(3, this);

private boolean first;

private HashSet<String> keys = new HashSet<String>();

// Setting up the initial state

// pad

private final int SPEED = 1; // set the spped of the computer pad to 1

private int padH = 10, padW = 50; // set padding width and height to 10 and

// 40, respectively.

private int bottomPadX, topPadX;

private int inset = 10;

// ball

// Setting the ball size to 20, ball velocity to 1 along X and Y axis.

private double ballX, ballY, velX = 1, velY = 1, ballSize = 20;

// score

private int scoreTop, scoreBottom;

public Game() {

addKeyListener(this);

setFocusable(true);

setFocusTraversalKeysEnabled(false);

first = true;

t.setInitialDelay(200);

t.start();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

height = getHeight();

width = getWidth();

// initial positioning

if (first) {

bottomPadX = width / 2 - padW / 2;

topPadX = bottomPadX;

ballX = width / 2 - ballSize / 2;

ballY = height / 2 - ballSize / 2;

first = false;

}

// bottom pad

Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH);

g2d.fill(bottomPad);

// top pad

Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH);

g2d.fill(topPad);

// ball

Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize);

g2d.fill(ball);

// scores

// Utility – gives the payoff with numeric values for each player

// Updating the score of the Top and the Bottom player

String scoreB = "Bottom: " + new Integer(scoreBottom).toString();

String scoreT = "Top: " + new Integer(scoreTop).toString();

g2d.drawString(scoreB, 10, height / 2);

g2d.drawString(scoreT, width - 50, height / 2);

if(scoreTop == SCORE_RANGE) {

g2d.drawString("Match won by Top", 20, height/4);

callRepaint();

}

if(scoreBottom == SCORE_RANGE) {

g2d.drawString("Match won by Bottom", 20, height/4);

callRepaint();

}

}

/**

* Implemented the callRepaint method will existing Jframe object to reset and start the game again

*

*/

private void callRepaint() {

frm.setTitle("Pong");

Game g = new Game();

frm.setContentPane(g);

frm.setSize(300, 700);

frm.setResizable(true);

frm.setVisible(true);

frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e) {

// Results – determines the outcome of a move

// side walls

if (ballX < 0 || ballX > width - ballSize) {

velX = -velX;

}

// top / down walls

if (ballY < 0) {

velY = -velY;

++scoreBottom;

if(scoreBottom == SCORE_RANGE) {

new Game();

}

}

if (ballY + ballSize > height) {

velY = -velY;

++scoreTop;

if(scoreTop == SCORE_RANGE) {

new Game();

}

}

// bottom pad

if (ballY + ballSize >= height - padH - inset && velY > 0)

if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW)

velY = -velY;

// top pad

if (ballY <= padH + inset && velY < 0)

if (ballX + ballSize >= topPadX && ballX <= topPadX + padW)

velY = -velY;

ballX += velX;

ballY += velY;

// pressed keys

if (keys.size() == 1) {

if (keys.contains("LEFT")) {

bottomPadX -= (bottomPadX > 0) ? SPEED : 0;

}

else if (keys.contains("RIGHT")) {

bottomPadX += (bottomPadX < width - padW) ? SPEED : 0;

}

}

// AI

double delta = ballX - topPadX;

if (delta > 0) {

topPadX += (topPadX < width - padW) ? SPEED : 0;

}

else if (delta < 0) {

topPadX -= (topPadX > 0) ? SPEED : 0;

}

repaint();

}

@Override

public void keyTyped(KeyEvent e) {

}

@Override

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

// Actions – returns the set of legal moves in a state

switch (code) {

case KeyEvent.VK_LEFT:

keys.add("LEFT");

break;

case KeyEvent.VK_RIGHT:

keys.add("RIGHT");

break;

}

}

@Override

public void keyReleased(KeyEvent e) {

int code = e.getKeyCode();

// Actions – returns the set of legal moves in a state

switch (code) {

case KeyEvent.VK_LEFT:

keys.remove("LEFT");

break;

case KeyEvent.VK_RIGHT:

keys.remove("RIGHT");

break;

}

}

public static JFrame frm = new JFrame();

public static void main(String[] args) {

frm.setTitle("Pong");

Game g = new Game();

frm.setContentPane(g);

frm.setSize(300, 700);

frm.setResizable(true);

frm.setVisible(true);

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Hope output , you check by running the program, because i cann't put upload all screens.

Thanks

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.geom.Ellipse2D;

import java.awt.geom.Rectangle2D;

import java.util.HashSet;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.Timer;

public class Game extends JPanel implements KeyListener, ActionListener {

/**

*

*/

private static final long serialVersionUID = 1L;

private static final int SCORE_RANGE = 10; //Threshold of score

private int height, width;

private Timer t = new Timer(3, this);

private boolean first;

private HashSet<String> keys = new HashSet<String>();

// Setting up the initial state

// pad

private final int SPEED = 1; // set the spped of the computer pad to 1

private int padH = 10, padW = 50; // set padding width and height to 10 and

// 40, respectively.

private int bottomPadX, topPadX;

private int inset = 10;

// ball

// Setting the ball size to 20, ball velocity to 1 along X and Y axis.

private double ballX, ballY, velX = 1, velY = 1, ballSize = 20;

// score

private int scoreTop, scoreBottom;

public Game() {

addKeyListener(this);

setFocusable(true);

setFocusTraversalKeysEnabled(false);

first = true;

t.setInitialDelay(200);

t.start();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g;

height = getHeight();

width = getWidth();

// initial positioning

if (first) {

bottomPadX = width / 2 - padW / 2;

topPadX = bottomPadX;

ballX = width / 2 - ballSize / 2;

ballY = height / 2 - ballSize / 2;

first = false;

}

// bottom pad

Rectangle2D bottomPad = new Rectangle(bottomPadX, height - padH - inset, padW, padH);

g2d.fill(bottomPad);

// top pad

Rectangle2D topPad = new Rectangle(topPadX, inset, padW, padH);

g2d.fill(topPad);

// ball

Ellipse2D ball = new Ellipse2D.Double(ballX, ballY, ballSize, ballSize);

g2d.fill(ball);

// scores

// Utility – gives the payoff with numeric values for each player

// Updating the score of the Top and the Bottom player

String scoreB = "Bottom: " + new Integer(scoreBottom).toString();

String scoreT = "Top: " + new Integer(scoreTop).toString();

g2d.drawString(scoreB, 10, height / 2);

g2d.drawString(scoreT, width - 50, height / 2);

if(scoreTop == SCORE_RANGE) {

g2d.drawString("Match won by Top", 20, height/4);

callRepaint();

}

if(scoreBottom == SCORE_RANGE) {

g2d.drawString("Match won by Bottom", 20, height/4);

callRepaint();

}

}

/**

* Implemented the callRepaint method will existing Jframe object to reset and start the game again

*

*/

private void callRepaint() {

frm.setTitle("Pong");

Game g = new Game();

frm.setContentPane(g);

frm.setSize(300, 700);

frm.setResizable(true);

frm.setVisible(true);

frm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}

@Override

public void actionPerformed(ActionEvent e) {

// Results – determines the outcome of a move

// side walls

if (ballX < 0 || ballX > width - ballSize) {

velX = -velX;

}

// top / down walls

if (ballY < 0) {

velY = -velY;

++scoreBottom;

if(scoreBottom == SCORE_RANGE) {

new Game();

}

}

if (ballY + ballSize > height) {

velY = -velY;

++scoreTop;

if(scoreTop == SCORE_RANGE) {

new Game();

}

}

// bottom pad

if (ballY + ballSize >= height - padH - inset && velY > 0)

if (ballX + ballSize >= bottomPadX && ballX <= bottomPadX + padW)

velY = -velY;

// top pad

if (ballY <= padH + inset && velY < 0)

if (ballX + ballSize >= topPadX && ballX <= topPadX + padW)

velY = -velY;

ballX += velX;

ballY += velY;

// pressed keys

if (keys.size() == 1) {

if (keys.contains("LEFT")) {

bottomPadX -= (bottomPadX > 0) ? SPEED : 0;

}

else if (keys.contains("RIGHT")) {

bottomPadX += (bottomPadX < width - padW) ? SPEED : 0;

}

}

// AI

double delta = ballX - topPadX;

if (delta > 0) {

topPadX += (topPadX < width - padW) ? SPEED : 0;

}

else if (delta < 0) {

topPadX -= (topPadX > 0) ? SPEED : 0;

}

repaint();

}

@Override

public void keyTyped(KeyEvent e) {

}

@Override

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

// Actions – returns the set of legal moves in a state

switch (code) {

case KeyEvent.VK_LEFT:

keys.add("LEFT");

break;

case KeyEvent.VK_RIGHT:

keys.add("RIGHT");

break;

}

}

@Override

public void keyReleased(KeyEvent e) {

int code = e.getKeyCode();

// Actions – returns the set of legal moves in a state

switch (code) {

case KeyEvent.VK_LEFT:

keys.remove("LEFT");

break;

case KeyEvent.VK_RIGHT:

keys.remove("RIGHT");

break;

}

}

public static JFrame frm = new JFrame();

public static void main(String[] args) {

frm.setTitle("Pong");

Game g = new Game();

frm.setContentPane(g);

frm.setSize(300, 700);

frm.setResizable(true);

frm.setVisible(true);

frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}