Results 1 to 8 of 8
  1. #1
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default How to get the "output" from NetBeans into a deployable applet

    Hello All!

    So I'm a first time java developer working on a text adventure project. I used the scanner class and managed to make a pretty serviceable little program that runs well in the output box of the NetBeans IDE. I've included the main method below for a quick glance into how it works, but my real question (and I know it's a big one) is what's the simplest way, using swing or otherwise, to get what runs in the output box to run in a web browser? I know it's possible to deploy code like this as an applet, but I'm not sure how to get the simple text display I'm looking for.

    Any and all help in this issue would be greatly appreciated. Thank you so much for taking the time to read this post.

    Code follows:

    Java Code:
     public class WoundedChapter1 {    
      
        
        public static void main(String[] args) {
            
            player playerOne = new player(1, 0, 0, 0, 0);
            Scanner sc = new Scanner(System.in);
            room gameRoom = new room(0);
            wind wind = new wind(1, 1, 0);
            
            System.out.println("My Little Pony is owned by Hasbro, Lauren Faust and "
                    + "the rest of it's creative team. Please support the official "
                    + "release.\n\n");
            
            System.out.println("You wake up. You feel... drained. So, very drained. "
                    + "You roll over on the cot and thow up. You're suprised to find "
                    + "a bucket waiting for you.\n\n"
                    + "You look up and see an eath pony mare in an orange vest smiling at you."
                    + "\"Welcome to the land of the living\" She says. \"Can you tell me your name?\"\n\n"
                    + "Enter name: ");
                    playerOne.name = sc.nextLine();
            System.out.println("Okay then " + playerOne.name + " We need to get you "
                    + "sorted out. We're all grateful for your sacrifice, but under "
                    + "the circumstances it's going it take us a while to trust you. \n\n");
            System.out.println("Sacrifice? And then you remember. Oh no. Oh no, no...\n"
                    + "1. Your horn\n"
                    + "2. Your wings\n"
                    + "3. Your leg (earth pony)\n\n"
                    + "Choose your race: ");
                    while (playerOne.race != 1 && playerOne.race != 2 && playerOne.race != 3) {
                    playerOne.race = sc.nextInt();
                    switch(playerOne.race){
                        case 1:
                            System.out.println("You reach up a hoof and poke the"
                                    + "shattered remenants of your horn. Instantly a"
                                    + "flare of pain shoots down your spine and you cry out in agony.\n"
                                    + "The mare snatches your hoof away. \"Don't touch that!\""
                                    + "The mare looks you dead in the eye. \"Look, I caulked "
                                    + "it in time, so it should heal up fine, but if you poke it"
                                    + "or try and throw around magic, that could change. Fast.\" \n"
                                    + "Your eyes widen. No magic? Oh this is bad, very bad...\n\n");
                        break;
                        case 2:
                            System.out.println("You instinctively try to spread your wings and cry out in pain"
                                    + "as broken bomes struggle against tight bandages. You can't... you can't"
                                    + "The mare puts a hoof on your shoulder and something about her expression "
                                    + "stops you. She looks strangely... exhausted and sad. Never the less, when she"
                                    + "meets your eyes she speaks with solid steel. \"Give it time. You will fly "
                                    + "again.\"\n\n");
                        break;
                        case 3:
                            System.out.println("You throw back the sheets and scream in terror. Your left hind leg now "
                                    + "ends in a bandaged stump just after your hock. For Season's sake you were a farmer!"
                                    + "How could you live if you couldn't work? How could you- \n"
                                    + "The mare physically grabs your head and twists it to face her. \n"
                                    + "\"Listen to me. You are going to WALK out of here. We don't care who's side you"
                                    + "were on.\n\n");
                            
                    }
                    }
            System.out.println("The mare puts a hoof on your chest. Your pounding heart slows and you feel like you can breathe again.\n\n"
                    + "The mare pulls her hoof back and looks down. \"Sorry, I shouldn't have done that... Then again, give a pony an apple...\" "
                    + "She smiles. \"I'm going to teach you a little trick. I'm not sure if they still teach it in basic training, so stop me if you've heard this one.\"\n\n"
                    + "\"Take a deep breath and close your eyes. I want you to focus on every part of your body piece by piece to feel out any"
                    + " possible injuries. Then, feel the weight on your body and try to remember each object that you're carrying"
                    + "Finally, try to picture the room around you, remember who's there and what objects are lying around. "
                    + "I want you to try and do all of that before you open your eyes. Are you ready?\n"
                    + "1. Breathe deep\n"
                    + "2. You're not going to do anything funny while my eyes are closed right?\n\n");
                    int tutorialChoiceOne = 0;
                        while (tutorialChoiceOne != 1 && tutorialChoiceOne !=2) {
                        System.out.println("Choose an option: ");
                        tutorialChoiceOne = sc.nextInt();
                            switch (tutorialChoiceOne){
                                case 1:
                                     //I'd like to condense these two methods into one if I can
                                    playerOne.breatheDeep();
                                    gameRoom.displayRoom();
                                break;
                                case 2:
                                    System.out.println("The mare smirks. \"No promises you flirt.\" \n\"In all seriousness though, it's just a breathing"
                                    + "exercise. You have my word, your virtue will be preserved.");
                                break;
                    }
                    }
                    if (tutorialChoiceOne == 2) {
                        System.out.println("1. Breathe deep\n"
                                + "2. I don't know...\n");
                        int tutorialChoiceOneA = 0;
                        while (tutorialChoiceOneA != 1 && tutorialChoiceOneA != 2){
                        System.out.println("Choose an option: ");
                        tutorialChoiceOneA = sc.nextInt();
                            switch (tutorialChoiceOneA){
                                case 1:
                                    playerOne.breatheDeep();
                                    gameRoom.displayRoom();
                                    tutorialChoiceOne = 1;
                                break;
                                case 2:
                                    System.out.println("The mare sighs. \"Okay, I guess it is a little silly...");
                            }
                        }
                    }
                    if (tutorialChoiceOne == 1){
                        System.out.println("The mare smiles. \"Perfect, your heartbeat is down already.\"");
                    }
            System.out.println("\"Now, do you have any allergies? Are you on any medication?\""
                    + "You shake your head. The mare gets up and walks over to a metal cabinet."
                    + "She pulls a key from her vest pocket and undoes a sturdy chain. as"
                    + "she opens the door, you see a scattering of medical supplies and a few"
                    + "bottles of various medications. She takes a small red vial from what"
                    + "looks like a rapidly diminishing stock and locks the door behind her."
                    + "\"Normally we like to reserve these for rather extreme emergencies,\""
                    + "She eyes the bottle uncertainly for a moment and sighs. \"I think you've"
                    + "earned one.\" She slips the bottle into your shaking forehoof. You'd"
                    + "seen something like this before once. When your batallion stopped a"
                    + "zebra smuggler."
                    + "\"It's a healing potion,\" the mare explains. \"It should counteract the"
                    + "blood loss and a smooth over your more minor injuries.\"\n");  
             System.out.println("1. Thank you\n"
                                + "2. Why should I trust your smuggled drugs?\n");
             int tutorialChoiceTwo = 0;
                        while (tutorialChoiceTwo != 1 && tutorialChoiceTwo !=2) {
                        System.out.println("Choose an option: ");
                        tutorialChoiceTwo = sc.nextInt();
                        }
             if (tutorialChoiceTwo == 2){
                 System.out.print("\nThe mare groans. \"A. it's not smuggled, and B... Look, "
                         + "the fact of the\nmatter is, you're not going to be moving very far "
                         + "without three weeks of\nbed rest or some healing magic, and I don't "
                         + "know if you've noticed, but\nI'm not exactly sitting on a big pile of "
                         + "reading material. What do I\nhave to do here? Drink half of it?\n\n"
                         + "1. No... \n"
                         + "2.Yes, yes you do.\n\n");
                 int drinkChoice = 0;
                 while (drinkChoice != 1 && drinkChoice != 2){       
                 System.out.print("Choose an option: \n");
                 drinkChoice = sc.nextInt();
    		switch (drinkChoice)
    		{
    		case 1:
    		playerOne.getHealingPotion(1);
                    wind.admire(1);
    		System.out.print("The mare nods. \"Good answer, those things are worth more than solid gold."
                            + "I didn't decide to give you one lightly.\"\n\n");
    		break;
                    case 2:
    		playerOne.getHalfHealingPotion(1);
    		wind.disdain(1);
    		System.out.print("The mare groans. \"You're really going to make me waste medical supplies,"
                            + "MAGICAL medical supplies, just because you don't trust me?\" "
                            + "Your\nexpression does not waver. "
                            + "She sighs. \"I'm knocking you down to eight lives saved for these horseapples.\" "
                            + "She picks the potion from your hoof\nand carefully measures out half of it into a cup. "
                            + "After a moment's\nhesitation she downs it. ... Nothing happens. \n"
                            + "The mare glowers at you.\"There, are you happy?\"\n\n");
    		break;
    		}
                 }
            }
             System.out.println("You fumble with the potion, it sort of... blurs as you try to bring it"
                                            + "to your lips.\n"
                                            + "The mare winces in sympathy. \"Why don't you try the breathing exercise"
                                            + "one more time, it will help you reorient to where things are.\"\n"
                     + "1. Breathe Deep"
                     + "2. Breathe Deep, but keep an eye open so she doesn't pull anything");
       int tutorialChoiceThree = 0;    
         while (tutorialChoiceThree != 2 && tutorialChoiceThree != 1)
        {
        System.out.println("Choose an option: \n");
        tutorialChoiceThree = sc.nextInt();
        switch (tutorialChoiceThree)
        {
            case 1:
                playerOne.breatheDeep();
                gameRoom.displayRoom();
                break;
            case 2:
                playerOne.breatheDeep();
                gameRoom.displayRoom();
                System.out.println("Your cracked eyelid lets you catch a glimpse of the mare sitting next to"
                        + "you... who does absolutely nothing. She sticks her tounge out when she sees you peeking.\n\n");
            break;
        }
        }
        while (playerOne.health < 5)
        {
            System.out.println("You fail to take the potion. You, a soldier of Queen Season's army, lack"
                    + " the mental discipline to guide a bucking bottle to your lips. You feel "
                    + "horribly ashamed, and you hang your head... Your stomach tells you that "
                    + "was a mistake.\n\n"
                    + "The mare gives you a level look, \"You took a fire spell and lived. I "
                    + "know you can do this\"\n\n"
                    + "Her words ring a little hollow though, as her eyes are locked nervously "
                    + "on the potion out of fear that it will drop \n\n"
                    + "1.Breathe Deep\n"
                    + "2.I need your help\n\n");
            int tutorialChoiceFour = 0;
            while (tutorialChoiceFour != 1 && tutorialChoiceFour != 2) {
            System.out.println("Choose an option: ");
            tutorialChoiceFour = sc.nextInt();
            switch (tutorialChoiceFour)
            {
                case 1:
                System.out.println("You draw up some hidden strength.\n\n");
                playerOne.heal(1);
                playerOne.breatheDeep();
                gameRoom.displayRoom();
                break;
                case 2:
                System.out.println("The mare nods. \"Okay.\"\n\n"
                        + "She reaches out and guides the bottle to your mouth, her movements much "
                        + "steadier than those of your shaking hooves.\n\n");
                if (playerOne.healingPotion ==1)
                {
                playerOne.heal(8);
                System.out.println("You feel a strong surge of energy and a distinctly itchy feeling as your wounds"
                        + " knit themselves together. You feel energized, you feel great! "
                        + "You look at your biggest wound, the one you're REALLY worried about... "
                        + "You sigh. Well, at least you're a healthy cripple.\n\n");
                playerOne.healingPotion = 0;
                }
                if (playerOne.halfHealingPotion ==1)
                {
                playerOne.heal(3);
                System.out.println("You feel a weak surge run through you and a distincly itchy feeling as"
                        + " your wounds start to knit themselves together. Start being the operative "
                        + "word. You still feel like garbage, but you think you can move now.\n\n");
                playerOne.halfHealingPotion = 0;
                }
                System.out.println("The mare gives you an odd look. \"Don't get me wrong. I'm here to "
                        + "help, but why didn't you want to try that on your own?\"\n\n"
                        + "1. I didn't think I could do it. \n"
                        + "2. My pride wasn't worth risking the potion.\n\n");
                int tutorialChoiceFive = 0;
                while (tutorialChoiceFive != 1 && tutorialChoiceFive !=2)
                {
                    System.out.println("Choose an option: ");
                    tutorialChoiceFive = sc.nextInt();
                    switch (tutorialChoiceFive)
                    {
                        case 1:
                        System.out.println("It felt good to be healthy... but not so good that you acted so weak in "
                                + "front of... of... Well \'The Enemy\' certainly wasn't the right phrase "
                                + "any more. You weren't sure how to think of the stange mare.\n\n"
                                + "She put a hoof lightly on your shoulder. \"Sometimes it takes a lot "
                                + "of strength to admit you need help.\"\n\n");
                        break;
                        case 2:
                        System.out.println("It felt good to be healthy... but not so good that you acted so weak in "
                                + "front of... of... Well \'The Enemy\' certainly wasn't the right phrase "
                                + "any more. You weren't sure how to think of the stange mare."
                                + "\n\nThe mare glances back at the locked cabinet. She smiles. "
                                + "\"You sounded a bit like the organizer there.\"\n\n");
                        wind.admire(1);
                        break;
                    }
                }
                break;
    
            }
        }
        System.out.println("The mare looks you over. \"Okay, it looks like you've healed up enough "
                + "to move. C'mon, there's some ponies that want to meet you. \n\n");
        if (playerOne.race == 3)
        {
            System.out.println("You start to roll yourself over, but the mare stops you. You look down "
                    + "at the empty space where your leg should be.\n"
                    + "Oh, right.\n"
                    + "The mare leans over and undoes the bandages, the healing potion has left "
                    + "a clean stump behind, but nothing more. You feel very numb looking at "
                    + "it. \n\n"
                    + "She touches the end gingerly. \"Do you feel any pain?\" \n"
                    + "You shake your head. the touch is disturbing, but harmless. \n\n"
                    + "\"Good\" she says \"That means that there's probably no infection, and the cut "
                    + "was through the lower portion of your metatarsus, so your mobility prospects are good.\" \n\n"
                    + "The mare goes back over to the cabinet and retrieves a bizzare looking wooden and metal "
                    + "contraption with a horse shoe on the end that you swiftly realize is a hoof prosthesis. "
                    + "She carries it over to you and places it gingerly on the stump. "
                    + "She then proceeds to fit it carefully to your leg and tighten it down with a series of straps. "
                    + "She makes note of your exact measurements. \n\n"
                    + "\"Does that feel okay?\" \n You nod. Then it's time. Slowly, carefully, and with a "
                    + "lot of help from the mare, you manage to stand up. You take a few steps forward. "
                    + "You're a little wobbly at first, but you can walk.\n\n"
                    + "The mare smiles. \"What'd I tell you? We quadrapeds pop right back up\" \n\n");
        }
        else if (playerOne.race == 1 || playerOne.race == 2)
        {
            System.out.println("Your muscles are stiff, and your injuries still limit your abilities, "
                    + "but you manage to roll out of bed. \n\n");
        }
        int tutorialChoiceSix = 0;
        while (tutorialChoiceSix != 1)
        {
            System.out.println("The mare nods, \"Okay then, let's get you to the stallion in charge.\" \n"
                    + "1. Okay, let's do it!\n"
                    + "2. Can I ask you some questions first? \n\n");
            System.out.println("Choose an option: \n");
            tutorialChoiceSix = sc.nextInt();
            switch (tutorialChoiceSix)
            {
                case 1:
                wind.setTime(1);
                System.out.println("\"Allright then, time to meet the people you saved.\"\n\n");
                System.out.println("End Tutorial. Wind's oppinion of you is: ");
                if (wind.opinnion == 2)
                {
                    System.out.println("FAVORABLE\n\n");
                }
                if (wind.opinnion == 1)
                {
                    System.out.println("SLIGHTLY FAVORABLE\n\n");
                }
                if (wind.opinnion == 0)
                {
                    System.out.println("NEUTRAL\n\n");
                }
                break;
                case 2:
                System.out.println("Fire away.\n\n");
                wind.haveConversation(0);
                break;
            }
        }
             //System.out.println("Winds oppinion:" + wind.opinnion + "\n");
        }
      
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to get the "output" from NetBeans into a deployable applet

    An Applet is a visual component and doesn't take input form the command line. You need to start by redeveloping your game with a GUI, preferably one housed in a JPanel that you can then add to a JApplet, a JFrame or show in a JOptionPane's dialog.

    Here's a good learning resource: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) (ignore the second section, what they claim about it isn't true).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by DarrylBurke View Post
    An Applet is a visual component and doesn't take input form the command line. You need to start by redeveloping your game with a GUI, preferably one housed in a JPanel that you can then add to a JApplet, a JFrame or show in a JOptionPane's dialog.

    Here's a good learning resource: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) (ignore the second section, what they claim about it isn't true).

    db
    Seriously? There's no simple way to translate what gets output in the command line to an output in a text box? Ouch. Okay, I think I could create a GUI in swing to accept inputs when the player makes decisions, but is there some specialized method I need other than System.out.println() to get the text I want displayed into a text box?

    Thank you for your help in general though!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by chaucer345 View Post
    Seriously? There's no simple way to translate what gets output in the command line to an output in a text box?
    Seriously. Your code above is nothing but a large static main method. To have this sort of flexibility, you should at least create an OOP-compliant set of classes that have non-static methods and fields, and behaviors that can be used on the command line or in a GUI.

    Okay, I think I could create a GUI in swing to accept inputs when the player makes decisions, but is there some specialized method I need other than System.out.println() to get the text I want displayed into a text box?
    Swing doesn't have a text box, but perhaps you meant JTextArea? If so, then yes, it has the setText(String text) and append(String str) methods. Please check out the Java API and the Swing tutorials for the details.

  5. #5
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by Fubarable View Post
    Seriously. Your code above is nothing but a large static main method. To have this sort of flexibility, you should at least create an OOP-compliant set of classes that have non-static methods and fields, and behaviors that can be used on the command line or in a GUI.



    Swing doesn't have a text box, but perhaps you meant JTextArea? If so, then yes, it has the setText(String text) and append(String str) methods. Please check out the Java API and the Swing tutorials for the details.

    Okay, I guess that makes sense.

    It should be noted that the code above is just the main method, there are a lot of non-static methods and fields in other classes associated with the package.

    Also, I'm very sorry, but I'm rather new at this. What do you mean by OOP compliant?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by chaucer345 View Post
    Okay, I guess that makes sense.

    It should be noted that the code above is just the main method, there are a lot of non-static methods and fields in other classes associated with the package.
    Good.

    Also, I'm very sorry, but I'm rather new at this. What do you mean by OOP compliant?
    I mean by creating classes that allow the creation of objects that have states (non-static fields) and inheritable behaviors (non-static methods), possibly like your player, room, and wind classes. Note that in order to comply with Java naming rules, those class names should all begin with an upper-case letter. This may not matter much to you, but it will matter if you ask others to help you as complying with these conventions will allow us to be able to more quickly and more completely understand your code.

    Also, note that there is a way to redirect standard output away from System.out and to your JTextField, but it involves a little trickiness including your creating a PrintStream object that directs all text to the JTextField. While this can be done via System.setOut(PrintStream out), I suggest that you avoid this for now and instead us the JTextArea methods that I showed above.

  7. #7
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by Fubarable View Post
    Good.



    I mean by creating classes that allow the creation of objects that have states (non-static fields) and inheritable behaviors (non-static methods), possibly like your player, room, and wind classes. Note that in order to comply with Java naming rules, those class names should all begin with an upper-case letter. This may not matter much to you, but it will matter if you ask others to help you as complying with these conventions will allow us to be able to more quickly and more completely understand your code.

    Also, note that there is a way to redirect standard output away from System.out and to your JTextField, but it involves a little trickiness including your creating a PrintStream object that directs all text to the JTextField. While this can be done via System.setOut(PrintStream out), I suggest that you avoid this for now and instead us the JTextArea methods that I showed above.
    Understandable, I'm sorry about the class names, I'm still learning the conventions. I do have one more question though. I know the basics of creating a JFrame GUI. Will I be able to safely create such a GUI for my program and then later easily convert the stand alone application to an applet? Or should I be using some other library/class/whatever for building a web-page embedded application GUI?

    Thank you once again for your help, it really means a lot.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to get the "output" from NetBeans into a deployable applet

    Quote Originally Posted by chaucer345 View Post
    Will I be able to safely create such a GUI for my program and then later easily convert the stand alone application to an applet?
    Create your GUI in a JPanel. This panel can be added to or set as the contentPane of a JFrame or a JApplet, r even displayed in a JOptionPane's dialog.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 2
    Last Post: 10-26-2011, 08:52 PM
  4. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

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