Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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  
  Creating an advanced text log...  (Read 1090 times)
0 Members and 1 Guest are viewing this topic.
Offline 8ballj

Senior Newbie





« Posted 2008-06-17 21:57:44 »

Hello everyone, I am trying to create a text log that updates throughout gameplay with meaningful information.  I've already done this before using a JTextArea, but this time I want the log to display different fonts/colors/sizes, not just one font for the whole thing.  I've spent hours trying to figure out how to do this with the JTextPane or JEditorPane but I am getting confused and frustrated trying to figure out if I need to have a seperate document etc...  is there a simple way to do this???

Thanks!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #1 - Posted 2008-06-17 22:19:58 »

This should get you started:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      JTextPane textPane = new JTextPane();
      StyledDocument doc = textPane.getStyledDocument();

      Style regular = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

      Style style1 = doc.addStyle("someCode", regular);
      StyleConstants.setFontFamily(style1, "courier");
      StyleConstants.setFontSize(style1, 12);
      StyleConstants.setBold(style1, true);

      Style style2 = doc.addStyle("plainText", style1); // inherits from style1
     StyleConstants.setFontFamily(style2, "arial");
      StyleConstants.setFontSize(style2, 12);
      StyleConstants.setItalic(style2, true);


      doc.insertString(0, "hello world");
      doc.setCharacterAttributes(0, 11, doc.getStyle("plainText"), false);
//or  doc.setCharacterAttributes(0, 11, style1, false);
     doc.setCharacterAttributes(3, 5, doc.getStyle("someCode"), false);
//or  doc.setCharacterAttributes(3, 5, style2, false);


"hello world" will be bold
"lo wo" will be both bold and italic


Be sure to run ALL CODE from the EDT, because JTextPane is VERY unreliable when accessed from other threads.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline 8ballj

Senior Newbie





« Reply #2 - Posted 2008-06-17 23:58:20 »

Thanks, this will work fine!  If the document object gets very large (like 1000+ lines of text), will the program slow down noticeably each time i add text and reset the document in the textPane?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2008-06-17 23:59:52 »

Try persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline woogley
« Reply #4 - Posted 2008-06-18 03:15:41 »

For this case I would actually just set up JList (or something similiar) to take HTML code. Then you can use simple <b> tags to bold things out, set colors with the <font> tag.. bla bla bla

You have to remember that the parser is compatible up to HTML 3.2, hence having to use <font> to set the color..

code snippet:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
Stack logData;
JList log;
JScrollPane logScroll;
public Constructor() {
  logData = new Stack();
  log = new JList();
  log.setEnabled(false);
  logScroll = new JScrollPane(log);
}
public void log(String msg) {
  logData.push(msg);
  log.setListData(logData);
  Rectangle r = log.getBounds();
  if (r.height >= logScroll.getBounds().height) {
    /* scrolls the log down to the newest item */
    logScroll.getViewport().setViewPosition(new Point(0,r.height));
  }
}
public void clear() {
  logData.clear();
  log.setListData(logData);
}


Then you can just log HTML code..
1  
log("<html><font color=blue>This will be blue");


That way you don't have to worry about using those Style objects and such..

The above code is taken from a template GUI I use whenever I make a server-client app. The result looks decent:

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (81 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.173 seconds with 21 queries.