Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (525)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  TextArea 12 - Me - 0  (Read 1419 times)
0 Members and 1 Guest are viewing this topic.
Offline straydoginc

Junior Devvie





« Posted 2005-11-07 21:04:37 »

Okay. So after getting the delayed text / thread thing worked out I am presented with another on going problem. How in the world do I print this text to a text area that word wraps and scrolls. Now after all the reading and googleing I have set up text areas over and over again to not only not have them appear, but also not have my text show in them.

Any Ideas on how this can and should be done. Please let me know if you would like my code as reference.

Thanx.
Offline cborders

Junior Devvie





« Reply #1 - Posted 2005-11-08 19:21:49 »

Here is an altered version of the other example I posted.  Hope this helps!
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JTextArea;

public class TypeWriter extends JFrame implements Runnable
{
   private final String message = "This is your mission, should you choose to accept it:\nFind the Princess and bring her back to the castle where she will be safe from the evil Oompa-Loompas.  Then kill as many of those little bastards as you can!";
   private JTextArea currMessage;
   private int currLetter = 0;
   private Thread thread;
   private long time = 0;
   
   public static void main(String[] args)
   {
      new TypeWriter();
   }

   public TypeWriter()
   {
      addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent we)
         {
            stop();
            System.exit(0);
         }
      });
      setLayout(new FlowLayout());
      currMessage = new JTextArea(5, 35);
      currMessage.setLineWrap(true);
      currMessage.setWrapStyleWord(true);
      currMessage.setCaretColor(Color.WHITE);
      add(currMessage);
      setSize(400, 120);
      setTitle("Evil Oompa-Loompas!");
      setVisible(true);
      start();
   }
   
   public void run()
    {
        Thread me = Thread.currentThread();
        while (thread == me)
        {
           if(time == 0 || System.currentTimeMillis() >= time)
           {
              if(currLetter <= message.length())
              {
                 currMessage.setText(message.substring(0, currLetter));
                 currLetter++;
                 repaint();
                 time = System.currentTimeMillis() + 75;
              }
              else
              {
                 break;
              }
           }
           else
           {
              try
              {
                 Thread.sleep(50);
              }
              catch(InterruptedException ie)
              {}
           }
        }
        thread = null;
    }
   
   public void start()
    {
       thread = new Thread(this);
       thread.setPriority(Thread.MIN_PRIORITY);
       thread.start();      
    }

    public void stop()
    {
       thread = null;
    }
}
Offline rdcarvallo

Senior Devvie


Projects: 5
Exp: 15 years


2D Java games forever!


« Reply #2 - Posted 2005-11-08 19:55:31 »

Are you using awt or swing??
In swing, you can append text to the document of a TextPane (don't  remember very well the name).
In awt I should see the API to give help..
If  I find something, i'll post it here!
  Rafael.-

EDIT: Seems like someone solved it.. !
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline straydoginc

Junior Devvie





« Reply #3 - Posted 2005-11-08 22:26:45 »

So I basically get rid of the Paint method entirely? Hmmmmm I am using AWT. Ill see what I get done with this one.. hopefully it'll give me a 1w - 12L record Smiley
Offline cborders

Junior Devvie





« Reply #4 - Posted 2005-11-08 23:29:48 »

If you are using a TextArea you won't need to override the paint().  That's only if you want to draw with a Graphics object or otherwise alter the default repaint behavior.
Offline straydoginc

Junior Devvie





« Reply #5 - Posted 2005-11-10 19:56:06 »

So ran in to a couple of issues trying the above code

First I got an out of bounds error in the while loop. go figure..

second.. Im trying to put this all into an applet. But the screen blinks when it repaints which is anoying. Can I extend Applet for my class and utilize the JFrame to input the interface inside of the JFrame to reduce the flashyness of repaint();??
Offline cborders

Junior Devvie





« Reply #6 - Posted 2005-11-10 20:05:05 »

Weird, it ran fine for me!  As far as a JFrame vs. a JApplet, the repaint speed shouldn't be any different.  Did it run ok (other than the exception) as a JFrame?
Offline straydoginc

Junior Devvie





« Reply #7 - Posted 2005-11-10 20:36:18 »

Switched my logic to be

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
for ( int i = 0; i < ConstantsT.message.length(); i++ ) {

try {
Thread.sleep(100); //pause 1 second
   } catch (InterruptedException e) {
    }
         
screenMessage.setText(ConstantsT.message.substring(0, i ));
         
}


This fixed the error.. what im not liking is

Class extends Applet <--ideal cause my game runs in an applet
* The text as its is typing flashes and starts to blink faster as the text writes itself out.

Class extends JFrame <--nifty but no sure how to integrate with my applet game
* Doesn't flash but pops up in a new window after running. which leads me to believe I can't use this for the applet. Which would also mean I may have to redesign my entire game to allow this text to work correctly.

Those are the two biggies at the moment with this class. Any thoughts?
Offline cborders

Junior Devvie





« Reply #8 - Posted 2005-11-10 20:47:08 »

You should be able to add the relevant stuff from the constructor to your init() method
1  
2  
3  
4  
currMessage = new JTextArea(5, 35);
currMessage.setLineWrap(true);
currMessage.setWrapStyleWord(true);
currMessage.setCaretColor(Color.WHITE);

and then add the stuff in the run() method to to your render() method.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
if(time == 0 || System.currentTimeMillis() >= time)
 {
        if(currLetter <= message.length())
        {
                currMessage.setText(message.substring(0, currLetter));
                currLetter++;
                time = System.currentTimeMillis() + 75;
        }
        else
        {
                break;
        }
}

You'll also have to decare the variables, but that should work!   I just did it that way to have a fully functional (well mostly functional  Embarrassed ) example.
Offline straydoginc

Junior Devvie





« Reply #9 - Posted 2005-11-10 20:51:48 »

But as it types the text out.. does the system screen flash.. Might be just because im using Eclipse not sure?

Also in my init() method.. can i make a JFrame to put my JTextArea in? If so how would i do this?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cborders

Junior Devvie





« Reply #10 - Posted 2005-11-10 20:58:57 »

Yeah, if you like you can do a
1  
JFrame textFrame = new JFrame();

then set the attributes and add the JTextArea and set it to visible when you want someone to talk and then not visible when they are done.
Offline straydoginc

Junior Devvie





« Reply #11 - Posted 2005-11-10 21:03:34 »

cool ill try that and see what it does. Thanx
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (70 views)
2014-11-26 15:22:04

toopeicgaming1999 (59 views)
2014-11-26 15:20:36

toopeicgaming1999 (14 views)
2014-11-26 15:20:08

SHC (27 views)
2014-11-25 12:00:59

SHC (25 views)
2014-11-25 11:53:45

Norakomi (31 views)
2014-11-25 11:26:43

Gibbo3771 (25 views)
2014-11-24 19:59:16

trollwarrior1 (38 views)
2014-11-22 12:13:56

xFryIx (77 views)
2014-11-13 12:34:49

digdugdiggy (55 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!