So I'm making a pong game that's almost done, but the one problem I have is that the JLabel that should display the score is in the main class on the JFrame and I need to change the value of it in the PongDrawer class. How can I do this since I can't make a public variable in a main class?
Here's my code. The score variables are p1Score and p2Score in the PongDrawer class, and the JLabel that's supposed to display them is called scoreLabel and it's in the main class.
Btw for some reason the comment formatting got messed up just ignore that lol.
Main class:
import java.awt.*; import javax.swing.*; @SuppressWarnings("serial") public class PongMain extends JPanel{ //------------------------------------------------------ //Creates the frame in which to play the game //------------------------------------------------------ @SuppressWarnings("deprecation") public static void main(String[] args) { JFrame table = new JFrame("Pong"); table.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); table.setBackground(Color.GRAY); PongDrawer game = new PongDrawer(); table.add(game); game.setBackground(Color.GRAY); table.getContentPane().add(game); JPanel scorePanel = new JPanel(); scorePanel.setSize(700, 50); scorePanel.setBackground(Color.GRAY); table.add(scorePanel, BorderLayout.NORTH); JLabel scoreLabel = new JLabel("score"); scorePanel.add(scoreLabel); table.pack(); table.resize(700,500); table.setResizable(false); table.setVisible(true); } }
PongDrawer class:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; @SuppressWarnings("serial") public class PongDrawer extends PongMain implements ActionListener, KeyListener { boolean started = false; boolean upRight = true; boolean downRight = false; boolean upLeft = false; boolean downLeft = false; Timer ballTimer; int ballSpeed = 50; int p1Y = 200; int p2Y = 200; int bX = 350; int bY = 220; int p1Score = 0; int p2Score = 0; Shape paddle1; Shape paddle2; Shape ball; Shape top; Shape bottom; public PongDrawer(){ setFocusable(true); setFocusTraversalKeysEnabled(false); addKeyListener(this); ballTimer = new Timer(ballSpeed, this); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; paddle1 = new Rectangle2D.Double(0, p1Y, 5, 50); paddle2 = new Rectangle2D.Double(689, p2Y, 5, 50); ball = new Ellipse2D.Double(bX, bY, 7, 7); top = new Line2D.Double(0,0,700,0); bottom = new Line2D.Double(0,447,700,447); g2.draw(top); g2.draw(bottom); g2.fill(paddle1); g2.fill(paddle2); g2.fill(ball); } public void keyPressed(KeyEvent e){ //----------------------------------------------------------------------------------------------------------------- //Starts the ballTimer if it hasn't already started and moves the paddles according to the keys that are pressed //----------------------------------------------------------------------------------------------------------------- int keyCode = e.getKeyCode(); switch(keyCode){ //moves player 2's paddle up and stops at the border of the frame case KeyEvent.VK_UP: if(!started) ballTimer.start(); if(p2Y > 0){ p2Y -= 20; repaint(); } break; //moves player 2's paddle down and stops at the border of the frame case KeyEvent.VK_DOWN: if(!started) ballTimer.start(); if(p2Y < 396){ p2Y += 20; repaint(); } break; //moves player 1's paddle up and stops at the border of the frame case KeyEvent.VK_W: if(!started) ballTimer.start(); if(p1Y > 0){ p1Y -= 20; repaint(); } break; //moves player 1's paddle down and stops at the border of the frame case KeyEvent.VK_S: if(!started) ballTimer.start(); if(p1Y < 396){ p1Y += 20; repaint(); } break; } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} public void actionPerformed(ActionEvent e){ //-------------------------------------------------------------------------------------------------------------------------------- //Calls the movement methods to make the ball move //-------------------------------------------------------------------------------------------------------------------------------- if(upRight) upRight(); else if(upLeft) upLeft(); else if(downRight) downRight(); else if(downLeft) downLeft(); repaint(); } //----------------------------------------------------------------------------------------------------------------------------------- //All the methods that will move the ball in a certain direction //Implemented in the acitonPerformed method // //Decides which direction the ball is going to move in //If the ball intersects with either of the paddles, it will bounce off accordingly and the ballSpeed will increase (until it = 10) //If the ball gets passed one of the paddles, everything resets and 1 is added to the score of the player that scored //Each time someone scores, the ball will start off in the opposite direction as before //------------------------------------------------------------------------------------------------------------------------------------ public void upRight(){ bX += 5; bY -= 5; if(paddle2.intersects(ball.getBounds2D())){ upRight = false; upLeft = true; if(ballSpeed > 15){ ballSpeed -= 5; ballTimer.setDelay(ballSpeed); } }else if(top.intersects(ball.getBounds2D())){ upRight = false; downRight = true; }else if(bX >= 700 || bX < 0){ ballTimer.stop(); ballSpeed = 50; ballTimer.setDelay(ballSpeed); started = false; bX = 350; bY = 200; p1Y = 200; p2Y = 200; upRight = false; downLeft = false; upLeft = false; downRight = true; p1Score ++; } } public void upLeft(){ bX -= 5; bY -= 2; if(paddle1.intersects(ball.getBounds2D())){ upRight = true; upLeft = false; if(ballSpeed > 15){ ballSpeed -= 5; ballTimer.setDelay(ballSpeed); } }else if(top.intersects(ball.getBounds2D())){ upLeft = false; downLeft = true; }else if(bX >= 700 || bX < 0){ ballTimer.stop(); ballSpeed = 50; ballTimer.setDelay(ballSpeed); started = false; bX = 350; bY = 200; p1Y = 200; p2Y = 200; upRight = true; downLeft = false; upLeft = false; downRight = false; p2Score ++; } } public void downRight(){ bX += 5; bY += 5; if(paddle2.intersects(ball.getBounds2D())){ downRight = false; downLeft = true; if(ballSpeed > 15){ ballSpeed -= 5; ballTimer.setDelay(ballSpeed); } }else if(bottom.intersects(ball.getBounds2D())){ upRight = true; downRight = false; }else if(bX >= 700 || bX < 0){ ballTimer.stop(); ballSpeed = 50; ballTimer.setDelay(ballSpeed); started = false; bX = 350; bY = 200; p1Y = 200; p2Y = 200; upRight = false; downLeft = true; upLeft = false; downRight = false; p1Score ++; } } public void downLeft(){ bX -= 5; bY += 5; if(paddle1.intersects(ball.getBounds2D())){ downRight = true; downLeft = false; if(ballSpeed > 15){ ballSpeed -= 5; ballTimer.setDelay(ballSpeed); } }else if(bottom.intersects(ball.getBounds2D())){ upLeft = true; downLeft = false; }else if(bX >= 700 || bX < 0){ ballTimer.stop(); ballSpeed = 50; ballTimer.setDelay(ballSpeed); started = false; bX = 350; bY = 200; p1Y = 200; p2Y = 200; upRight = false; downLeft = false; upLeft = true; downRight = false; p2Score ++; } } }