JFrames and JPanels

I have been pulling my hair for days with this now. I simply want to create a black background and then add a JPanel on top of that with a green bg. New to Java and Swing obvisiouly. Over all I want to draw a football field in the panel, that displays 30 yards at a time and the background will scroll up and down following the location of the football. I figured with a panel I could draw the entire field and the panel would clip the remainder of the field. Problem is I cant get the second panel to even show. This is what I have.

public static void main(String[] args) {

    drawPracticeField addField = new drawPracticeField();

    //Create frame


    JFrame backGroundWindow = new JFrame();
    
        backGroundWindow.setSize(760, 760);
        backGroundWindow.setUndecorated(true);
        backGroundWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Utilizing apsolute positioning
        backGroundWindow.setLayout(null);
        //make Visible
        backGroundWindow.setVisible(true);

        
        //JPanel Background color component

        JPanel backGroundPanel =  new JPanel();
        backGroundPanel.setSize(760,760);
        backGroundPanel.setBackground(Color.black);

        // add panel to container
        backGroundWindow.add(backGroundPanel);
        // addNewField JPanel
        backGroundWindow.add(addField).setLocation(50, 40);

public class drawPracticeField extends JPanel{

public drawPracticeField() {

    
    
    JPanel newField = new JPanel();
    Dimension d = new Dimension();
    d.setSize(600, 600);
    newField.setPreferredSize(d);
    
}

    /*
    Draw outside boarders
    Draw hash marks
    Draw 10 yard lines
    Add Line numbers Line of scrimage to 40 yard line
     */



protected void paintComponent(Graphics g) {

   super.paintComponent(g);
   
   Color greenField = new Color (45, 96, 32);
   g.setColor(greenField);
   g.drawRect(50,40,600,600);
   g.fillRect(50,40,600,600);

   


}

Thank you for any info you can give me.

You have 3 different JPanels, and none have been added to the JFrame.

backGroundPanel is created and never added to a JFrame, so it will never appear.

newField is created inside your drawPracticeField constructor, but never added to anything. Therfore, when the constructor exits, new field gets destroyed.

addField gets created, but never gets added to a JFrame, so it will never appear.

Could you describe what you think each panel is supposed to do ?

Sorry that few lines of code where I added the Panels to the frame

You never set the size of the “drawPraticeField” panel. It’s default size is 0,0 :wink:

Don’t try to use multiple panels for a single drawing area. Here is what you can try:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test extends JFrame {
    public Test() {
        setLayout(new BorderLayout());
        JScrollPane scroll = new JScrollPane(new FootballField());
        add(scroll, BorderLayout.CENTER);
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //for anything to have a size you need to make it visible
        setVisible(true);
        Insets frameMargin = getInsets();
        Insets scrollMargin = scroll.getInsets();
        int frameWidth = 490 + frameMargin.left + frameMargin.right + scrollMargin.left + scrollMargin.right + scroll.getVerticalScrollBar().getWidth();
        //resize the frame so the entire football field can be seen
        setSize(frameWidth, 500);
    }
    public static void main(String... args) {
        Test t = new Test();
    }
}

class FootballField extends JPanel {
    Dimension size = new Dimension(490, 980);
    
    public void paintComponent(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 490, 980);
        g.setColor(Color.GREEN);
        g.fillRect(70, 70, 350, 840);
    }
    
    //override getPreferredSize so any container will know what size this component is
    public Dimension getPreferredSize() {
        return size;
    }
}