Reputation: 247
I wrote a program that takes a picture and creates an ASCII representation (ASCII art). At first, I wrote the ASCII string to the console, but now I have a JLabel that holds the text. However, the JLabel output is irregular and is not as even as the console output. I tried using a monospaced font (Droid Sans Mono) as the font in the JLabel since it's what I use for the console output, but the output is still irregular. Is there a better way to specify a monospaced font in Swing/JLabel?
Here is the method I use to create the JLabel:
public JLabel formatLabel(){
String l = "<html>";
for(int j = 0; j < height; j+=patchSize){
for(int k = 0; k < width; k+=patchSize){
l+=imageArray.get(j).get(k);
}
l+="<br>";
}
l+="</html>";
return new JLabel(l);
}
And the line I use to specify the font:
label.setFont(new Font("Droid Sans Mono", Font.PLAIN, 6));
EDIT: I changed the JLabel to a JTextArea and that works alot better. The only problem is even with a tiny font, the frame is usually outside the screen. But I have a method that changes the number of pixels to sample, which would make the output smaller.
Upvotes: 3
Views: 2411
Reputation: 306
You can use the html font tag to specify the font for your text.
By specifying HTML code in a label's text, you can give the label various characteristics such as multiple lines, multiple fonts or multiple colors. If the label uses just a single color or font, you can avoid the overhead of HTML processing by using the setForeground or setFont method instead.
public JLabel formatLabel(){
String l = "<html><font face=\"verdana\" color=\"green\" size=\"12\">";
for(int j = 0; j < height; j+=patchSize){
for(int k = 0; k < width; k+=patchSize){
l+=imageArray.get(j).get(k);
}
l+="<br>";
}
l+="</html>";
return new JLabel(l);
}
Upvotes: 2