Unable to load panel images, text, etc. When switching from one panel to another

I am a high school student who needs to finish a game for my Java class. My game revolves around levels. The main problem that I experience in my game is when I load one of my panels. It so happened that the screen froze. I could not press the buttons or even close the panel. There were no errors on the terminal, and it was successful after compilation.

Some information:

  • Using CardLayout
  • The first panel that appeared, and the other panels after it worked fine. Just not this panel.
  • When I tried to make this panel initially at startup, the panel was white.
  • Used by eclipse and added SerialVersionUID to all my classes. Still not working.

Please excuse any code that is or looks unattractive to you. Thank.

class Level1 extends JPanel implements ActionListener
{
    private String [] questions;
    private String question;
    private String [] answers;
    private String answer;
    private String [] images;
    private String image;
    private String [] wrongAnswers1;
    private String wrongAnswer1;
    private String [] wrongAnswers2;
    private String wrongAnswer2;
    private String [] wrongAnswers3;
    private String wrongAnswer3;
    private int time;
    private int integer;
    private int problemNumber;
    private int lives;
    private int answerLocation;
    private int wrongOrNot;
    private int num;
    private boolean finished;
    private boolean newProblem;
    private boolean getOut;
    private boolean makeOrder;
    private JButton restartButton;
    private JButton getOutButton;
    private ButtonGroup colorgroup;
    private JRadioButton answer1;
    private JRadioButton answer2;
    private JRadioButton answer3;
    private JRadioButton answer4;
    private File file;
    private PrintWriter makesOutput;
    private Scanner lookForTwo;
    private String number;
    private boolean thereIsTwo;
    private Timer timer;
    private ArrayList<Integer> list1;
    private int i;
    private int y;

    public Level1()
    {
        questions = new String[15];
        //filled in each part of the array;
        question = "";
        answers = new String[15];
        //filled in each part of the array
        answer = "";
        images = new String[15];
        //filled in each part of the array
        image = "";
        wrongAnswers1 = new String[15];
        //filled in each part of the array
        wrongAnswer1 = "";
        wrongAnswers2 = new String[15];
        //filled in each part of the array
        wrongAnswer2 = "";
        wrongAnswers3 = new String[15];
        //filled in each part of the array
        wrongAnswer3 = "";
        time = 60;
        integer = -1;
        problemNumber = 1;
        lives = 3;
        answerLocation = -1;
        wrongOrNot = 1;
        num = 0;
        finished = false;
        newProblem = true;
        getOut = false;
        makeOrder = true;

        restartButton = new JButton("Restart"); //create buttons: Restart, Exit
        add(restartButton);    
        restartButton.addActionListener(this);

        getOutButton = new JButton("Exit");
        add(getOutButton);
        getOutButton.addActionListener(this);

        colorgroup = new ButtonGroup();

        answer1 = new JRadioButton("");//create radiobuttons to hold answer
        colorgroup.add(answer1);
        answer2 = new JRadioButton("");
        colorgroup.add(answer2);
        answer3 = new JRadioButton("");
        colorgroup.add(answer3);
        answer4 = new JRadioButton("");
        colorgroup.add(answer4);
        //radiobuttons on south side

        file = new File("SaveFiles.txt");
        makesOutput = null;    //variables for try-catch block
        lookForTwo = null;
        number = "";
        thereIsTwo = false;

        timer = new Timer(0,this);
        timer.setDelay(1000);
        timer.start();

        list1 = new ArrayList<Integer>();
        i = 0;
        y = 0;
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(newProblem)    //when a new problem needs to be made, the variable needs to be true
        {
            if(makeOrder)   //create random order of problems
            {
                while(i<15) 
                {
                    list1.add(new Integer(i));
                    i++;
                }
                Collections.shuffle(list1);
                makeOrder = false;
            }

            integer++;
            /*create problems randomly along with answer based on Math.Random
            and if-else along with level #s*/

            Image img1 = Toolkit.getDefaultToolkit().getImage(images[integer]);
            g.drawImage(img1, 500, 500, this);    //make sure to have an image if needed for the problem
            question = questions[integer];
            g.drawString(question, 500, 400);
            answer = answers[integer];
            wrongAnswer1 = wrongAnswers1[integer];
            wrongAnswer2 = wrongAnswers2[integer];
            wrongAnswer3 = wrongAnswers3[integer];

            answerLocation = (int)(Math.random()*4);    //set Location of answers on radiobuttons
            if(answerLocation == 1)
            {
                answer1.setText(answer);
                answer2.setText(wrongAnswer1);
                answer3.setText(wrongAnswer3);
                answer4.setText(wrongAnswer2);
            }
            else if(answerLocation == 2)
            {
                //different locations
            }
            else if(answerLocation == 3)
            {
                //different locations
            }
            else if(answerLocation == 4)
            {
                //different locations
            }
        }
        g.drawString("Time: " + time, 900, 100);

        //time is on upperleft; use drawString and time variable
        g.drawString(problemNumber + " out of 10", 50, 50);
        if(time == 0 || lives == 0)    //if time or lives goes to zero, open GameOverScreen
            panels.show(c, "Game Over Screen1");

        if(wrongOrNot == 2)
        {
            g.setColor(Color.RED);
            g.drawString("Incorrect",600,600);
        }
        else if(wrongOrNot == 3)
        {
            g.setColor(Color.GREEN);
            g.drawString("Correct",600,600);
        }

        g.setColor(Color.BLACK);

        if(problemNumber == 6)    //try-catch block for when user completes levels
            finished = true;

        try //PrintWriter is used when 5 problems are finished 
        {    
            makesOutput = new PrintWriter("SaveFiles.txt");
            lookForTwo = new Scanner("SaveFiles.txt");
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Can't find file.");
            System.exit(1);
        }
        while(lookForTwo.hasNext())
        {
            if(number.equals("2"))
                thereIsTwo = true;
        }

        if(getOut)
        {
            panels.show(c, "Title Screen");
            getOut = false;
        }

        if(finished && thereIsTwo == false) 
            makesOutput.println("2");

        if(finished)
        {
            panels.show(c, "Level Complete");
            finished = false;
        }

        makesOutput.close();

        time--;
    }

    public void actionPerformed(ActionEvent e)
    {
        newProblem = false;
        if(answer1.isSelected() && answer1.getText() == answer)
        {
            problemNumber++;
            newProblem = true;
            wrongOrNot = 3;
            questions[integer] = "";
        }
        else if(answer2.isSelected() && answer2.getText() == answer)
        {
            //same as above
        }
        else if(answer3.isSelected() && answer3.getText() == answer)
        {
            //same as above
        }
        else if(answer4.isSelected() && answer4.getText() == answer)
        {
            //same as above
        }
        else
        {
            lives-=1;
            wrongOrNot = 2;
        }

        if(restartButton.getModel().isPressed())
        {
            problemNumber = 1;
            time = 60;
            lives = 3;
            newProblem = true;
        }
        else if(getOutButton.getModel().isPressed())
        {
            getOut = true;
        }

        repaint();
    }
}

        try //code is now from another panel that loads Level1
        {
            scanner = new Scanner(file);
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Can't find file.");
            System.exit(1);
        }                
        while(scanner.hasNext())
        {
            if(word.equals("2"))
                toLevel2 = true;

            if(word.equals("3"))
                toLevel3 = true;

        }
        scanner.close();

        if(backToTitle)
        {
            panels.show(c, "Title Screen");
            backToTitle = false;
        }

        if(lvl1ButtonPressed)
            panels.show(c, "Level 1");
+4
source share

All Articles