Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting the "native size" of an image created with JLabel
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/a/4437998/11236">I am using JLabel</a> to create an image file from a string.</p> <p>I have to specify an image dimensions (<code>label.setSize(width, height)</code>), otherwise I get an exception:</p> <pre><code>java.lang.IllegalArgumentException: Width (0) and height (0) cannot be &lt;= 0 at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.awt.image.BufferedImage.&lt;init&gt;(BufferedImage.java:338) at com.shopsnips.portal.services.ImageCreator.createFromText(ImageCreator.java:31) at com.shopsnips.portal.services.ImageCreator.main(ImageCreator.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) </code></pre> <p>I can control the font size using </p> <pre><code>label.setFont(new Font("Serif", Font.BOLD, 26)); </code></pre> <p>When I use a font or text that is too large to fit the fixed dimentions, the label is truncated and "..." is included instead. Is there a way to identify the optimal/maximal font size that still fits in the dimensions I set?</p> <p>Or alternatively, how can I find out whether the current settings (font size + dimensions) will cause the text to be truncated?</p> <p>Here is some source:</p> <pre><code>import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class ImageCreator { private ImageCreator(){} private final static String FONT = "Freestyle Script"; public static void main(String[] args) { Path outputFile = Paths.get("c:\\tmp\\img\\test.png"); createFromText("Hello World - this is a long text", outputFile, 150, 50); } /** * &lt;p&gt;Create an image from text. &lt;p/&gt; * &lt;p/&gt; * https://stackoverflow.com/a/4437998/11236 */ public static void createFromText(String text, Path outputFile, int width, int height) { JLabel label = new JLabel(text, SwingConstants.CENTER); label.setSize(width, height); label.setFont(new Font(FONT, Font.BOLD, 24)); BufferedImage image = new BufferedImage( label.getWidth(), label.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics g = null; try { // paint the html to an image g = image.getGraphics(); g.setColor(Color.BLACK); label.paint(g); } finally { if (g != null) { g.dispose(); } } // get the byte array of the image (as jpeg) try { ImageIO.write(image, "png", outputFile.toFile()); } catch (IOException e) { throw new RuntimeException(e); } } } </code></pre> <p>Please log in to comment. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload