|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-23-2008, 11:31 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 4
|
|
|
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
|
|

01-24-2008, 06:48 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
|
Welcome to the Java Forums!
Hmm... seems like an environment or local issue.. I run linux and it outputs this:
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-24-2008, 06:52 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
What's your code look like? Here's what I did:
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();
}
}
}
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-24-2008, 10:32 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 4
|
|
|
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!
|
|

01-24-2008, 05:44 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 839
|
|
|
This doesn't appear to be a Java issue... but I've been wrong before.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

01-27-2008, 12:42 AM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 4
|
|
|
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
|
|

01-29-2008, 04:20 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 4
|
|
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:
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|