Results 1 to 11 of 11
  1. #1
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Question String to bitmap generation

    Hi

    I try to generate a bmp file. The input data will be a String in Unicode
    like String s = "Hello world"; The output will be a bmp file of hight and width which will have this text on it. The font used to generate bmp should be also a parameter.

    Now my quastion: is it possible to generate this in Java without any additional libraries? WHat kind of package (classes and methods) should I use?

    THX

  2. #2
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    public void generateBitMap(String input, Font font, File out) {
     FontMetrics fm = this.getFontMetrics(font); // assuming enclosing instance is a JComponent
     BufferedImage image = new BufferedImage(fm.stringWidth(input), fm.getHeight(), BufferedImage.OPAQUE);
     Graphics2D g = image.createGraphics();
     g.setFont(font);
     g.drawString(input, 0, 0);
     g.dispose();
     ImageIO.write(image, "png", out);
    }
    something like that should work

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by JvlShz View Post
    Java Code:
    public void generateBitMap(String input, Font font, File out) {
     FontMetrics fm = this.getFontMetrics(font); // assuming enclosing instance is a JComponent
     BufferedImage image = new BufferedImage(fm.stringWidth(input), fm.getHeight(), BufferedImage.OPAQUE);
     Graphics2D g = image.createGraphics();
     g.setFont(font);
     g.drawString(input, 0, 0);
     g.dispose();
     ImageIO.write(image, "png", out);
    }
    something like that should work
    Please stop posting spoonfed boilerplate, code especially when it's incorrect:

    1) the y coordinate determines the base line, not the top coordinate of the bounding rectangle.
    2) the OP wanted to write .bmp files, not .png files.

    kind regards,

    Jos

  4. #4
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    Java Code:
    public void generateBitMap(String input, Font font, File out) {
     FontMetrics fm = this.getFontMetrics(font); // assuming enclosing instance is a JComponent
     BufferedImage image = new BufferedImage(fm.stringWidth(input), fm.getHeight(), BufferedImage.OPAQUE);
     Graphics2D g = image.createGraphics();
     g.setFont(font);
     g.drawString(input, 0, fm.getHeight());
     g.dispose();
     ImageIO.write(image, "bmp", out);
    }
    fixed, thanks JoSAH for pointing that out

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by JvlShz View Post
    fixed, thanks JoSAH for pointing that out
    Stop the spoonfeeding; I have reported your posts.

    kind regards,

    Jos

  6. #6
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Stop the spoonfeeding; I have reported your posts.

    kind regards,

    Jos
    just trying to help.. maybe I should include some comments

  7. #7
    lipkerson is offline Member
    Join Date
    May 2010
    Location
    Poland-Silesia
    Posts
    17
    Rep Power
    0

    Default

    Thx :-) It was useful.
    I always thought thst Graphics is dedicated to Swing components...but I was wrong.
    It runs in console as good as in JFrame. And fot those who want to know what graphics extensions are accepted can
    use this:

    <code>
    import javax.imageio.ImageIO;

    public class Formats {
    static public void main(String args[]) throws Exception {
    String names[] = ImageIO.getReaderFormatNames();
    for (int i = 0; i < names.length; ++i) {
    System.out.println("reader " + names[i]);
    }

    names = ImageIO.getWriterFormatNames();
    for (int i = 0; i < names.length; ++i) {
    System.out.println("writer " + names[i]);
    }
    }
    }
    </code>
    Last edited by lipkerson; 06-26-2010 at 04:01 PM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by JvlShz View Post
    just trying to help.. maybe I should include some comments
    You shouldn't include any spoonfeedig code at all.

    kind regards,

    Jos

    edit: great, now the OP is publishing your boilerplate code in the Sun Java Forums without giving any credits and without mentioning the crossposting(s) itself. Well done all of you.
    Last edited by JosAH; 06-26-2010 at 04:17 PM.

  9. #9
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You shouldn't include any spoonfeedig code at all.

    kind regards,

    Jos

    edit: great, now the OP is publishing your boilerplate code in the Sun Java Forums without giving any credits and without mentioning the crossposting(s) itself. Well done all of you.
    to be honest it was a no brainer to write that code, I dont really care if he plagarizes it (around 20 seconds to write)

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by JvlShz View Post
    to be honest it was a no brainer to write that code, I dont really care if he plagarizes it (around 20 seconds to write)
    Maybe for you it was a no brainer, for the OP it was a huge problem but above all forum regulars don't like spoonfeeding. Forums aren't production units for boiler plate code; remember that. Otherwise rentacoder.com will be a better place.

    kind regards,

    Jos
    Last edited by JosAH; 06-29-2010 at 09:32 AM.

  11. #11
    JvlShz is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Maybe for you it was a no brainer, for the OP it was a huge problem but above all forum regulars don't like spoonfeeding. Forums are production units for boiler plate code; remember that. Otherwise rentacoder.com will be a better place.

    kind regards,

    Jos
    thanks for the link Jos

Similar Threads

  1. payroll generation
    By kriti in forum New To Java
    Replies: 5
    Last Post: 06-02-2010, 03:21 PM
  2. Validate XML against XSD during generation
    By mandarkatdare in forum XML
    Replies: 0
    Last Post: 05-29-2010, 10:57 AM
  3. War file generation
    By rummy in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 12:57 PM
  4. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 03:43 AM
  5. String generation
    By codingisfun22 in forum Advanced Java
    Replies: 3
    Last Post: 12-03-2007, 05:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •