0

I was about to program a little game. I have my main class MenuFrame from which I call my Gui class which draws my Game.

MenuFrame.java:

public class MenuFrame extends JFrame implements ActionListener {

    private JButton start;

    public static void main(String[] args) {
        MenuFrame mainframe = new MenuFrame("Menu");
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setSize(600, 400);        
        mainframe.setLayout(null);
        mainframe.setVisible(true);
    }

    public MenuFrame(String title) {        
        super(title);           
        start = new JButton("Start game");
        start.setBounds(220, 60, 160, 40);
        start.addActionListener(this);
        add(start);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == start) {
            game(hauptSpiel);
        }
    }

    public static void game() {
        JFrame game = new JFrame;
        game.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        game.setUndecorated(true);
        game.setResizable(false);
        game.setSize(480, 800);
        game.setLocation(1920/2-480/2, 1080/2-800/2);
        game.setVisible(true);
        game.add(new Gui());        
    }
}

As you can see, I call my class Gui() from my void game().
In my Gui class, I do some painting.

The class looks a little bit like this:

public class Gui extends JPanel implements ActionListener {

    public Gui() {              
        setFocusable(true); 
        ImageIcon i = new ImageIcon(background.jpg);
        background = i.getImage();

        ImageIcon b = new ImageIcon("ball.png");
        ball = b.getImage();
    }   

    public void paint(Graphics g) {
        Graphics2D f2 = (Graphics2D)g;

        f2.drawImage(background, 0, 0, null);
        f2.drawImage(ball, 0, 600, null);
    }   
}

I removed my game logic for reasons of clarity and comprehensibility.

However, if the game is over, I want to dispose my game() JFrame in the MenuFrame Class.

Is there any way to do this clean and smooth?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
joelsa
  • 65
  • 7

1 Answers1

0

There are two ways I can think of to do this off the top of my head. I'm still fairly new at this myself, but I have run across this type of situation myself a few times. Here's how I do it (personally I like option 2, but don't know if this is an accepted OOP technique or not)

Top Down: Garbage Collection MO

1) Have a method within MenuFrame that is called regularly and checks a boolean in the GUI/game object that indicates whether the object is done or not. If it is true, dispose of it.

Bottom Up: Proactive Child MO

2) Have a method within MenuFrame that disposes of the object sent to it as a parameter. Call said method from within "game" passing itself as a parameter. The method could be static to clear up instance issues. If security is an issue (don't want random objects disposed of) specify that if the said method is called, The Child "game" is disposed of. Kind of like a getter/setter method, it limits the possibilities of what exactly can be disposed of.

Please, share your ideas too.

Zaanzabar
  • 28
  • 1
  • 1
  • 10
  • First of all, thank you for your answer Well, yes I originally thought of something like this. I implemented some Code similar to your 1st method, but I thought it was not that pretty. After reading myself through the CardLayout functions, I think that I will use a CardLayout, as it is simply much more beautiful. – joelsa Mar 22 '15 at 21:51