Liam Maloney Liam Maloney - 2 months ago 13
Java Question

Java Graphics Rectangle Size Changing

I have just started to learn Java, so excuse my messy code. Looking all over the internet, I have not found a solution to this question yet. This is the game pong. I have to padels that I am controlling ith the keys, 'w', 's', up, and down. When I draw the panels in my graphics section, I use the padelY for Y1 and use padelY + padelLength(A constant) for Y2. I cannot seem to understand why when I rut the program the padels change size as I move them up and down. Also, the padel on the right is named panel1, when I draw the padel I use the screen size - the padel spacing for X1 yet the padel appears to be stuck on the side.

I would really appreciate any advice that anyone could give on those issues or how to clean up my code in general!

Thanks!

package Pong;

//Import Libraries
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class Pong extends JPanel implements KeyListener{

int padel1y = 150;
int padel2y = 150;

static final int padelLength = 100;

int padelWidth = 10;
int padelSpeed = 2;

int padel2Speed = 0;
int padel1Speed = 0;

int ballX = BOX_WIDTH/2;
int ballY= BOX_HEIGHT/2;
int ballXSpeed = 2;
int ballYSpeed = 1;
int ballRadius = 10;
int ballCount = 0;

public final static int ballSpeedIncrease = 500;
public final static int BOX_WIDTH = 600;
public final static int BOX_HEIGHT = 600;
public final static int UPDATE_RATE = 100;
public final static int padelSpace = 10;

public Pong() {

setPreferredSize(new Dimension(BOX_WIDTH,BOX_HEIGHT));

Thread gameThread = new Thread() {

public void run(){

while(true){

ballCount = ballCount + 1;

if(ballCount%ballSpeedIncrease == 0){

if(ballXSpeed < 0){
ballXSpeed = ballXSpeed - 1;
}
if(ballYSpeed < 0){
ballYSpeed = ballYSpeed - 1;
}
if(ballXSpeed > 0){
ballXSpeed++;
}
if(ballYSpeed > 0){
ballYSpeed++;
}

}

if(ballX-ballRadius <= 10+padelWidth){

if(ballY<padel2y || ballY>padel2y+padelLength){
System.exit(0);
}
else{
ballXSpeed = -ballXSpeed;
}

}

else if(ballX+ballRadius >= 590-padelWidth){

if(ballY<padel1y || ballY>padel1y+padelLength){
System.exit(0);
}
else{
ballXSpeed = -ballXSpeed;
}

}

if(ballY-ballRadius <= 0){

ballYSpeed = -ballYSpeed;

}

else if(ballY+ballRadius >= BOX_HEIGHT){

ballYSpeed = -ballYSpeed;

}

padel1y = padel1y +padel1Speed;
padel2y = padel2y +padel2Speed;
ballX = ballX + ballXSpeed;
ballY = ballY+ ballYSpeed;

repaint();

try {

Thread.sleep(1000 / UPDATE_RATE);

}

catch (InterruptedException ex) {}

}

}

};

gameThread.start();

}

@Override
public void paintComponent(Graphics g) {

super.paintComponent(g);

g.setColor(Color.white);
g.fillRect(0,0,BOX_WIDTH,BOX_HEIGHT);

g.setColor(Color.blue);
g.fillRect(padelSpace,padel2y,padelSpace + padelWidth,padel2y+padelLength);
g.fillRect(BOX_WIDTH-padelSpace,padel1y,BOX_WIDTH-padelSpace-padelWidth,padel1y+padelLength);

g.setColor(Color.green);
g.fillOval(ballX, ballY, ballRadius*2, ballRadius*2);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

//Create Frame
JFrame frame = new JFrame("PONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONGPONG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Pong pong = new Pong();
frame.setContentPane(pong);
frame.setSize(BOX_WIDTH,BOX_HEIGHT);
frame.pack();
frame.addKeyListener(pong);
frame.setVisible(true);

}

});

}

public void keyPressed(KeyEvent e){

if(e.getKeyCode() == KeyEvent.VK_UP){

padel1Speed = -padelSpeed;

}

else if(e.getKeyCode() == KeyEvent.VK_DOWN){

padel1Speed = padelSpeed;

}

else if(e.getKeyCode() == KeyEvent.VK_W){

padel2Speed = -padelSpeed;

}

else if(e.getKeyCode() == KeyEvent.VK_S){

padel2Speed = padelSpeed;

}

}

@Override
public void keyReleased(KeyEvent e) {

if(e.getKeyCode() == KeyEvent.VK_UP){

padel1Speed = 0;

}

else if(e.getKeyCode() == KeyEvent.VK_DOWN){

padel1Speed = 0;

}

else if(e.getKeyCode() == KeyEvent.VK_W){

padel2Speed = 0;

}

else if(e.getKeyCode() == KeyEvent.VK_S){

padel2Speed = 0;

}

}

@Override
public void keyTyped(KeyEvent e) {}


}

Answer

See this for documentation on how to use the fillRect() method: https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillRect(int,%20int,%20int,%20int)

The arguments are: x position, y position, width and height.

You use it like this: g.fillRect(padelSpace,padel2y,padelSpace + padelWidth,padel2y+padelLength);

The second and third arguments are wrong. You should only be passing the width and height. Like this: g.fillRect(padelSpace, padel2y, padelWidth, padelLength);

More generally, when something is going wrong (drawing the paddle) read the documentation on the methods you use to do that.

Comments