[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Substitute for JTextPane in Java1

Posted on 2002-04-28
8
Medium Priority
?
305 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. 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

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…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

649 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