Solved

Substitute for JTextPane in Java1

Posted on 2002-04-28
8
291 Views
Last Modified: 2008-02-26
Hi.

I want to write an Applet to write text with different colors in it, i know how to do it with JTextPane, but the browser is supporting Java 1 only therefor does not display the JTextPane at all, is there any idea how to write lines with different colors in an applet? or something to replace the JTextPane to Java 1 object?
thanks
0
Comment
Question by:SteelFist
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 6976871
Essentially, it's necessary to override the applet's paint() method. The following code, although primarily intended to demonstrate centering a string in the applet, shows how to do this:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class StringCenterApplet extends JApplet {
  // maybe get this from an applet parameter
  String stringToCenter = "Testing centering of string";

  public void init() {
    try {
      this.setSize(new Dimension(400,300));
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  public void paint(Graphics g){
    Point where = CenterString.centerString(g,getSize(),stringToCenter);
    g.drawString(stringToCenter,(int)where.getX(),(int)where.getY());
    g.setColor(Color.RED);
    g.drawString(stringToCenter,(int)where.getX(),(int)(where.getY() + 30));
  }

  //encapsulate for re-use
  static class CenterString {
    public static Point centerString(Graphics g,Dimension d,String s){
      Point p = new Point();
      FontMetrics fm = g.getFontMetrics();
      double lineWidth = fm.stringWidth(s);
      double lineHeight = fm.getHeight();
      return new Point((int)(d.width - lineWidth) / 2,(int)(d.height - lineHeight) / 2);
    }
  }
}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
  <HEAD>
  </HEAD>
  <BODY>
    <applet
      code="StringCenterApplet.class"
      codebase="."
      width="200"
      height="200"
    >
    </applet>

  </BODY>
</HTML>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6976874
You can, of course, change public class StringCenterApplet extends JApplet to public class StringCenterApplet extends Applet
0
 

Author Comment

by:SteelFist
ID: 6977052
Thanks for your reply, but what you wrote using the drawString method, which just draws the string in the color you mantioned, i am looking for substitute for JTextPane, same as TextArea but with different colors.
Because i want to print a document or give people write a form there for example, if i want to print a java source code in the apple, and color several words in the document.

i know how to do it with JTextPane, but again, it's not showing in the applet.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 6977490
The principle would be exactly the same if you were to use TextArea. You would have to override its paint() method.
0
 

Author Comment

by:SteelFist
ID: 6978034
Thanks for your reply, but what you wrote using the drawString method, which just draws the string in the color you mantioned, i am looking for substitute for JTextPane, same as TextArea but with different colors.
Because i want to print a document or give people write a form there for example, if i want to print a java source code in the apple, and color several words in the document.

i know how to do it with JTextPane, but again, it's not showing in the applet.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6978049
Are we getting this right - you want to create something like code syntax coloring using a TextArea in an applet < Java 1.2?
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7992339
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by: CEHJ
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venci75
EE Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 8048131
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question