Reputation: 11944
Hi i want to create an image file and write strings in it.and if there is no space left on the image file to write more strings ,then the remaining strings should be written in the next image file and so on.how can i do that in Java??
Upvotes: 0
Views: 834
Reputation: 1355
Use the FontMetrics class to calculate the height of the current Font. Something like this:
// g is your Graphics object
Font f = g.getFont();
FontMetrics fm = g.getFontMetrics(f);
int font_height = fm.getHeight()
int linesPerImage = image_height / font_height;
Then keep looping through your text until no lines of text are left to draw. When you reach linesPerImage, save the current image and create a new one. For example, if you have a list of strings that you want to draw in a List called "lines":
int i = lines.size();
for (int j = 0; j < i, j++) {
g.drawString(lines.get(j), x, y);
if (j > 0 && j % linesPerImage == 0) {
// save current image and create new graphics to draw to
}
}
Upvotes: 1
Reputation: 88757
Well, have a look here: http://download.oracle.com/javase/tutorial/2d/images/index.html
This should explain how to create images, draw text to them and then write them to a file.
What would be left is to detect when the string doesn't (entirely?) fit into that image. This, however, depends on the font, font size etc. For that, have a look here: http://download.oracle.com/javase/tutorial/2d/text/measuringtext.html
Upvotes: 1