leespector
YaBB Newbies
Offline
Posts: 4
minimal scrolling text field for output
Jul 22nd , 2006, 4:41am
I'd like simple replacements for print() and println() that send their text not only to the pde console but also to a scrolling text field that will appear in exported applets. I don't much care what the text field looks like, although I do want a scroll bar so that I can send an arbitrary amount of text. I worked up a crude graphics-based substitute -- building on the typography examples that come with Processing -- but I really want selectable and scrollable text instead. I am finding this unexpectedly difficult to achieve. I'm new to Processing and not much of a Java programmer (although I have a lot of experience in other languages), so maybe I'm missing something obvious. But I've tried to consult the relvant parts of the core Processing docs, the library docs (e.g. for MyGui), the AWT docs, and the forums, and although I've had many near misses I can't quite do what I need to do, though it seems like it should be simple. The best I've been able to achieve is the code presented below, for two approaches: one using an AWT textArea and one using a MyGUITextInput object. Both have serious pathologies. The AWT approach almost works but it's strangely flakey -- sometimes it works and sometimes it doesn't. When it doesn't it displays only an empty textArea. I can run the thing several times in a row, either in the pde or as an applet in Safari, and I get a seemingly random sequence of working and not working. If I could eliminate this flakeyness then I'd be perfectly happy with the AWT solution. (In case it matters I'm running this under Mac OS 10.4.6 on a G4 Powerbook.) The MyGUITextInput approach doesn't provide a scroll bar, and excess text actually spills out of the MyGUITextInput object and onto the background -- very odd looking. I'm also not crazy about this approach because it requires installation of a library and I'll be using this at the start of a course for beginning programmers -- I'd like students to be able to get it up and running with a standard install of Processing if possible. Of course the library installation isn't that big a deal and we could live with it, but if there's a way to do this without installing a library I'd prefer it. I'd appreciate any help anyone can provide either on debugging the approaches presented here or on new/better approaches. Thanks for any help you can provide! -Lee // the AWT approach import java.awt.TextArea; TextArea screen_text; void setup () { size(600, 400); background(255); // white noLoop(); screen_text = new TextArea("",7,31,1); this.add(screen_text); } // print both to the pde and to the field on the screen void textln(String text_to_print) { println(text_to_print); screen_text.setText(screen_text.getText() + text_to_print + "\n"); // screen_text.append(text_to_print); // should this work instead? } void draw () { for(int i=0; i<40; i=i+1) { textln("the rain in spain"); } } // the MyGUITextInput approach import mkv.MyGUI.*; MyGUI gui; MyGUITextInput screen_text; void setup () { size(600, 400); background(255); // white noLoop(); gui = new MyGUI(this); screen_text = new MyGUITextInput(this, width/2, height/2, 300, 300);; gui.add(screen_text); } // print both to the pde and to the field on the screen void textln(String text_to_print) { println(text_to_print); screen_text.setValue(screen_text.getValue() + text_to_print + "\n"); } void draw () { for(int i=0; i<40; i=i+1) { textln("the rain in spain"); } } -- Lee Spector, Professor of Computer Science School of Cognitive Science, Hampshire College 893 West Street, Amherst, MA 01002-3359 lspector@hampshire.edu, http://hampshire.edu/lspector/ Phone: 413-559-5352, Fax: 413-559-5438