This is part of a larger project. I've simplified it here. I am implementing a word search program where word puzzles are produced and displayed. There are two main panels. One is the word search panel where there are rows of letters that spell words in different directions. The user clicks different boxes on the panel for the first and last letters of the word, at which point the letters change colors and the word is circled. Another panel is a panel which lists the words to find. There are some other panels, including one with buttons, which produces a new word set to find and a different rows of letters. My problem appears to be this. When I click the "New Game" button, the frame goes blank. I have to maximize the screen to get it to repaint so I can see the panels and the buttons again. I would like to not have to maximize the screen. I need to call something somewhere in my Reset2 function, but I'm not sure what.

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

public class PracticeFrame extends JFrame
{
     PracticePanel pc;
     
     public PracticeFrame ()
     {
         this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         pc = new PracticePanel (this);
         add (pc);
         this.setSize(500, 500);
         this.setVisible(true);
     }
     
     
     public void paintComponent (Graphics g)
     {
          super.paint(g);
     }
 
     
     public static void main (String args[])
     {
         new PracticeFrame ();
     }
}
import java.awt.*;
import javax.swing.*;


public class PracticePanel extends JPanel
{
    PracticeFrame pf;
    Panel1 pan1;
    Panel2 pan2;
    
    public PracticePanel (PracticeFrame pracFrame)
    {
        pf = pracFrame;
        LayoutManager gl = new GridLayout (2, 1);
        setLayout (gl);
        pan1 = new Panel1 (this, Color.RED);
        pan2 = new Panel2 (this, Color.BLUE);
        this.add (pan1);
        this.add (pan2);
        this.setVisible (true);        
    }
    
    
    public void Reset1 (Color color1, Color color2)
    {
        pan1.color = color1;
        pan2.color = color2;
        repaint ();
    }
    
    
    public void Reset2 (Color color1, Color color2)
    {
        LayoutManager gl = new GridLayout (2, 1);
        setLayout (gl);
        this.remove (pan1);
        this.remove (pan2);
        pan1 = new Panel1 (this, color1);
        pan2 = new Panel2 (this, color2);
        this.add (pan1);
        this.add (pan2);
        this.setVisible (true);
        repaint ();
    }

    
    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);
        Graphics2D g2 = (Graphics2D) g;
        pan1.repaint ();
        pan2.repaint ();
    }
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;


public class Panel1 extends JPanel implements ActionListener
{
    PracticePanel pp;
    JButton button1;
    JButton button2;
    Color color;
    Random random;
    
    
    public Panel1 (PracticePanel pracPan, Color col)
    {
        random = new Random ();
        color = col;
        pp = pracPan;
        button1 = new JButton ("Change Colors");
        button2 = new JButton ("New Game");
        button1.addActionListener (this);
        button2.addActionListener (this);
        add (button1);
        add (button2);
        setVisible (true);
    }

    
    public void actionPerformed(ActionEvent e) 
    {
        Color colors[] = {Color.RED, Color.BLACK, Color.BLUE, Color.GREEN};
        if (e.getSource () == button1)
        {
            pp.Reset1 (colors[random.nextInt (4)], colors[random.nextInt (4)]);
        }
        else if (e.getSource () == button2)
        {
            pp.Reset2 (colors[random.nextInt (4)], colors[random.nextInt (4)]);
        }
    }
    
    
    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);
        Graphics2D g2 = (Graphics2D) g;
        setBackground (color);        
    }
}
import java.awt.*;
import java.awt.event.*;


import java.util.Random;
import javax.swing.*;


public class Panel2 extends JPanel
{
    PracticePanel pp;
    Color color;
    
    
    public Panel2 (PracticePanel pracPan, Color col)
    {
        color = col;
        pp = pracPan;
        setVisible (true);
    }
    
    
    public void paintComponent (Graphics g)
    {
        super.paintComponent (g);
        Graphics2D g2 = (Graphics2D) g;
        setBackground (color);        
    }
}

The relevant procedure, I believe is Reset2 in the PracticePanel class. I am creating two new panels, getting rid of the old ones, and replacing them with the new ones. Is there some repaint that is not being called until I maximize the screen? Thank you.

Dumb mistake. I added the following line to the end of my Reset2 function and it seems to work:

validate ();

I think I'm going to keep this unsolved for now though. I want to try it on my larger project with more than two panels. Imagine it's the same problem though.

nice job catching your mistake

also do the the validate before repaint

nice job catching your mistake

also do the the validate before repaint

Thanks.

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.