Can i get help with these java questions about this java code. Main.java import
ID: 3735250 • Letter: C
Question
Can i get help with these java questions about this java code.
Main.java
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Main{
public static void main(String[] args) {
JFrame frame = new JFrame("Pong");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
PongPanel pongPanel = new PongPanel();
frame.add(pongPanel, BorderLayout.CENTER);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
PongPanel.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PongPanel extends JPanel implements ActionListener, KeyListener{
private boolean showTitleScreen = true;
private boolean playing = false;
private boolean gameOver = false;
private boolean upPressed = false;
private boolean downPressed = false;
private boolean wPressed = false;
private boolean sPressed = false;
private int ballX = 250;
private int ballY = 250;
private int diameter = 20;
private int ballDeltaX = -1;
private int ballDeltaY = 3;
private int playerOneX = 25;
private int playerOneY = 250;
private int playerOneWidth = 10;
private int playerOneHeight = 50;
private int playerTwoX = 465;
private int playerTwoY = 250;
private int playerTwoWidth = 10;
private int playerTwoHeight = 50;
private int paddleSpeed = 5;
private int playerOneScore = 0;
private int playerTwoScore = 0;
//construct a PongPanel
public PongPanel(){
setBackground(Color.BLACK);
//listen to key presses
setFocusable(true);
addKeyListener(this);
//call step() 60 fps
Timer timer = new Timer(1000/60, this);
timer.start();
}
public void actionPerformed(ActionEvent e){
step();
}
public void step(){
if(playing){
//move player 1
if (upPressed) {
if (playerOneY-paddleSpeed > 0) {
playerOneY -= paddleSpeed;
}
}
if (downPressed) {
if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
playerOneY += paddleSpeed;
}
}
//move player 2
if (wPressed) {
if (playerTwoY-paddleSpeed > 0) {
playerTwoY -= paddleSpeed;
}
}
if (sPressed) {
if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
playerTwoY += paddleSpeed;
}
}
//where will the ball be after it moves?
int nextBallLeft = ballX + ballDeltaX;
int nextBallRight = ballX + diameter + ballDeltaX;
int nextBallTop = ballY + ballDeltaY;
int nextBallBottom = ballY + diameter + ballDeltaY;
int playerOneRight = playerOneX + playerOneWidth;
int playerOneTop = playerOneY;
int playerOneBottom = playerOneY + playerOneHeight;
float playerTwoLeft = playerTwoX;
float playerTwoTop = playerTwoY;
float playerTwoBottom = playerTwoY + playerTwoHeight;
//ball bounces off top and bottom of screen
if (nextBallTop < 0 || nextBallBottom > getHeight()) {
ballDeltaY *= -1;
}
//will the ball go off the left side?
if (nextBallLeft < playerOneRight) {
//is it going to miss the paddle?
if (nextBallTop > playerOneBottom || nextBallBottom < playerOneTop) {
playerTwoScore ++;
if (playerTwoScore == 3) {
playing = false;
gameOver = true;
}
ballX = 250;
ballY = 250;
}
else {
ballDeltaX *= -1;
}
}
//will the ball go off the right side?
if (nextBallRight > playerTwoLeft) {
//is it going to miss the paddle?
if (nextBallTop > playerTwoBottom || nextBallBottom < playerTwoTop) {
playerOneScore ++;
if (playerOneScore == 3) {
playing = false;
gameOver = true;
}
ballX = 250;
ballY = 250;
}
else {
ballDeltaX *= -1;
}
}
//move the ball
ballX += ballDeltaX;
ballY += ballDeltaY;
}
//stuff has moved, tell this JPanel to repaint itself
repaint();
}
//paint the game screen
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
if (showTitleScreen) {
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString("Pong", 165, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press 'P' to play.", 175, 400);
}
else if (playing) {
int playerOneRight = playerOneX + playerOneWidth;
int playerTwoLeft = playerTwoX;
//draw dashed line down center
for (int lineY = 0; lineY < getHeight(); lineY += 50) {
g.drawLine(250, lineY, 250, lineY+25);
}
//draw "goal lines" on each side
g.drawLine(playerOneRight, 0, playerOneRight, getHeight());
g.drawLine(playerTwoLeft, 0, playerTwoLeft, getHeight());
//draw the scores
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
//draw the ball
g.fillOval(ballX, ballY, diameter, diameter);
//draw the paddles
g.fillRect(playerOneX, playerOneY, playerOneWidth, playerOneHeight);
g.fillRect(playerTwoX, playerTwoY, playerTwoWidth, playerTwoHeight);
}
else if (gameOver) {
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
g.drawString(String.valueOf(playerOneScore), 100, 100);
g.drawString(String.valueOf(playerTwoScore), 400, 100);
g.setFont(new Font(Font.DIALOG, Font.BOLD, 36));
if (playerOneScore > playerTwoScore) {
g.drawString("Player 1 Wins!", 165, 200);
}
else {
g.drawString("Player 2 Wins!", 165, 200);
}
g.setFont(new Font(Font.DIALOG, Font.BOLD, 18));
g.drawString("Press space to restart.", 150, 400);
}
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (showTitleScreen) {
if (e.getKeyCode() == KeyEvent.VK_P) {
showTitleScreen = false;
playing = true;
}
}
else if(playing){
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_W) {
wPressed = true;
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
sPressed = true;
}
}
else if (gameOver) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
gameOver = false;
showTitleScreen = true;
playerOneY = 250;
playerTwoY = 250;
ballX = 250;
ballY = 250;
playerOneScore = 0;
playerTwoScore = 0;
}
}
}
public void keyReleased(KeyEvent e) {
if (playing) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_W) {
wPressed = false;
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
sPressed = false;
}
}
}
}
1. How does g.drawLine work? (What are the 4 parameters for?) 2. They use g.fillOval. How is this different than g.drawOval? 3. What is the loop for in the section to draw the dashed line down center? 4. What is the Timer timer for? 5. Why is it important to have upPressed, wPressed, downPressed and sPressed as variables controlled by keyPressed and keyReleased? (hard question, give it some thought). keys and left with w and s. Correct this and include your corrected code snippet. code snippet. 6. The controls for the players are reversed. The right side should be controlled with the arrow 7. The game goes very slowly. Make the ball go faster. Correct this and include your corrected 8. In the middle of a game, I wanted to quit the game by clicking the ESC- Escape key. Implement 9. Find two ways that give a slight advantage to player #1. Implement this and include your code 10. HARD QUESTION-NO CODING: Drag the bottom of the frame up and down while the game is this and include the code that does this. that allows the cheats. playing. Notice the ball's vertical behavior. NOW, drag the right side of the frame right and left. Notice the ball's horizontal behavior. Explain why the paintComponent method produces this behavior. What would you have to change to make everything (the dashed line, ball movement, and player 2's paddle) shift along with the horizontal change in frame's size.Explanation / Answer
The answers for your questions are given below. Please check.
Ans: the drawLine method take 4 integer parameters, lets say x1,y1,x2 and y2. Here x1 is the x coordinate of starting point, y1 is the y coordinate of starting point, x2 is the x coordinate of ending point, y2 is the y coordinate of ending point.
Ans: drawOval method draws an oval whereas fillOval method draws the oval and fill it with the current color of the graphics
Ans:
for (int lineY = 0; lineY < getHeight(); lineY += 50) {
g.drawLine(250, lineY, 250, lineY+25);
}
This loop will draw dashed lines along the Y axis at the center , giving 25 pixel gap between each line until the value of lineY crosses the total height of the screen (bottom end).
Ans: The timer object will handle the animation of moving the ball in specified time interval, in this case, it will execute the step() method in 1000/60th of every seconds, which is called as the frame rate
Ans: This is because, keyPressed will handle the cases for press and hold of a key, whereas keyTyped will handle the case of the single click of a key. So if we use keyTyped() method, it will register only the single click, which will move the ‘rectangle’ of a player by one unit. If we want to move the rectangle box up and down, we will have to click repeatedly. Whereas if we use keyPressed like in the program, the box will move up/down until the pressed button is released.
Ans: This will need the below changes in step() method.
.
.
.
//move player 1
if (wPressed) {
if (playerOneY-paddleSpeed > 0) {
playerOneY -= paddleSpeed;
}
}
if (sPressed) {
if (playerOneY + paddleSpeed + playerOneHeight < getHeight()) {
playerOneY += paddleSpeed;
}
}
//move player 2
if (upPressed) {
if (playerTwoY-paddleSpeed > 0) {
playerTwoY -= paddleSpeed;
}
}
if (downPressed) {
if (playerTwoY + paddleSpeed + playerTwoHeight < getHeight()) {
playerTwoY += paddleSpeed;
}
}
.
.
.
Ans: We can do this by updating the ballDeltaX variable. It was -1, I have changed it to -2, which will double the speed.
private int ballDeltaX = -2;
Ans: For this functionality, add the below code to keyTyped() method
public void keyTyped(KeyEvent e) {
/**
* this will quit the program if ESC key is pressed
*/
char code=e.getKeyChar();
if(code==KeyEvent.VK_ESCAPE){
System.exit(0);
}
}
Ans: We can allow player1 to ‘cheat’ by providing him a ‘bigger’ rectangular box, in other way, by changing the playerOneHeight attribute.
private int playerOneHeight = 60;
Also, there is one more way : by allowing more paddleSpeed to playerOne. This may look a bit unclean, as both players use same paddleSpeed variable. We can make slight modifications in the step() method, to allow a +2 speed for playerOne only, like shown below.
//move player 1
if (wPressed) {
if (playerOneY-paddleSpeed+2 > 0) {
playerOneY -= paddleSpeed+2;
}
}
if (sPressed) {
if (playerOneY + paddleSpeed+2 + playerOneHeight < getHeight()) {
playerOneY += paddleSpeed+2;
}
}
Ans: This is because, in this program, hardcoded literals are used in many places, that is, the center position is always supposed to be (250,250) throughout the program, we can overcome this resizing issue by using getWidth()/2 as center X coordinate and getHeight()/2 as center Y coordinate, and replace it wherever we use 250