Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to make a "Level 2" with JFrame


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Patriarch K   Members   -  Reputation: 212

Like
0Likes
Like

Posted 21 November 2012 - 07:47 AM

I use JFrame with my game like this:

MAIN-class
public class Main extends JFrame {
public Main(){

JFrame frame = new JFrame();
frame.add(new Level1());
frame.setTitle("Save little L");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1024,768);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public static void main(String[] args){
Main m = new Main();

}
}

...and then I load my Level1()-class as you can see. Now I have made my whole Level 1 and it's finished and you can kill the boss. Now I want to come to level 2, but I don't know how to do. I was wondering about doing some kind of boolean "if (level1Complete == true)" in my MAIN class and then add the argument frame.add(new Level2()); but it doesn't work like that and I don't know how to tidy up the whole frame from everything. The only option I know is to make everything in level1 as a boolean and then copy the code and start the other copy in the same class file when it's supposed to be active.

Do you get my point?

Sponsor:

#2 Lauris Kaplinski   Members   -  Reputation: 841

Like
0Likes
Like

Posted 21 November 2012 - 01:40 PM

I think there is JFrame.removeAll()

But how is your game logic and display implemented? Do you really have to create new JPanel subclass for each game level? I think usually the GUI simply creates canvas (or similar) object and game levels draw onto it.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

#3 de_mattT   Members   -  Reputation: 308

Like
1Likes
Like

Posted 21 November 2012 - 01:56 PM

You could pass a reference to Main into the Level1 constructor, and then call a method in main when the boss is killed. E.g.

public class Level1
{
	 // store a reference to main so that main can be informed when
         // level is complete
	 private Main main = null;

	 // constructor takes initMain as input. initMain is informed when
	 // an event (e.g. boss killed) happens
	 public Level1(Main initMain)
	 {
	 	 main = initMain;
	 }

	 ...

	 public bossKilled()
	 {
	 	 // inform main that the level is complete
	 	 main.handleLevelEvent("BOSS_KILLED");
	 }
}

public class Main extends JFrame
{
	 // move frame outside of Main constructor so that all methods
	 // can see it
	 private JFrame frame = null;

	 public Main()
	 {
		  frame = new JFrame();

		  // Pass reference to Main to level1
		  frame.add(new Level1(this));
		  frame.setTitle("Save little L");
		  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		  frame.setSize(1024,768);
		  frame.setVisible(true);
	 	 frame.setLocationRelativeTo(null);
	 }

	 public static void main(String[] args)
	 {
	 	 Main m = new Main();
	 }

	 public void handleLevelEvent(String eventName)
	 {
	 	 if(eventName == "BOSS_KILLED")
	 	 {
	 	 	 // change to level 2
	 	 	 frame.remove(0);
	 	 	 frame.add(new Level2());
	 	 }
	 }
}

The code above forms the basic mechanism of the Observer (sometimes called Listener) design pattern.
The full Observer pattern is only slightly more complex - check out wikipedia for details.

I notice that the Component class has a protected method "firePropertyChange" which achieves the same kind of thing using the PropertyChangeListener interface. Perhpas you could make use of this feature.

Matt

Edit - Corrected to clarify that the code example is not an example of the observer pattern, but uses the mechanism at the heart of the pattern.

Edited by de_mattT, 22 November 2012 - 06:48 AM.


#4 Patriarch K   Members   -  Reputation: 212

Like
0Likes
Like

Posted 21 November 2012 - 02:42 PM

Hmm, I will try the MVC-thing with observer. I have came across that before but I haven't really understood the thing with firePropertyChange() so I just ignored that. Maybe I should check this out! Thanks! I'll come back if it doesn't work.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS