Add centered text and minor fixes

master
John 2022-06-06 11:24:21 -04:00
parent 2f5ded58ab
commit a92616ac45
3 changed files with 38 additions and 4 deletions

View File

@ -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) {

View File

@ -65,6 +65,10 @@ 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;
}

25
src/TextManager.java Normal file
View File

@ -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);
}
}