loading/calling classes from inside a class

Im making a game thats a bunch of microgames( 5 second games), what i need help with is can i load a class inside of another class? ive read about the class loader but the code escapes me and i wouldnt be able to implement it if i tried, my teacher is partly to blame as he was only a week ahead of us when teaching us java.anywayshelp would be appreciated

code:

publicclass Indexextends MiniTV{

privatestaticvoid runGame(){

// i need to call other classes on random from here

}

privatestaticvoid createAndShowGUI(){

JFrame frame =new JFrame("MiniTV");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addComponentsToPane(frame.getContentPane());

Insets insets = frame.getInsets();

frame.setSize(810,630);

frame.setVisible(true);

strt.addMouseListener(new MouseAdapter(){

publicvoid mouseClicked(MouseEvent me){

runGame();

}

});

}

publicstaticvoid main(String[] args){

javax.swing.SwingUtilities.invokeLater(new Runnable(){

publicvoid run(){

createAndShowGUI();

}

});

}

}

[2290 byte] By [KoPoWa] at [2007-11-27 6:33:02]
# 1

Not sure if I understand you correctly or not.

private static void runGame(){

Game g = new Game();

g.doStuff();

}

floundera at 2007-7-12 17:58:48 > top of Java-index,Java Essentials,New To Java...
# 2

You have a set of games. And you want to invoke one of them randomly. Is that what you want?

You can do it like this if you are the one that write all those tiny games.

Write an interface to be implemented by all games. which should look like this

public interface Game{

public void start();

//Add more methods if you want.

}

Now the main class of each and every game must implement above interface.

Then you need a collection of available games. Each game can be represented by the Class object of its main class which implements the above interface.

You can easily do that using an array list.

ex:-

public class GameColection{

ArrayList<Class> gameClasses = new ArrayList<Class>();

/ /You have to add the available game classes to the array list

public Game getGameInstance(int i){

return (Game)gameClasses.get(i).newInstance();

}

//you can use above method to get a game instance by index

}

Now you can get a game instance randomly by providing a random index to getGameInstance method and then invoke start on that game.

Every game should implement the start method.

LRMKa at 2007-7-12 17:58:48 > top of Java-index,Java Essentials,New To Java...