Wrong colors in JLabel when using HTML string
I'm trying to use HTML styling in a JLabel and convert the BufferedImage
to jpg. However the colors are quite different than what I expect.
Here is the code:
val html = "<html><body style='width: 400px; padding: 5px; margin:0;
color:#000;'>" + "<h1>teststring1</h1>" + key + "
<h2>teststring</h2><body></html>"
val textLabe = new JLabel(html)
textLabe.setSize(textLabe.getPreferredSize)
val d: Dimension = textLabe.getPreferredSize
val bi: BufferedImage = new BufferedImage(d.width, d.height,
BufferedImage.TYPE_INT_ARGB)
val g = bi.createGraphics
textLabe.paint(g)
val a = new ByteArrayOutputStream()
ImageIO.write(bi, "jpg", a)
As you can see the text color is not black and the background is black
instead of white. I tried to set setForeground(Colors.white),
setBackground(Colors.white) and setOpaque(true) but the background color
turn into pink in this case.
How can I fix this problem?
No comments:
Post a Comment