开发者

Java:Create an image file and write strings in it?

Hi i want to creat开发者_运维知识库e 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??


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


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
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