Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Change a JLabel value that is in another class?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Change a JLabel value that is in another class?

    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 ++;
    		}
    	}
    }
    Last edited by jm24; February 17th, 2012 at 06:17 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Change a JLabel value that is in another class?

    You could create a JLabel field in your PongDrawer class, then pass scoreLabel into the PongDrawer object (either by the constructor or a set() method). That way, you could make changes to the JLabel directly within the PongDrawer class and the changes would then show up in your GUI.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. The Following User Says Thank You to snowguy13 For This Useful Post:

    jm24 (February 17th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Change a JLabel value that is in another class?

    Awesome! Thanks it worked perfectly

  5. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Change a JLabel value that is in another class?

    That's great! I'm glad it worked!

    Good luck with your programming!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Similar Threads

  1. JLabel
    By Poseidon in forum AWT / Java Swing
    Replies: 2
    Last Post: February 18th, 2012, 04:39 PM
  2. How can I change the values in an object instantiated from an abstract class?
    By babe20042004 in forum Object Oriented Programming
    Replies: 10
    Last Post: December 16th, 2011, 12:05 AM
  3. JLabel image?
    By frozen java in forum AWT / Java Swing
    Replies: 2
    Last Post: September 28th, 2011, 07:44 AM
  4. Everything but JLabel is displaying
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2011, 10:30 PM
  5. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM