I've searched everywhere looking for *anything* to help me with this - but I
haven't found anything.  Hopefully someone on here can provide me some
insight.

I have a servlet that loads a jpeg image, renders text onto a section of it
(a rectangle, although, the rectangle isn't rendered), and returns the image
to a browser.  My problem comes in wrapping the text within the bounds of
the rectangle.  The image that I load come from a database.  I'll also know
the upper x,y and lower x,y cordinates for the bounding rectangle.  I've
hard coded most of these numbers below just to get things working... and it
is pretty much working with only a couple of minor glithches - what I'm
wondering is, is there a more efficent way of doing this?  Am I making this
harder than it really is?  This is the first time that I've worked with the
2D API - so I'm not that familiar with it.


Below is the code of importance:

//the string in question...
String message = "here's a lot of text that we'll have to wrap if this thing
is to look                              good.  here's a lot of text that
we'll have to wrap if this thing is to                  look good.  here's a
lot of text that we'll have to wrap if this thing is                    to
look good.  here's a lot of text that we'll have to wrap if this thing
is to look good.";

//standard stuff here, loading/creating...
URL imageURL = new URL("http://mysite/images/image.jpg");
BufferedImage myImage = Toolbox.loadJPEGImage(imageURL);
Locale currentLocale = new Locale("English", "US");
Graphics2D g = myImage.createGraphics();

//the upper left cordinates for the bounding rectangle (in pixels)
//or the basline of the first line of text...
Point2D loc = new Point(360, 40);

Font defaultfont = new Font("Arial", Font.PLAIN, 14);
g.setFont(defaultfont);
FontRenderContext frc = g.getFontRenderContext();
g.setColor(Color.red);

BreakIterator boundary = BreakIterator.getLineInstance(currentLocale);
boundary.setText(message);
int start = boundary.first();
int end = boundary.next();
int lineLength = 0;

//I fiddled maxLength this until it 'worked' - this should be
//somehow calculated though.
int maxLength = 33;
double myY = loc.getY();
double myX = loc.getX();

while (end != BreakIterator.DONE) {
        String word = message.substring(start, end);
        lineLength = lineLength + word.length();
        if (lineLength >= maxLength) {
                //if we've reached our maxLength, drop down a line,
                myY = myY + 15;

                //reset myX to the left boundry,
                myX = loc.getX();

                //reset lineLength to the current 'word'
                lineLength = word.length();
        }
        //draw the word...
        g.drawString(word, (int)myX, (int)myY);

        //increase myX so that the next word will be
        //rendered beside the previous word... 6 is a number
        //i just picked - seems to work ok...
        myX = loc.getX() + (lineLength * 6);
        start = end;
        end = boundary.next();
}

here's the text that is output *exactly* as it appears on the image:

here's a lot  of text that we'll
haveto wrapif  this thing is
to look good. here's a lot  of
text  that we'll haveto wrapif
this  thing is  to look good.
here's a lot  of text that we'll
haveto wrapif  this thing is
to look good.  here's a lot of
text  that we'll  haveto wrapif
this thing is to look good.

regards,

ryanS

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to