Search code examples
javajframejpanelpaintcomponentcardlayout

JPanel won't show paintComponent while using cardLayout


I have been having trouble getting the paint component to draw anything on the third screen. I have tested it with this simple amount of code and have found 1 thing out. If i set the co-ordinates to be 0,0 for the rectangle then a small tiny dot appears at the middle top of the screen at a fraction of the size it should be.

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

public class hello extends JPanel {
    public int[][] room = new int[20][20];// Whether the room is solid or hollow

    public static Random r = new Random();
    boolean toggle, GameEnable;
    int i, j, px, py, temp, endx, endy;
    private static final long serialVersionUID = 1L;
    JFrame f = new JFrame("Maze Game");

    JPanel p0 = new JPanel();
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();

    JButton b1 = new JButton("Screen 2");
    JButton b2 = new JButton("Give Visuals");

    CardLayout cl = new CardLayout();
    public void Game() {
        p0.setLayout(cl);

        p1.add(b1);
        p2.add(b2);
        p3.add(new GameVisual());
        p1.setBackground(Color.RED);
        p2.setBackground(Color.BLACK);
        p4.setBackground(Color.PINK);

        p0.add(p1, "1");
        p0.add(b2, "2");
        p0.add(p3, "3");
        p0.add(p4, "4");
        cl.show(p0, "1");

        final Timer timer = new Timer(10 * 60 * 1000, new ActionListener() {
            public void actionPerformed(final ActionEvent e) {
                cl.show(p0, "4");
            }
        });

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(p0, "2");
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(p0, "3");
                timer.start();
            }
        });

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(816, 816);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.add(p0);
        timer.start();
    }

    class GameVisual extends JPanel { //This is a sub class not a separate class file
        private static final long serialVersionUID = 2L;

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLUE);
            g.fillRect(10,10, 400, 400);
        }
    }

    public static void main(String[] args) {
        new hello().Game();
    }
}

So my question is why is my paintcomponent not apearing the the screen (or showing at the fraction of the size at the wrong location and how may I fix this. P.S. if possible i would like to keep everything in the same class rather than put them into separate classes as i use an array to do the majority of my painting in the larger version of this code.


Solution

  • First, I think you mean to add p2 to p0 (not b2):

    p0.add(p2, "2");
    

    Second, if you want to see the full rectangle, you can do this:

    GameVisual gv = new GameVisual();
    gv.setPreferredSize(new Dimension(500,500));
    p3.add(gv);
    

    However, as you add components to GameVisual, it will resize to fit those components. So perhaps you shouldn't set the preferred size.