how to display multiline content using java drawString() method

T

theAndroidGuy

Hi,
I'm trying to use a simple applet to display content withing a html
page using the applet code given below.
I've a separate class called SolrjTest. I'm getting the result from
one of the methods in that class, and then I want to display the
content. Now the problem is that I'm not able to display the full
content[consisting of many many lines]. What I'm able to see is just a
single line. So can anyone give me some pointers on this issue. Thank
you very much.

----------------------------code snip ----------------------
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;

public class ControlJava extends Applet {
Font f = new Font("TimesRoman", Font.BOLD, 15);
String Message;

public void init() {
Message = new String("ubuntu");
}

public void SetMessage(String MsgText) {
SolrjTest solr = new SolrjTest();
Message = solr.query(MsgText); //Message contains the string that
I want to display
repaint();
}

public void paint(Graphics g) {
g.setFont(f);
g.drawString(Message, 15, 50);
}
}

And the html page that I'm using to display the page is this,

-----------------------------------code snip ---------------------
<HTML>
<HEAD>
<TITLE>Search</TITLE>
</HEAD>
<BODY>
<H3>Solr Search</H3>
<HR>
Enter your search query in the box.
<HR>
<FORM NAME="form1">
<INPUT TYPE="TEXT" NAME="text1">
<INPUT TYPE="BUTTON" VALUE="Search"
onClick="document.ControlJava.SetMessage
(document.form1.text1.value);">
</FORM>
<HR>
<APPLET NAME="ControlJava" CODE="ControlJava.class" WIDTH=1250
HEIGHT=325>
</APPLET>
<HR>
End of page.
</BODY>
</HTML>

Thanks,
Ahmed.

Note:Googling didn't help me much and I'm newbie learning all these
things.
 
A

Andrew Thompson

..the problem is that I'm not able to display the full
content[consisting of many many lines].

Look into the FontMetrics class.
----------------------------code snip ----------------------

'code snips' that are uncompilable due to missing
classes are much less useful than an SSCCE.
<http://pscode.org/sscce.html>

In this instance, the SolrjTest class could be
factored out by hard coding a long string as the
Message.

Note that Java coding conventions are that
attributes and methods are camelCase.
E.G. 'message' and 'setMessage()'.
 
K

KK

..the problem is that I'm not able to display the full
content[consisting of many many lines].

Look into the FontMetrics class.
http://java.sun.com/javase/6/docs/api/java/awt/FontMetrics.html
----------------------------code snip ----------------------

'code snips' that are uncompilable due to missing
classes are much less useful than an SSCCE.
<http://pscode.org/sscce.html>

In this instance, the SolrjTest class could be
factored out by hard coding a long string as the
Message.

Note that Java coding conventions are that
attributes and methods are camelCase.
E.G. 'message' and 'setMessage()'.

Hi All,
I'm also looking for answer to this... Any other concrete response???

Thanks
 
J

John B. Matthews

[...]
Now the problem is that I'm not able to display the full
content[consisting of many many lines]. What I'm able to see is just
a single line. So can anyone give me some pointers on this issue. [...]
String Message = solr.query(MsgText); [...]
g.setFont(f);
g.drawString(Message, 15, 50);

Nothing in the API for drawString [1] suggests that it wraps text to
any boundary. Andrew Thompson helpfully suggested FontMetrics, and
getFontMetrics() [1] can be used after setFont(), accordingly. While
this maximizes versatility in effecting a particular layout, a similar
result can be obtained using a suitable text component [2,3].

[1]<http://www.j2ee.me/javase/6/docs/api/java/awt/Graphics.html>
[2]<http://java.sun.com/docs/books/tutorial/uiswing/components/text.html>
[3]<http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html>
 
A

Andrew Thompson

...
I'm also looking for answer to this...

Well you are not very good at it, considering you
bottom-posted your reply to an answer, which was..
Any other concrete response???

Any more concrete and it would be 'thick as
a brick'.
 
A

Andrew Thompson

Jethro Tull was a superb band.

Sure, but we should not spend too much time
'living in the past'. ;-)

He says.. as the local playlist jumps from
Leadbelly's 'Gallis Pole' to Snob Scrilla's
'Chasing Ghosts'.
 
R

Roedy Green

I'm trying to use a simple applet to display content withing a html
page using the applet code given below.

Have a look at the source for PrettyCanvas. It renders string tokens
that include a font and colour.

https://wush.net/websvn/mindprod/fi...path=/com/mindprod/jdisplay/PrettyCanvas.java
--
Roedy Green Canadian Mind Products
http://mindprod.com

"It wasn’t the Exxon Valdez captain’s driving that caused the Alaskan oil spill. It was yours."
~ Greenpeace advertisement New York Times 1990-02-25
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top