From a92616ac45fe3f371f29e133c25873950b8d79cc Mon Sep 17 00:00:00 2001 From: John Date: Mon, 6 Jun 2022 11:24:21 -0400 Subject: [PATCH] Add centered text and minor fixes --- src/MenuPanel.java | 11 ++++++++--- src/NonPlayer.java | 6 +++++- src/TextManager.java | 25 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/TextManager.java diff --git a/src/MenuPanel.java b/src/MenuPanel.java index 42b326b..74d4368 100644 --- a/src/MenuPanel.java +++ b/src/MenuPanel.java @@ -13,8 +13,7 @@ public class MenuPanel extends JFrame implements ActionListener { public MenuPanel() { this.setTitle("First"); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - this.setSize(280, 200); - + this.setSize(1225, 630); launchGame = new JButton("Click"); launchGame.addActionListener(this); @@ -27,7 +26,13 @@ public class MenuPanel extends JFrame implements ActionListener { public void paint(Graphics g) { super.paint(g); - g.drawString("potato", 50, 50); + draw(g); + } + + public void draw(Graphics g) { + g.setColor(Color.black); + g.setFont(new Font("Monospaced", Font.PLAIN, 60)); + g.setColor(Color.white); } public void actionPerformed(ActionEvent evt) { diff --git a/src/NonPlayer.java b/src/NonPlayer.java index fd3a527..3582eb9 100644 --- a/src/NonPlayer.java +++ b/src/NonPlayer.java @@ -65,10 +65,14 @@ public class NonPlayer extends GenericSprite { realX = x-GamePanel.camera.x; } public void move(){ + if (isDead) { + // preemptively end move function if enemy is dead + return; + } if(!canUpdate(xVelocity, 0)){ xVelocity*=-1; } - if(!canUpdate(0, yVelocity)){ + if(!canUpdate(0, yVelocity)){ if(yVelocity>0){ while(canUpdate(0,1)){ y+=1; diff --git a/src/TextManager.java b/src/TextManager.java new file mode 100644 index 0000000..2c0e90d --- /dev/null +++ b/src/TextManager.java @@ -0,0 +1,25 @@ +import java.awt.*; + +public final class TextManager { + // utility class, constructor is private to prevent initialization + private TextManager() {} + + private static void drawTextBox(Graphics g, int y, int xOffset, int xWidth, int yHeight, int totalWidth, + String text, Color backgroundColor, Color textColor) { + int newX = (totalWidth - xWidth)/2; + int newY = y - yHeight/2; + g.setColor(backgroundColor); + g.drawRect(newX, newY, xWidth, yHeight); + g.setColor(textColor); + drawCenteredString(g, y, xOffset, xWidth, text); + } + private static void drawCenteredString(Graphics g, int y, int xOffset, int xWidth, String text) { + int x; + // get font size + FontMetrics metrics = g.getFontMetrics(); + // determine x for the text + x = xOffset + (xWidth - metrics.stringWidth(text)) / 2; + // draw centered string + g.drawString(text, x, y); + } +}