Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default 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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    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.

  4. #4
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    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?

  5. #5
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    How would I make and use a data file? I have never heard of those.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Are the "screens" you want to change components displayed in the Applet panel or are they external frames created by the applet?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,008
    Rep Power
    20

    Default

    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):
    Java Code:
    ##### ####
    ####  ####
    ##### ####
    #####  ###
    ###### ###
    #####  ###
    ##### ####
    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.

    Does that make sense or am I talking fluent gibberese...?

  8. #8
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    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.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    How would I even run the jar file in an applet
    This section of the forum is for Applets.
    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.

  10. #10
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    So then can I switch which .class file in the jar file is running?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    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.

  12. #12
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    So how do I use jvm to load the classes I request? Can you post an exampel because you're starting to confuse me.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    the classes I request?
    How do you "request" a class? Give me some code that "requests" a class.

    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.

  14. #14
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    After that the JVM will load the classes you request via thenew statement (or other reference) from the classpath.
    You said that, how would I use the different classes?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    how would I use the different classes
    How do you use any class? This is a very basic question. Everything you do in a java program is done in a class.



    Give me some code that "requests" a class.

  16. #16
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    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.

  17. #17
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Quote Originally Posted by pizzadude223 View Post
    So how do I use jvm to load the classes I request? Can you post an exampel because you're starting to confuse me.
    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.

  19. #19
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Can you explain how I would execute the different class files in the jar files at different times?

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. how to protect java codes from being seen?
    By anthrax in forum Eclipse
    Replies: 5
    Last Post: 10-14-2009, 03:43 PM
  2. Missing 4 ISO 3166-1 codes in java.util.Locale
    By fkeller in forum Advanced Java
    Replies: 1
    Last Post: 01-16-2009, 05:41 PM
  3. java codes
    By Balajee in forum AWT / Swing
    Replies: 1
    Last Post: 09-30-2008, 05:04 PM
  4. Algorithm statement in my Java codes
    By javanewbie in forum New To Java
    Replies: 2
    Last Post: 06-24-2008, 02:52 PM
  5. How to get error codes using java program
    By kasipandian in forum Web Frameworks
    Replies: 10
    Last Post: 05-25-2008, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •