Results 1 to 8 of 8
  1. #1
    dishab is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Angry AWT drawString doesn't work on Linux

    Hi *,
    I have a little program which lets me make an image from a string and write it in a file. It works well on my XP but on my Linux server only the background pink is generated but no text (see attachment) . I also tried to set the Font but it didn't help. Any Ideas?
    Hier is the code:
    public static void textToImage4() throws IOException {
    int width = 200, height = 135;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();

    g.setColor(Color.pink);
    g.fillRect(0, 0, width, height);

    // Font font = new Font("SERIF", Font.BOLD, 18);
    // g2.setFont(font);

    g.setColor(Color.black);
    g.drawString("test", 46 , 54);

    g.dispose();

    ImageIO.write(image, "jpg", new File("test.jpg"));
    }
    I run the program with: java -Djava.awt.headless=true ...

    thanks,
    dishab
    Attached Thumbnails Attached Thumbnails AWT drawString doesn't work on Linux-test.jpg  

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Welcome to the Java Forums!

    Hmm... seems like an environment or local issue.. I run linux and it outputs this:
    Attached Thumbnails Attached Thumbnails AWT drawString doesn't work on Linux-test.jpg  
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    What's your code look like? Here's what I did:
    Java Code:
    public class Test {
      public static void textToImage4() throws IOException {
        int width = 200, height = 135;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
    
        g.setColor(Color.pink);
        g.fillRect(0, 0, width, height);
    
        // Font font = new Font("SERIF", Font.BOLD, 18);
        // g2.setFont(font);
    
        g.setColor(Color.black);
        g.drawString("test", 46 , 54);
    
        g.dispose();
    
        ImageIO.write(image, "jpg", new File("test.jpg"));
      }
     
      public static void main(String[] args) {
        Test t = new Test();
        try { 
          t.textToImage4();
        } catch (IOException ioE) {
          ioE.printStackTrace();
        }
      }
    }
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    dishab is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    At the moemnt there is only this piece of code:

    public class PicTest {
    public static void main(String[] args) throws IOException {
    textToImage4();
    }

    public static void textToImage4() throws IOException {
    // ... as already posted
    }
    }

    When it works, I want to use it for creating captchas.
    I tested it also on a solaris machine and it works as expected. Only not on my Linux Red hat!

  5. #5
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    This doesn't appear to be a Java issue... but I've been wrong before.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  6. #6
    dishab is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    I tried some more things but had still no success:

    java -Dawt.toolkit=sun.awt.X11.XToolkit -Djava.awt.headless=true -cp . PicTest
    java -Dawt.toolkit=sun.awt.motif.MToolkit -Djava.awt.headless=true -cp . PicTest

    I also downloaded PJA and executed PJADemo. This program generated 3 images. Two of them are including different shapes and looking good. The third one, which should contain text, is only a white rectangle.

    I have no fonts under /usr/X11R6/lib/X11/fonts/TrueTypes. Can this be the problem?
    Thanks, Dishab

  7. #7
    dishab is offline Member
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    For those who may have a similar problem: I could solve the problem. The reason was,that I had no ttf fonts an my machine and the default jdk fonts had no glyps in them (I dont know if it was a installation failur or it is the default behavior of jdk_1.4).
    So I cpoied a ttf file say xyz.ttf to my machine und loaded it dynamically:
    Java Code:
       Font font = createFont(Font.TrueType, new InputStream(new File("xyz.ttf")));
       font = font.deriveFont(Font.BOLD, 14);
       g.setFont(font);
    and got waht I wanted.

  8. #8
    elektromobil is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default

    i meet same problem. drawString working on XP
    not working on Oracle Unbreakable Linux (EL5.)

    im using embeded java in Oracle DB.

    in linux command line application "java Test" is working properly
    but in linux textToImage4(); method when called from PL/SQL is not working .
    drawlines ok, drawString not.


    what is location of file "xyz.ttf" .

Similar Threads

  1. Replies: 6
    Last Post: 04-08-2009, 04:05 AM
  2. new member need help with linux
    By praveena in forum New To Java
    Replies: 0
    Last Post: 01-22-2008, 10:22 AM
  3. problem with ServerSocket on Linux
    By gabriel in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:46 PM
  4. eclipse in linux
    By bbq in forum Eclipse
    Replies: 1
    Last Post: 07-04-2007, 08:59 AM
  5. Linux and java
    By Alan in forum Advanced Java
    Replies: 2
    Last Post: 05-31-2007, 02:30 PM

Posting Permissions

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