Results 1 to 9 of 9
  1. #1
    xsimsyx is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default Text adventure setup

    Hi,I am writing a text adventure in a Java applet using netbeans.6.8.
    I am using a text area for displaying the text to the user,a text field for user input and a enter button for the action events.I am having difficulty in displaying the text to the user in sequences after they have entered there input.At the moment all the text is displayed in the text area which I dont want to happen,I would like user input then display the next block of text,user input then display the next block of text etc etc.Also in a scrollbar tutorial which is added and works manually the setcarat to display isnt working automatically so is commented and a progress bar which I plan to use as a health meter.If anybody has the time would they mind showing me where I am going wrong.
    Many thanks
    Paul
    Attached Files Attached Files

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    what you mean saying "what am I doing wrong" ? It is all depends of the effect you want to achieve but for some reason you can't :)
    sorry... but the question form is quite confused in some blabla manner I think...
    What exactly you want your application to have? Is there some exception or so?
    It is a forum but an Institute lecture room :) my pardon...

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

    Default

    Your code:
    Java Code:
       JScrollPane myscrollpane = new JScrollPane(mytextarea);
       add(mytextarea);
    For starters if you add a JTextArea to a JScrollPane, then you need to add the JScrollPane not the JTextArea to the applet for scrolling to occur:
    Java Code:
       JScrollPane myscrollpane = new JScrollPane(mytextarea);
       //add(mytextarea);
       add(myscrollpane);
    This may not work though since you are using null layout and I don't see that you've set the JScrollPane's bounds. You really should avoid doing using null layout you know.

    The Sun Swing Tutorials will tell you pretty much what you need to know about creating GUIs, but you may want to hit some of the other Sun Java tutorials as well. There's a wealth of knowledge waiting to be learned. Much luck!

  4. #4
    xsimsyx is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default text adventure

    Hi,what I am aiming for is for the text area to display the text of the game,for example.. enter your name,the user then inputs their name which then the textarea then displays the next block of the game..enter a number between 1 and 20,and this is how I would like the game to run.Currently the text area displays all the game which I am unable to display 1 part of the game at a time following user input.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package paratrooper1;
    
    
    //class libraries
    import java.applet.Applet;
    import java.awt.event.*;
    import javax.swing.JScrollPane;
    import java.awt.*;
    import javax.swing.JProgressBar;
    
    /**
     *
     * @author Paul
     */
    public class Paratrooper1 extends Applet implements ActionListener{
    
        //variables
        String name;
        String answer1getnumber;
        String block2;
        JProgressBar progresshealth;
        TextField userinput;
        TextArea mytextarea;
        JScrollPane myscrollpane;
        Button Enterbutton;
    
        public void init() {
    
        setLayout(null);
        setBackground(Color.BLUE);
    
        userinput = new TextField("");
        userinput.setBounds(110,550,180,20);
        add(userinput);
    
        Enterbutton = new Button("Enter");
        Enterbutton.setBounds(290,550,60,20);
        add(Enterbutton);
      
        mytextarea = new TextArea("",80, 50,1);
        mytextarea.setBounds(60,400,350,140);
        mytextarea.setEditable(false);
        mytextarea.setFont(new Font("Serif", Font.BOLD, 14));
    //   mytextarea.setLineWrap(true);
    //   mytextarea.setWrapStyleWord(true);
    //   mytextarea.add(myscrollpane);
       JScrollPane myscrollpane = new JScrollPane(mytextarea);
    
       add(myscrollpane);
       add(mytextarea);
       
       progresshealth = new JProgressBar(500,500);
       progresshealth.setValue(0);
       progresshealth.setMinimum(0);
       progresshealth.setMaximum(100);
    
    //progresshealth.setStringPainted(true);
       add(progresshealth);
       progresshealth.setVisible(true);
       //I would like to add text paragraph by paragraph but settext does not allow for this???nor insert
      //append..ie.mytextarea.append("string");
       //Start game
    
    
       //I WOULD LIKE TO DISPLAY THIS FIRST AND WAIT FOR USER TO INPUT THERE NAME
       //CURRENTLY BLOCK 1 AND 2 ARE SHOWING AS WELL
    
       mytextarea.setText("                               Paratrooper \n\nPlease enter your name");
    
       name=userinput.getText();
      // if false wait;
      // if true continue;
       //next block of code to run after enter name
    
       //SHOW AFTER NAME INPUT
       //block1
       mytextarea.append("\nPlease enter the following for your personalised adventure\nEnter a number below 20");
       userinput.getText();//= answer1getnumber int;
       answer1getnumber=userinput.getText();
    
    
      //SHOW AFTER ENTER NUMBER.ETC.ETC.
      // block2
       mytextarea.append("\nNext block of code to run\nNext block of code to run\nask question");
    
       //mytextarea.setCaretPosition(mytextarea.getDocument().getLength());
    
       setSize(600,600);
         
         Enterbutton.addActionListener(this);
    
         // TODO start asynchronous download of heavy resources
        }
        /**
         * Initialization method that will be called after the applet is loaded
         * into the browser.
         */
        public void actionPerformed(ActionEvent evt) {
         
            if(evt.getSource()== Enterbutton){
               if (mytextarea.getText() == name);
               mytextarea.setText(mytextarea.getText()+"\n\nWelcome to Paratrooper "+userinput.getText());
               userinput.setText("");
             
            }
            else
                {
              if (userinput.getText() == answer1getnumber);
                 userinput.setText("");
                  }
    
         // else etc.etc
    
        // TODO overwrite start(), stop() and destroy() methods
            }
    
        }
    Moderator Edit: Code tags corrected -- back slash added to final tag
    Last edited by Fubarable; 03-14-2010 at 03:17 PM. Reason: Moderator Edit: Code tags corrected -- back slash added to final tag

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

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

    Default

    OP, please read up on the proper etiquette for notifying us about cross-posts here: JavaRanch - Be Forthright When Cross Posting To Other Sites

    Your cooperation on this would be greatly appreciated (and the converse is true!).

    Best of luck.

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

    Default

    For one thing, these semicolons are killing you:
    Java Code:
        if (evt.getSource() == Enterbutton) {
          if (mytextarea.getText() == name); // This semicolon! ****
            mytextarea.setText(mytextarea.getText() + "\n\nWelcome to Paratrooper " + userinput.getText());
          userinput.setText("");
    
        } else {
          if (userinput.getText() == answer1getnumber); // this semicolon! ****
            userinput.setText("");
        }

    You may wish to scrap this code and start over as there are many many problems here. You're mixing Swing with AWT, adding visual components to two places at once, using a massive init method, not enclosing your if /else blocks in curly braces and having errors because of this, using null layout, using == to compare Strings instead of equals(...), ....

    You may wish to go through the tutorials first to get a better handle on the basics, but again scrap this program and start over. Sorry.
    Last edited by Fubarable; 03-14-2010 at 03:39 PM.

  8. #8
    xsimsyx is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Hi,thanks for replying,the brackets are removed but the main problem remains which is displaying the next block of the game text after the user input.As it is at the moment all the game text is displayed.
    Thanks for your help.

  9. #9
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    Java Code:
    add(myscrollpane);
       add(mytextarea);
    :eek:

    Say "double juice", buddie :) Add some specified component either myscrollpane which contains JTextarea or mytextarea with no scrollpane :cool:

Similar Threads

  1. please help me to create setup
    By javastuden in forum AWT / Swing
    Replies: 4
    Last Post: 11-10-2009, 05:58 AM
  2. Replies: 0
    Last Post: 07-07-2009, 06:22 AM
  3. Eclipse Environment setup
    By mudgil.gaurav in forum Eclipse
    Replies: 1
    Last Post: 09-18-2008, 07:10 AM
  4. Setup RMI service on Ant
    By vaskarbasak in forum Advanced Java
    Replies: 0
    Last Post: 07-24-2008, 02:25 PM
  5. Setup
    By Mir in forum New To Java
    Replies: 9
    Last Post: 06-27-2008, 11:12 AM

Posting Permissions

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