I am doing a game and I need to reset the graphics back to its original picture once a new game begins, however when I click the new game button, everything else resets but the graphics do not even though I called repaint. I have included the code for my main game panel(the new game buttons etc) and the game logic code with paint graphics.

This is my main game panel code.

public class MainPanel extends JPanel implements ActionListener{
    
    private JButton newGame, quit;
    private JRadioButton onePlayer, twoPlayer;
    private JComboBox diff;

    private Hangman game = new Hangman();
    
    public MainPanel (){
        
        super();
       
        //create menu panel
        JPanel menuPanel = new JPanel();
        menuPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
        menuPanel.setBackground(Color.orange);
        menuPanel.setPreferredSize(new Dimension(150, 550));

        //create game display panel
        JPanel gamePanel = new JPanel();
        gamePanel.setBorder(BorderFactory.createTitledBorder("Game Display"));
        gamePanel.setBackground(Color.white);
        gamePanel.setPreferredSize(new Dimension(400, 550));
      
        //create game status panel
        JPanel statusPanel = new JPanel();
        statusPanel.setBorder(BorderFactory.createTitledBorder("Game Status"));
        statusPanel.setBackground(Color.yellow);
        statusPanel.setPreferredSize(new Dimension(180, 350));

        //main panel bg color
        setBackground(Color.black);

        //Text labels for game status
        JLabel difficulty = new JLabel("Difficulty: ");
        JLabel tries = new JLabel("Tries Remaining: " + (6 - game.getNumGuesses()));
        JLabel hint = new JLabel("Hint: " );
        JLabel currLevel = new JLabel("Level: 1");

        //create game menu buttons
        newGame = new JButton("New Game");
        quit = new JButton("Quit");

        //new game button
        newGame.setPreferredSize(new Dimension(100,20));
        newGame.setToolTipText("Start New Game");
        newGame.addActionListener(this);

        //quit button
        quit.setPreferredSize(new Dimension(100,20));
        quit.setToolTipText("Exit game");
        quit.addActionListener(this);

        //radio buttons for one/two player modes
        onePlayer = new JRadioButton ("One Player", true);
        twoPlayer = new JRadioButton ("Two Players");
        onePlayer.addActionListener(this);
        twoPlayer.addActionListener(this);

        //group radio buttons
        ButtonGroup group = new ButtonGroup();
        group.add (onePlayer);
        group.add (twoPlayer);

        //combo box for difficulty settings
        String[] level = {"Auto","Level 1", "Level 2", "Level 3"};
        diff = new JComboBox (level);
        diff.setSelectedIndex(0);
        diff.addActionListener(this);
                
        //menu panel, buttons, box layout
        menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
        menuPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        menuPanel.add (newGame);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
       // menuPanel.add (difficulty);
       // menuPanel.add (diff);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        menuPanel.add (onePlayer);
        menuPanel.add (twoPlayer);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        menuPanel.add (quit);
        menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
        
        //game status panel with labels, box layout
        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.PAGE_AXIS));
        statusPanel.add (currLevel);
        statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        statusPanel.add (hint);
        statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
        statusPanel.add (tries);
        statusPanel.add (Box.createVerticalStrut(320));

        //nest panels in main panel
        gamePanel.add(game);
        menuPanel.add(statusPanel);
        add(menuPanel);
        add(gamePanel);

        game.repaint();
}



/*
 * Action Listener for menu buttons.
 * */
public void actionPerformed(ActionEvent e) {

        if(e.getSource() == newGame){
       
        game.resetGame();
        game.repaint();
        return;

        }


        if(e.getSource() == quit){
            System.exit(1);
        }

        

    }


}

Here is my game logic code with the paint graphics. My game buttons resets and works, I get a new random word too. Only thing that doesnt work is the graphics resetting to original.

public class Hangman extends JPanel implements ActionListener{

    private boolean used[];

    private String guessword;

    private int numguesses;

    private boolean finished;

    private Button a[];

    private boolean won;

