[SOLVED] Subclass Questions

So, I have my Launcher class and, I have two other classes that extend it. Then, Those two classes use super to define which if statement to execute within the Launcher class.

Now, What I’m trying to do is have one of those extended classes create a JPanel and, a JLabel within it. So that I can show a loading gui, Which updates the JLabel to show things like; Loading: Tile “Grass” or, Loading: Image X.

Updating the JLabel isn’t a problem, The problem is adding the panel from the extended class to the JFrame of the super class (Launcher.class). Which has to be done within the if statement that identifies the extended class by a id#.

If necessary I can attach code to this post to better assist someone with helping me, Upon request. I’m kind of multi tasking right now, So if something is hard to understand just ask for clarification.

Always just put in code if you have it. I couldn’t really tell what you meant.

Okay, Just forgive me for any code that doesn’t comply to the common practices…

The first code sample is the LauncherGUI(Play Game, Configuration, Exit Game), The second is the LoadingGUI(Updates a JLabel prior to launching the game itself).

Launcher GUI:


package project.x;

import java.awt.*;
import static java.awt.Component.LEFT_ALIGNMENT;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class LauncherGUI extends JFrame {
    
    public static Configuration setting = new Configuration();    
    public static String title, author, version;
    public static int buttonWidth, buttonHeight, buttonScale;
    public static int guiHeight, guiWidth;
    private Rectangle rlogo, rplay, rconfiguration, rquit;
    
    public LauncherGUI(int id) throws IOException {
        // Load Configuration Settings
        setting.Configuration("resources/settings/Config.xml");
        title = setting.Project();
        author = setting.Author();
        version = setting.Version();
        guiHeight = setting.ResolutionHeight();
        guiWidth = setting.ResolutionWidth();
        buttonHeight = 40;
        buttonWidth = (guiWidth / 4);
        buttonScale = 30;
        // End of Configuration Settings
        
        setTitle(".:: " + title + " v" + version + " by: " + author + " :: Launcher ::.");
        setBounds(0, 0, guiWidth, guiHeight);
        setResizable(false);
        setLocationByPlatform(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        validate();
        
        if (id == 0) {
            BufferedImage RefinedbgImg = ImageIO.read(new File("resources/images/Launcher/Background1.png"));
            ImageIcon RefinedlogoImg = new ImageIcon("resources/images/Logo.png");
            
            BufferedImage rawButton = ImageIO.read(new File("resources/images/Launcher/button.png"));
            Image scaledButton = rawButton.getScaledInstance(buttonWidth, buttonHeight, Image.SCALE_SMOOTH);
            ImageIcon button = new ImageIcon(scaledButton);
            
            BackgroundPanel bgPanel = new BackgroundPanel(RefinedbgImg, BackgroundPanel.SCALED, 0, 0);
            //bgPanel.setPaint(paint);
            
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(null);
            mainPanel.setOpaque(false);
            mainPanel.setBounds(0, 0, guiHeight, guiWidth);
            mainPanel.setAlignmentY(LEFT_ALIGNMENT);
            mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            
//            JLabel logo = new JLabel();
//            logo.setIcon(RefinedlogoImg);
//            rlogo = new Rectangle((guiWidth / 2), (guiHeight / 2) + 200);
//            logo.setBounds(rlogo);
            
            JButton play = new JButton(button);
            play.setLayout(new BorderLayout());
            JLabel playButtonLabel = new JLabel("Play Game");
            playButtonLabel.setForeground(Color.WHITE);
            playButtonLabel.setFont(new Font("Times New Roman", Font.BOLD, guiHeight / buttonScale));
            playButtonLabel.setHorizontalAlignment(SwingConstants.CENTER);
            play.add(playButtonLabel);
            rplay = new Rectangle((guiWidth / 2) - (buttonWidth / 2), (guiHeight /2) - (buttonHeight / 2) - (buttonHeight + 20), buttonWidth, buttonHeight);
            play.setBounds(rplay);
            
            JButton configuration = new JButton(button);
            configuration.setLayout(new BorderLayout());
            JLabel configButtonLabel = new JLabel("Configuration");
            configButtonLabel.setForeground(Color.WHITE);
            configButtonLabel.setFont(new Font("Times New Roman", Font.BOLD, guiHeight / buttonScale));
            configButtonLabel.setHorizontalAlignment(SwingConstants.CENTER);
            configuration.add(configButtonLabel);
            rconfiguration = new Rectangle((guiWidth / 2) - (buttonWidth / 2), (guiHeight /2) - (buttonHeight / 2), buttonWidth, buttonHeight);
            configuration.setBounds(rconfiguration);
            
            JButton quit = new JButton(button);
            quit.setLayout(new BorderLayout());
            JLabel quitButtonLabel = new JLabel("Exit Game");
            quitButtonLabel.setForeground(Color.WHITE);
            quitButtonLabel.setFont(new Font("Times New Roman", Font.BOLD, guiHeight / buttonScale));
            quitButtonLabel.setHorizontalAlignment(SwingConstants.CENTER);
            quit.add(quitButtonLabel);
            rquit = new Rectangle((guiWidth / 2) - (buttonWidth / 2), (guiHeight /2) - (buttonHeight / 2) + (buttonHeight + 20), buttonWidth, buttonHeight);
            quit.setBounds(rquit);
        
            play.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    try {
                        new LoadingGUI(3);
                    } catch (IOException ex) {
                        Logger.getLogger(LauncherGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        
            configuration.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    try {
                        new ConfigurationGUI(1);
                    } catch (IOException ex) {
                        Logger.getLogger(LauncherGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
        
            quit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();  
                    System.exit(0);
                }
            });
            mainPanel.add(play);
            mainPanel.add(configuration);
            mainPanel.add(quit);
            bgPanel.add(mainPanel);
            add(bgPanel);
            //add(mainPanel);
        } else if (id == 1) {
            BufferedImage rawButton = ImageIO.read(new File("resources/images/Launcher/button.png"));
            Image scaledButton = rawButton.getScaledInstance(buttonWidth, buttonHeight, Image.SCALE_SMOOTH);
            ImageIcon button = new ImageIcon(scaledButton);
 
            JPanel configPanel = new JPanel();
            configPanel.setLayout(null);
            configPanel.setOpaque(false);
            configPanel.setBounds(0, 0, guiHeight, guiWidth);
            configPanel.setAlignmentY(LEFT_ALIGNMENT);
            configPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        
            JButton snc = new JButton(button);
            snc.setLayout(new BorderLayout());
            JLabel sncButtonLabel = new JLabel("Save & Close");
            sncButtonLabel.setForeground(Color.WHITE);
            sncButtonLabel.setFont(new Font("Times New Roman", Font.BOLD, guiHeight / buttonScale));
            sncButtonLabel.setHorizontalAlignment(SwingConstants.CENTER);
            snc.add(sncButtonLabel);
            Rectangle rsnc = new Rectangle((guiWidth - (buttonWidth + 10)), (guiHeight - (buttonHeight + 30)), buttonWidth, buttonHeight);
            snc.setBounds(rsnc);
            
            configPanel.add(snc);
        
            Choice ResolutionChoice = new Choice();
            Rectangle DisplayResolution = new Rectangle((0 + 100), (0 + 20), 140, 40);
            ResolutionChoice.setBounds(DisplayResolution);
            ResolutionChoice.add("640, 480");
            ResolutionChoice.add("800, 600");
            ResolutionChoice.add("1024, 768");
            ResolutionChoice.add("1280, 1024");
            ResolutionChoice.add("1600, 1200");
            if (guiWidth == 640) {
                ResolutionChoice.select(0);            
            } else if (guiWidth == 800) {
                ResolutionChoice.select(1);
            } else if (guiWidth == 1024) {
                ResolutionChoice.select(2);
            } else if (guiWidth == 1280) {
                ResolutionChoice.select(3);
            } else if (guiWidth == 1600) {
                ResolutionChoice.select(4);
            }
            configPanel.add(ResolutionChoice);
        
            JLabel ChooseResolution = new JLabel("Resolution:");
            Rectangle ResolutionLabel = new Rectangle((0 + 20), (0 + 20), 100, 40);
            ChooseResolution.setBounds(ResolutionLabel);
            configPanel.add(ChooseResolution);
        
            snc.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                int Resolution = ResolutionChoice.getSelectedIndex();
                if (Resolution == 0) {
                    dispose();
                    try {
                        setting.saveConfiguration("ResolutionWidth", "640", "Project");
                        setting.saveConfiguration("ResolutionHeight","480", "Project");
                        Thread.sleep(2000);
                        new LauncherGUI(0);
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (Resolution == 1 || Resolution == -1) {
                    dispose();
                    try {
                        setting.saveConfiguration("ResolutionWidth", "800", "Project");
                        setting.saveConfiguration("ResolutionHeight", "600", "Project");
                        Thread.sleep(2000);
                        new LauncherGUI(0);
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (Resolution == 2) {
                    dispose();
                    try {
                        setting.saveConfiguration("ResolutionWidth", "1024", "Project");
                        setting.saveConfiguration("ResolutionHeight", "768", "Project");
                        Thread.sleep(2000);
                        new LauncherGUI(0);
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (Resolution == 3) {
                    dispose(); 
                    try {
                        setting.saveConfiguration("ResolutionWidth", "1280", "Project");
                        setting.saveConfiguration("ResolutionHeight", "1024", "Project");
                        Thread.sleep(2000); 
                        new LauncherGUI(0);
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (Resolution == 4) {
                    dispose(); 
                    try { 
                        setting.saveConfiguration("ResolutionWidth", "1600", "Project");
                        setting.saveConfiguration("ResolutionHeight", "1200", "Project");
                        Thread.sleep(2000);   
                        new LauncherGUI(0);
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ConfigurationGUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
        add(configPanel);   
    } else if (id == 2) {
// This is the part of the LauncherGUI that applies to the LoaderGUI        
        
        
    }
    setVisible(true);
    }
}

LoaderGUI


package project.x;

import static java.awt.Component.LEFT_ALIGNMENT;
import java.awt.ComponentOrientation;
import java.awt.Rectangle;
import java.io.IOException;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import static project.x.LauncherGUI.author;
import static project.x.LauncherGUI.guiHeight;
import static project.x.LauncherGUI.guiWidth;
import static project.x.LauncherGUI.title;
import static project.x.LauncherGUI.version;
import project.x.levels.Level;

public class LoadingGUI extends LauncherGUI {
    

    private static ImageManager im;
    private static Level map;
    private static Player player;
    
    public LoadingGUI(int id) throws IOException {
        super(2);
        setTitle(".:: " + title + " v" + version + " by: " + author + " :: Loading ::.");
        
        // From here down is where I've been trying to implement the new panel + label to do what I need
        JPanel loadingPanel = new JPanel();
        loadingPanel.setLayout(null);
        loadingPanel.setOpaque(false);
        loadingPanel.setBounds(0, 0, guiHeight, guiWidth);
        loadingPanel.setAlignmentY(LEFT_ALIGNMENT);
        loadingPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
        
        JLabel loadingLabel = new JLabel("Testing");
        
        Rectangle rlL = new Rectangle((guiWidth / 2), (guiHeight / 2) , 200, 40);
        loadingLabel.setBounds(rlL);
        
        loadingPanel.add(loadingLabel);
        
        add(loadingPanel);
        // End of attempt # 7
        validate();
        
        // Implemented thread sleep so there's time to see the GUI
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            Logger.getLogger(LoadingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        im = new ImageManager();
        map = new Level();
        //player = new Player((WIDTH / 2) - (map.get_TileSize() / 2), (HEIGHT / 2) - (map.get_TileSize() / 2), im);
        
        player = new Player(0, 0, im);
        this.addKeyListener(new Keyboard());
        
        // The next line starts the actual game
        new Display().DisplayStart();
        
        dispose();
    }
    
    public static Level getLevel() {
        return map;
    }
    
    public static Player getPlayer() {
        return player;
    }
    
    public static ImageManager getImageManager() {
        return im;
    }
}

I’m just looking for something that works right now, Not a complete rewrite. If that’s a possible answer… The LoadingGUI class is based on another class (ConfigurationGUI) which functions similarly.

What exactly is the problem? Does the loading panel not display? What happens if you remove the dispose()?

loading panel doesn’t display and, removing dispose() just keeps the LoadingGUI open.

Not sure why your panel isn’t displaying, but here’s a more idiomatic and stripped down version that uses your inheritance model to play with:


import java.awt.*;

import javax.swing.*;


class TestFrame extends JFrame {
	
	TestFrame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
}
class RunFrame extends TestFrame {
	 
	RunFrame() {
		super();
		 
		JPanel loadingPanel = new JPanel();
		loadingPanel.setLayout(new GridBagLayout()); // protip: non-constrained GBL to center stuff
		loadingPanel.setOpaque(false);
		loadingPanel.setSize(400, 400);
		// preferredSize needed for pack()
		loadingPanel.setPreferredSize(loadingPanel.getSize());
	        
		JLabel loadingLabel = new JLabel("Testing");
		loadingPanel.add(loadingLabel);
		
		add(loadingPanel);
		// similar to setSize(loadingPanel.getSize());
		pack();
		
		// reliable way to center frame on screen, must be done after frame has been sized
		Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(ss.width/2 - getWidth()/2, ss.height/2-getHeight()/2);
	}
	 
	public static void main(String[]a) {
		new RunFrame();
	}
}

Hey, I got everything working now, Just have to continue with what I was doing. Just had to put all the JPanel stuff in the LauncherGUI class and, the new player/imagemanager/map/etc in the subclass. Thanks for the suggestions :slight_smile: