Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a solution that works as I wanted: I created a class called <code>CharacterImageGenerator</code> that generates (and caches) <code>Image</code>s of characters. I then draw and scale these images whenever I want to draw a character.</p> <pre><code>public class CharacterImageGenerator { private FontMetrics metrics; private Color color; private Map&lt;Character, Image&gt; images; public CharacterImageGenerator(FontMetrics metrics, Color color) { this.metrics = metrics; this.color = color; images = new HashMap&lt;Character, Image&gt;(); } public Image getImage(char c) { if(images.containsKey(c)) return images.get(c); Rectangle2D bounds = new TextLayout(Character.toString(c), metrics.getFont(), metrics.getFontRenderContext()).getOutline(null).getBounds(); if(bounds.getWidth() == 0 || bounds.getHeight() == 0) { images.put(c, null); return null; } Image image = new BufferedImage((int)bounds.getWidth(), (int)bounds.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); Graphics g = image.getGraphics(); g.setColor(color); g.setFont(metrics.getFont()); g.drawString(Character.toString(c), 0, (int)(bounds.getHeight() - bounds.getMaxY())); images.put(c, image); return image; } } </code></pre> <p>Which I then initialize with a large font to get decent looking characters.</p> <pre><code>// During initialization graphics.setFont(new Font("monospaced", Font.PLAIN, 24)); characterGenerator = new CharacterImageGenerator(graphics.getFontMetrics(), Color.WHITE); </code></pre> <p>And then scale and draw to the size I want.</p> <pre><code>private void drawCharacter(int x, int y, char c) { graphics.drawImage(characterGenerator.getImage(c), PADDING + (x * TILE_WIDTH), PADDING + (y * TILE_HEIGHT), TILE_WIDTH, TILE_HEIGHT, null); } </code></pre>
    singulars
    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.
    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