Hi - Im making a small and relatively simple game that use's the common feature of the scoreboard to keep track of the user's score. I have declared the main game frame in one class, the actual game on one panel in another and have decided to keep the scoreboard as a class of its own.

When I want to update the score using the scoreboard class I can - but when I try to do the same thing using the same methods outlined in the scoreboard class it doesn't work. I create an instance of the class as you would and have added a few lines to determine whether or not the method was being called - it was. So why wouldn't it update the way it does in the other class.

The scoreboard class is as follows:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

public class scoreBoard extends JPanel
{
	int userScore = 0;
	String text = "Score: "+userScore;
	int posX = 10;	int posY = 25;	
	
	JButton changeText = new JButton("Update Score");
	
	public scoreBoard()
	{
		changeText.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				text = "Score: "+increaseScore();
				
				/**
				posX += 5;
				posY += 10;
				*/
				
				repaint();
			}
			
		});
		
		add( changeText );
		setBackground( Color.RED );
	}
	

	public void paint(Graphics g) 
	{
        super.paint( g );								// Over-ride the paint method

        Graphics2D g2score = ( Graphics2D )g;
        g2score.drawString( text, posX, posY );
        
	}
	
	public int increaseScore()
	{
		userScore += 10;
		return userScore;
	}
}

***Please excuse the randomness of some of the colours :P***

Second class called MPanel - I have only included the relevant method & the code which creates the instance here:

scoreBoard keepScore = new scoreBoard();
/*************************************************/


	public JButton createJButton( int buttonNum )
	{
		// final int rand = getRandomNum(4);
		
		/**
		Check the random numbers are generated 
		System.out.println( rand );
		*/
		
		// Button becomes a constant
		final JButton button = new JButton();
		
		button.setText(""+buttonNum );
		button.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if( button.getText().contains("1") || button.getText().contains("2") )
				{
					System.out.println( "BOOM" );
					
					keepScore.text = "Score: "+keepScore.increaseScore();
					keepScore.repaint();
					
					button.setIcon( bomb );
					
				}
			}
			
		});
		
		return button;
		
	}

So what you think?

Recommended Answers

All 3 Replies

Since MPanel is not complete, and you haven't included your main game class, I created a tester and just put your scoreBoard panel on a JFrame. Everything seems to be working fine. So I also added a button to try to update the scoreBoard like you're doing in MPanel, and that is also working. What is the problem you're having? Here's the tester I wrote.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Tester {

  public static void main(String[] args) {
    JFrame frame = new JFrame("Test");
    final scoreBoard keepScore = new scoreBoard();
    frame.setLayout(new BorderLayout());
    frame.add(keepScore, BorderLayout.CENTER);

    JButton b = new JButton("Press");
    b.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ae) {
        keepScore.text = "Score: "+keepScore.increaseScore();
        keepScore.repaint();
      }
    });
    frame.add(b, BorderLayout.SOUTH);

    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Well it simply isn't working - the main class is as follows:

import java.awt.BorderLayout;
import javax.swing.*;

public class MFrame extends JFrame
{
	public MFrame()
	{
		setLayout( new BorderLayout() );
		
		add( new MScoreboard(), BorderLayout.NORTH );
		add( new MPanel(), BorderLayout.CENTER );
		
		setTitle( "Game" );
		setSize( 300,350 );
		setVisible( true );
		setResizable( false );
		setLocationRelativeTo( null );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}
	
	public static void main( String[] args )
	{
		new MFrame();
	}
}

I hardly think this is the problem - it runs fine but simply won't update the score. What am I doing wrong?

It turns out that - through some testing - the problem lies in the fact that its a panel on another panel I believe - when I tested it on a frame it all worked fine. I just need to think how I can properly & simply migrate some things to go it working well

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.