在使用束带将字符串写入图像时更改颜色

时间:2012-08-14 17:33:58

标签: java image colors drawstring

我正在用三个较小的字符串创建一个字符串,我从三个不同的文本文件读入,然后将字符串写入图像。我想在创建的图像中间更改段落的颜色。我可以使用三个paragraphes创建图像,看起来像this.

但是如何将中间段落的字体颜色更改为红色?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.imageio.ImageIO;




public class writeToImage {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    int imgnumber = 0;

      FileInputStream fstream1 = null;
      FileInputStream fstream2 = null;
      FileInputStream fstream3 = null;
    try {
        fstream1 = new FileInputStream("vorlauf.txt");
        fstream2 = new FileInputStream("mitte.txt");
        fstream3 = new FileInputStream("nachlauf.txt");

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      // Get the object of DataInputStream
      DataInputStream vorlauf = new DataInputStream(fstream1);
      BufferedReader br1 = new BufferedReader(new InputStreamReader(vorlauf));

      DataInputStream mitte = new DataInputStream(fstream2);
      BufferedReader br2 = new BufferedReader(new InputStreamReader(mitte));

      DataInputStream nachlauf = new DataInputStream(fstream3);
      BufferedReader br3 = new BufferedReader(new InputStreamReader(nachlauf));

      String vorlauf1   = null;
      String mitte1 = null;
      String nachlauf1  = null;


        try {
            while ((vorlauf1 = br1.readLine()) != null && (mitte1 = br2.readLine()) != null && (nachlauf1 = br3.readLine()) != null){
            try {

                String vorlaufdiv = StringDivider(vorlauf1);
                String mittediv = StringDivider(mitte1);
                String nachlaufdiv = StringDivider(nachlauf1);


                String totalPassage = vorlaufdiv + "\n\n" + mittediv + "\n\n" + nachlaufdiv;

                //Image file name
                   String fileName = "English-translated-";

                   imgnumber++;

                    //create a File Object
                    File newFile = new File("./" + fileName + imgnumber + ".jpg");

                    //create the font you wish to use
                    Font font = new Font("Tahoma", Font.PLAIN, 15);

                    //create the FontRenderContext object which helps us to measure the text
                    FontRenderContext frc = new FontRenderContext(null, true, true);

                    //get the height and width of the text
                    Rectangle2D bounds = font.getStringBounds(totalPassage, frc);
                    int w = 750;
                    int h = 430;

                    //create a BufferedImage object
                   BufferedImage image = new BufferedImage(w, h,   BufferedImage.TYPE_INT_RGB);

                    //calling createGraphics() to get the Graphics2D
                    Graphics2D g = image.createGraphics();

                    //set color and other parameters
                    g.setColor(Color.WHITE);
                    g.fillRect(0, 0, w, h);
                    g.setColor(Color.BLACK);
                    g.setFont(font);

                    int index = 0;
                    String[] parts = totalPassage.split("\n");
                    for(String part : parts){
                    g.drawString(part, (float) bounds.getX(), (float) -bounds.getY() + 20 * index++);
                    }


                  //releasing resources
                  g.dispose();

                    //creating the file

                    try {
                        ImageIO.write(image, "jpg", newFile);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            }


        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }





    }

public static String StringDivider(String s){

    StringBuilder sb = new StringBuilder(s);

    int i = 0;
    while ((i = sb.indexOf(" ", i + 100)) != -1) {
        sb.replace(i, i + 1, "\n");
    }

    return sb.toString();

}


}

2 个答案:

答案 0 :(得分:1)

这真的不是你想要的方式...如果你阅读评论有更好的方法,但这是一种方法,应该工作,你不必改变任何其他东西。这将在字符串中第一次显示颜色,因此它提供的文件不包含\ n \ n。

                int index = 0;
                int paragraphCount = 1; // starting on first paragraph
                String[] parts = totalPassage.split("\n");
                for(String part : parts){
                   if(part.length() == 0) {    
                       paragraphCount++;
                   }
                   if(paragraphCount==2){
                       g.setColor(Color.RED);
                   }else{
                       g.setColor(Color.BLACK);
                   }
                    g.drawString(part, (float) bounds.getX(), (float) -bounds.getY() + 20 * index++);
                }

答案 1 :(得分:0)

我不认为你的解决方案很好,但是对你的代码最简单的答案是将你的代码部分更改为中间段红色。

      int index = 0;
      String[] parts = totalPassage.split("\n");
      for(String part : parts){
         if (index == 2)
             g.setColor(Color.red);
         else 
            g.setColor(Color.black);
         g.drawString(part, (float) bounds.getX(), (float) -bounds.getY() + 20 * index++);
       }

我建议你坚持其他人在评论中给你的建议。