0

I am currently making a game in Eclipse, and have a specific font I wish to use, however, it's a PNG. How can I do this in Java 8? (I am an Intermediate in Java) Here's the Image:

enter image description here

I have no code because I have no Idea how to approach this. Thanks in advance.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Coppyhop
  • 23
  • 6

1 Answers1

2

The long and short of it all is, you'll need to use BufferedImage#getSubImage to carve the base image up.

You'll need to write some kind of conversion routine that can take a String and either generate a single image or return back the sub images required to generate it, for example

example

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FontImage {

    public static void main(String[] args) {
        new FontImage();
    }

    public FontImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;

        public TestPane() {
            try {
                img = ImageIO.read(getClass().getResource("/EwnpgTF.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        public List<BufferedImage> convert(String text) {

            List<BufferedImage> images = new ArrayList<>(25);

            for (char c : text.toCharArray()) {
                c = Character.toUpperCase(c);
                int smudge = 1;
                int offset = -1;
                if (c >= 48 && c <= 57) {
                    offset = c - 48;
                } else if (c >= 65 && c <= 90) {
                    offset = c - 65 + 10;
                } else if (c == 32) {
                    offset = 48;
                    smudge = 2;
                }

                if (offset >= 0) {
                    BufferedImage sub = img.getSubimage((offset * 8) + smudge, 0, 8 - smudge, 8);
                    images.add(sub);
                }
            }

            return images;

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            List<BufferedImage> text = convert("This is a test");
            int x = (getWidth() - (8 * text.size())) / 2;
            int y = (getHeight() - 8) / 2;
            for (BufferedImage img : text) {
                g2d.drawImage(img, x, y, this);
                x += img.getWidth();
            }
            g2d.dispose();
        }
    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Any way I can Scale the drawn text by 5? – Coppyhop Jul 01 '14 at 03:45
  • Take a look at `AffineTransformation` or `Graphics2D#scale`. – MadProgrammer Jul 01 '14 at 03:46
  • Wait, how can I draw different text at different coordinates? – Coppyhop Jul 01 '14 at 04:08
  • Well, I would change the `convert` method to generate a single `BufferedImage` (the method would need to create a new `BufferedImage` internally and draw each character onto it), this will allow you to simply use `Graphics#drawImage(Image, int, int, ImageObserver)` as demonstrated in the example – MadProgrammer Jul 01 '14 at 04:16