?
Solved

Substitute for JTextPane in Java1

Posted on 2002-04-28
8
Medium Priority
?
300 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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