I’ve been trying to make better fonts, using java, so I can apply it to my game later. But for some reason, I can’t draw more then 2 fonts.
Here’s the code
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.font.*;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FontTest extends JPanel {
public MyFont f;
public MyFont f2;
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
f = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "2/4");
f.drawText();
f2 = new MyFont(g2d, "DejaVu Sans Mono", 100, 100, 36, 5, Color.GRAY, Color.WHITE, "3/4");
f2.drawText();
}
public class MyFont {
public int centerX;
public int centerY;
public Shape text;
public Graphics2D g;
public String kind;
public int x;
public int y;
public int s;
public int str;
public Color oc;
public Color ic;
public String word;
public int w;
public int h;
public MyFont(Graphics2D g2d, String font, int x2, int y2, int s2, int stroke, Color outColor, Color inColor, String word2) {
g = g2d;
kind = font;
x = x2;
y = y2;
s = s2;
str = stroke;
oc = outColor;
ic = inColor;
word = word2;
}
public void drawText() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.getAllFonts();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
FontRenderContext frc = g.getFontRenderContext();
Font font = new Font(kind, Font.PLAIN, s);
TextLayout tl = new TextLayout(word, font, frc);
AffineTransform transform = new AffineTransform();
text = tl.getOutline(null);
w = text.getBounds().width;
h = text.getBounds().height;
centerX = (w / 2);
centerY = (h / 2);
transform.translate(x - centerX, y + centerY);
g.transform(transform);
g.setStroke(new BasicStroke(str, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.setColor(oc);
g.draw(text);
g.setColor(ic);
g.fill(text);
g.clip(text);
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
public int centerX() {
return centerX();
}
public int centerY() {
return centerY();
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new FontTest());
f.setSize(400, 400);
f.setVisible(true);
}
}