    private RandomWord rw = new RandomWord();

public Hangman(){

    setPreferredSize(new Dimension(350, 350));
    initgame();
    repaint();

}


public int getNumGuesses(){
    return numguesses;
}


/*
 * initialise the game
 * */
public void initgame() {

        	//initialise game variables
            rw.newGame();  //generate random word
            guessword = rw.getWord();//get the random word into word buffer

            won = false;
            finished = false;
            numguesses = 0;

            used = new boolean[26];

			a = new Button[26];
            StringBuffer buffer;
			//create keyboard buttons
			for (int i = 0; i <26; i++) {
			    buffer = new StringBuffer();
				buffer.append((char)(i+65));
				a[i] = new Button(buffer.toString());
				a[i].addActionListener(this);
                a[i].setEnabled(true);
                add(a[i]);
			}
				
            
            

    }


/*
 * Paint Hangman picture
 *
 * */
@Override
public void paintComponent(Graphics g) {


    Graphics2D g2 = (Graphics2D) g;

    //draw the hangman pole
    setBackground(Color.WHITE);
    g2.fillRect(10, 250, 150, 20);
    g2.fillRect(40,70,10,200);
    g2.fillRect(40,70,60,10);
    g2.setColor(Color.GRAY);
    g2.fillRect(95,70,5,25);

    //draw head
    g2.setColor(Color.MAGENTA);
    if (numguesses >=1 )
    g2.drawOval(82,95,30,30);

    //draw body
    g2.setColor(Color.PINK);
    if (numguesses >=2 )
    g2.drawLine(97,125,97,150);

    if (numguesses >=3 )
    g2.drawLine(97,150,117,183);

    if (numguesses >=4 )
    g2.drawLine(97,150,77,183);

    if (numguesses >=5 )
    g2.drawLine(97,125,117,135);

    if (numguesses >=6 )
    g2.drawLine(97,125,77,135);

    //fill up the used letters
    StringBuffer st = new StringBuffer();
    for (int l=0; l<=25; l++) {
    if (used[l])
        st.append((char)(l+65));
    else
        st.append("-");
    }

    g2.setColor(Color.BLUE);
    Font f = new Font("Times New Roman",Font.ITALIC,14);

    //display the used letters
    g2.setFont(f);
    g2.drawString(st.toString(),25,285);
    st.delete(0, 26);

    StringBuffer guessed = new StringBuffer();
    Font ff = new Font("Times New Roman",Font.BOLD,24);
    g2.setColor(Color.BLACK);
    g2.setFont(ff);

    //display the hidden word as - and letters
    for (int mm = 0;mm < guessword.length();mm++) {
    if (used[(int)guessword.charAt(mm)-65])
    guessed.append(guessword.charAt(mm));
    else
    guessed.append("-");
    }

    g2.drawString(guessed.toString(),75,230);
    guessed.delete(0, guessword.length()+1);


    if (numguesses >=6) {//more than 6 tries = lose
    g2.setColor(Color.WHITE);
    g2.fillRect(70, 200, 200, 30);
    g2.setColor(Color.BLACK);
    g2.drawString(guessword.toString(),75,230);
    Font fff = new Font("Times New Roman",Font.BOLD,36);
    g2.setFont(fff);

    g2.setColor(Color.RED);
    g2.drawString("You lose!",130,150);//display you lose

    finished = true;//game finish = true
    }

    if (won) {//if win game
    Font fff = new Font("Times New Roman",Font.BOLD,36);
    g2.setFont(fff);

    g2.setColor(Color.GREEN);

    g2.drawString("You Win!",130,150);//display you win

    finished = true;//game finish = true

    }

    repaint();
    
}


/*
 * Action listener for key buttons
 * */
public void actionPerformed( ActionEvent ev) {
	  
	  for (int i = 0; i < 26; i++) {
	  	if (ev.getSource() == a[i]) { 
            checker(i);//call check letter method
            a[i].setEnabled(false);//disable once pressed
           
        }
	  }
}
    
    
/*
 * Check letter input from user
 * */
public void checker(int letter) {

    if (!finished) {

        boolean found = false;
        boolean wrong = false;

        if (!used[letter]) {//if not in the used letter array
                for (int mm = 0;mm < guessword.length(); mm++) {
                        if (((char)(letter+65)) == guessword.charAt(mm))
                        {   System.out.println((char)(letter+65));
                            found = true;//letter matching found = true
                        }

                }
                if (!found)//if not matching
                numguesses++;//number of guesses used increased
                System.out.println(numguesses);

        }

        used[letter] = true;//set true when letter is used

        
        //if word not matching wrong = true
        for (int mm = 0;mm < guessword.length(); mm++) {
              if (!used[(int)(guessword.charAt(mm))-65])
                  wrong = true;
        }

        if (!wrong)//if word is not wrong win game
        won = true;

        repaint();
   }

}


public void resetGame(){

            //initialise game variables
            rw.newGame();  //generate random word
            guessword = rw.getWord();//get the random word into word buffer
            used = new boolean[26];
            won = false;
            finished = false;
            numguesses = 0;

            //reset letter board
            for (int i = 0; i <26; i++) {
                a[i].setEnabled(true);
            }
            
            
}

}

In MainPanel, at the very end, try doing "validate();" right before repainting, so the game object can note any changes that were made.

In your paint() method, call super.paintComponent(g). It will clear the screen. Alternatively, you could use this piece of code from inside your paintComponent method:

g.clearRect(0, 0, this.getWidth(), this.getHeight());
This question has already been answered. Start a new discussion instead.