From da5b0fd5b34527ce29429bd46f99215e0e4476bd Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 5 Sep 2021 21:47:45 +0200 Subject: [PATCH] Add text capabilities to Drawable --- src/main/java/de/oshgnacknak/gruphi/Canvas.java | 13 +++++++++++++ src/main/java/de/oshgnacknak/gruphi/Drawable.java | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/de/oshgnacknak/gruphi/Canvas.java b/src/main/java/de/oshgnacknak/gruphi/Canvas.java index 634b5f6..2c08aa8 100644 --- a/src/main/java/de/oshgnacknak/gruphi/Canvas.java +++ b/src/main/java/de/oshgnacknak/gruphi/Canvas.java @@ -75,6 +75,19 @@ public class Canvas extends JPanel { r.run(); g.scale(1/scale, 1/scale); } + + @Override + public void text(double x, double y, String text) { + FontMetrics metrics = g.getFontMetrics(g.getFont()); + int d = metrics.getAscent(); + g.drawString(text, round(x), round(y) + d); + } + + @Override + public void textSize(int size) { + var f = g.getFont(); + g.setFont(new Font(f.getName(), Font.PLAIN, size)); + } }); } diff --git a/src/main/java/de/oshgnacknak/gruphi/Drawable.java b/src/main/java/de/oshgnacknak/gruphi/Drawable.java index 628e793..64a3aa7 100644 --- a/src/main/java/de/oshgnacknak/gruphi/Drawable.java +++ b/src/main/java/de/oshgnacknak/gruphi/Drawable.java @@ -23,4 +23,8 @@ public interface Drawable { void translated(double x, double y, Runnable r); void scaled(double scale, Runnable r); + + void text(double x, double y, String text); + + void textSize(int size); }