Results 1 to 20 of 36
Thread: How load other java codes
- 07-21-2010, 05:04 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
How load other java codes
Okay I'm making a game applet and I want differen't levels, so is there a way to switch out which java code is running? So like say I hava a .class file named main which has my main screen with buttons on it, and then when you click one of the buttons I would want it to load level1.class file. Can I do this? If so how?
- 07-21-2010, 05:16 PM #2
Not sure what you are asking.
You can "load" a class by creating an instance of it by using the new statement.
Put that statement in the listener method for the button.
- 07-21-2010, 05:20 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Usually in level games each level is a data file, not an extra bit of code. The game would then read the new level data in and go from there.
- 07-21-2010, 05:27 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
So like all the levels are different java and .class files. I would have the menu screen that would have a button. When you press the button I want the menu screen to disappear and the level to appear, and the level is a whole different set of .java and .class files. I want to do this for ease of keeping my codes in order. Is it possible to do this?
- 07-21-2010, 05:30 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
How would I make and use a data file? I have never heard of those.
- 07-21-2010, 05:42 PM #6
Are the "screens" you want to change components displayed in the Applet panel or are they external frames created by the applet?
- 07-21-2010, 05:46 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
To take your first post, you can simply download all the classes in one jar in one go. How big are you expecting this to be? I can't imagine it'll be huge.
What you're talking about is simply displaying a different screen. One screen would be the menu, which would be hidden and a "level" screen would be displayed. That's simply your GUI at work.
Now, your level screen (JPanel? See Swing) would display whatever is required for that level. That data, in most games, is held in a data file. Take Doom for example. Each level there is some form of data file. The game has a set of code that can read one of these files and turn it into a level the player can play.
As a simple example (stealing slightly from someone else asking a question on here):
That's my (rather simple) level map, and the "game" is simply to walk from the bottom to the top. '#' is a wall, so I can't walk through that. So my code can read this level file and knows how to display this to the user and what the user is allowed to do. Since the game has the basic rules, I can write as many levels as I want and I know the system can handle it (within reason). I also don't have to write individual classes for each level.Java Code:##### #### #### #### ##### #### ##### ### ###### ### ##### ### ##### ####
Does that make sense or am I talking fluent gibberese...?
- 07-21-2010, 06:48 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
To answer norm all the things would be displayed in the same applet panel.
To answer tolls, Your kinda confusing me, how would I put all the class files into a jar file? How would I even run the jar file in an applet? Can you explain it a little simpler for me? I'm used to c++ and robots.
- 07-21-2010, 10:08 PM #9
This section of the forum is for Applets.How would I even run the jar file in an applet
Have you looked at the HTML tag: <APPLET?
It has an "archive" attribute.
If you put all the apps classes in a jar file you and point the archive=YOURJARFILE.jar
to your jar file, then the browser will be able to find all the classes you have put into the jar file.
Use the 'jar' command to put .class files into a jar file.
How do you "run" your applet code now?
Look at the html for any web page that uses an applet for examples.
- 07-21-2010, 10:27 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
So then can I switch which .class file in the jar file is running?
- 07-21-2010, 10:32 PM #11
The browser loads the first class based on the <APPLET code= entry.
After that the JVM will load the classes you request via the new statement (or other reference) from the classpath. The jar file in the archive= tag is on the classpath.
- 07-21-2010, 10:36 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
So how do I use jvm to load the classes I request? Can you post an exampel because you're starting to confuse me.
- 07-21-2010, 10:40 PM #13
How do you "request" a class? Give me some code that "requests" a class.the classes I request?
Most of what I am talking about is just a description of how the JVM works.
You put all your classes on the class path and then request them as you need them.
The JVM will find them by looking for them on the classpath.
- 07-21-2010, 10:48 PM #14
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
- 07-21-2010, 10:57 PM #15
How do you use any class? This is a very basic question. Everything you do in a java program is done in a class.how would I use the different classes
Give me some code that "requests" a class.
- 07-21-2010, 11:00 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I don't know how to request a class, that's what I'm having troubles with. I want one class file in a jar file to be switched with the other class file in the web page coding.
- 07-21-2010, 11:01 PM #17
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
By requesting a class file it would be the equivilent of using the <applet> code in a webpage to request the .class file you put in it.
- 07-21-2010, 11:09 PM #18
Sorry, your post above implied that you "requested" a class.
Ok. Forget about requesting a class and how the JVM works.
Write your code as normal, put it in a jar file, create an HTML file with the <APPLET tag, put the jar file in the same folder with the HTML file and open the HTML file in a browser.
- 07-21-2010, 11:11 PM #19
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
Can you explain how I would execute the different class files in the jar files at different times?
- 07-21-2010, 11:16 PM #20
You don't need to worry about that. The JVM takes care of it.
Write your program to use classes as you normally would.
I think you should start with the basics before going on. Write a simple HelloWorld Applet and get that to execute. Just use System.out.println() to write to the browser's java console. When you get that to work, you'll understand how the HTML, APPLET tag, and browser work. Then put the helloworld class file in a jar, change the HTML and get that to work.
Then you can move on to the more complicated things.
Similar Threads
-
how to protect java codes from being seen?
By anthrax in forum EclipseReplies: 5Last Post: 10-14-2009, 03:43 PM -
Missing 4 ISO 3166-1 codes in java.util.Locale
By fkeller in forum Advanced JavaReplies: 1Last Post: 01-16-2009, 05:41 PM -
java codes
By Balajee in forum AWT / SwingReplies: 1Last Post: 09-30-2008, 05:04 PM -
Algorithm statement in my Java codes
By javanewbie in forum New To JavaReplies: 2Last Post: 06-24-2008, 02:52 PM -
How to get error codes using java program
By kasipandian in forum Web FrameworksReplies: 10Last Post: 05-25-2008, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks