-1

I am using BufferedImage and Graphics2D to generate a GIF image, but I am struggling to have the layout like in the picture below. I am not even sure if it's possible at all.

The GIF will be animated and the values in Heading3 will change. I've created the code to generate the GIF, but I can't really get this layout. I looked into TextLayout and LineBreakMeasurer, but am not getting the desired o/p. The text should scale based on the image size (which I've got working), but that's key as well.

Any tips?

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
avijendr
  • 3,958
  • 2
  • 31
  • 46
  • 4
    Use the Graphics.getFontMetrics(...) method. With the FontMetrics object you can calculate the width of each string and do your own alignment. – camickr Jun 28 '22 at 00:41

1 Answers1

3

HTML can be used in Swing components which support rich text, like JLabel. The label can be painted onto an image.

This HTML/CSS should do the trick.

.num {
    color: rgb(0,0,255);
}
td {
    text-align: center;
    padding: 10px;
}
<table border=0 cellspacing=0>
<tr>
<td>
HEADING1<br>
<span class='num'>103</span>
</td>
<td>
HEADING2<br>
<span class='num'>76</span>
</td>
<td>
HEADING3<br>
<span class='num'>64</span>
</td>
</tr>

Edit 1:

Can't use HTML as I have custom fonts and it won't use the fonts correctly.

To use a custom font in HTML, it needs to be (imported and) registered with the GraphicsContext as shown in this answer.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Can't use HTML as I have custom fonts and it won't use the fonts correctly. – avijendr Jun 28 '22 at 18:11
  • 1
    Sure it will. You just need to register the fonts with the runtime, then you're good to go. – Andrew Thompson Jun 29 '22 at 01:00
  • It won't, I tried. And moreover I am going to run this code inside an AWS Lambda function calling through APIGW from a browser, where the Lambda returns a generated gif. Unless the enduser have have this custom font installed, it wont work. BTW, I solved the issue using FontMetrics and GlyphVector. Thanks for the suggestion though, I didn't knew this was possible (Not for this instance) but in the future I'll surely use this. – avijendr Jun 29 '22 at 01:54
  • 1
    *"It won't, I tried."* It will, I've done it. I can only assume you were doing it wrong. – Andrew Thompson Jun 29 '22 at 02:27
  • I am sorry, I was using the wrong name and it works. I want to try if it works in the AWS Lambda as if i can get the graphics env. Anyways I am going to accept your answer. Can you please update your answer with the font registration part as well please? I'll update the question too. – avijendr Jun 29 '22 at 11:51
  • See edit to answer. – Andrew Thompson Jun 29 '22 at 18:41
  • Thank you. I've accepted the answer, but I have to ditch the html way as it's pretty outdated and won't support a lot of the css, like padding, margin etc,So pretty hard to really align things the way I want. – avijendr Jun 29 '22 at 18:44
  • *"won't support a lot of the css"* True, but.. *"like padding, margin"* False. It supports both of those just fine. Don't use HTML if you don't want, but please stop providing demonstrably(1) false information. 1) The above example *uses* cell padding specified by CSS, and it is trivial to confirm that margins also work. And yes, I adjusted the padding to a point that 'looked right' for the purpose. – Andrew Thompson Jun 29 '22 at 20:26
  • Do you have that code? Neither padding or margin worked in a div or span for me. https://stackoverflow.com/questions/31635447/stroke-text-with-html-in-java-swing – avijendr Jun 29 '22 at 20:45
  • 1
    *"Do you have that code?"* It is literally right above in the answer. As to interacting with spans and divs, it is likely the HTML you used is invalid. Programers are usually horrid at writing HTML. Make sure to validate your (assuming HTML 3.2 - which is all Java Swing HTML supports), using the [Markup Validation Service](https://validator.w3.org/). Also run the CSS through the [CSS Validation Service](https://jigsaw.w3.org/css-validator/). Admittedly that is less useful given the patchy support for CSS in Swing, but at least it will warn of invalid structures (or attributes). – Andrew Thompson Jun 29 '22 at 20:52