Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 05:43 PM
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
Help with class loading in java
Hi, My friend and I are trying to make this text-based tactics game with java.

We are trying to make an user interface, via console, as interactive as possible. So, we need quiet of commands. How would you manage all these commands? I was thinking about shoving all the classes into Object[] and calling them as users need it, but we hit the wall when I figured out that it is not possible to instance them all automatically.

What is a good way to manage insane amount of class files and calling them as they fit? Or, should i just paste the whole program of 20000 if/else statements?

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 09:51 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
OK If I understand correctly, in your game if I typed "kill" you want it to match up with a class that handles the "kill" command and then runs some method on that class to perform the operation right?

This is how I would do it. Create a file of commands and the class name ex.
kill=KillClass
run=RunClass
...

I would make all command classes implement an interface or inherit from a common base class. For my example I'll call it the Command interface.

Then in the startup read the file and load the commands into a Map<String, Command>. You can do this by reading each line and splitting it on the '='. The left side is the key in the map and the right you use to instantiate the class like so:
Code:
Map<String, Command> commandMap = new HashMap<String, Command>(); Class commandClass = this.getClass().getClassLoader().loadClass(rightSideClassName); Command com = (Command)commandClass.newInstance(); commandMap.put(commandKey, com);
Then when they type in a command just look it up in the map, pull out the Command class and call the method.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Images - Imp Thulasiraman Advanced Java 0 01-28-2008 10:33 AM
Loading An Image Help Please! shaungoater Java 2D 2 01-09-2008 09:14 AM
Java Applet loading bar genette Java Applets 2 12-27-2007 04:39 AM
Help Loading Up Pictures marco Java Applets 3 08-14-2007 11:32 PM
How can I set a time out on the loading process paul Advanced Java 1 08-07-2007 05:54 AM


All times are GMT +3. The time now is 09:58 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org