Dynamic center and place strings on a JPanel

Hi,

I’m having trouble with Painting multiple strings nice underneath eatchother.
I already can draw a message excactly in the center
but when i start adding more there is no spacing between them…
so as a workaround i add more “\n” symbols but after adding 3 when trying to write game over in 35 points i had enough…
I alsoo tried to put the startRow higher but then all rows get placed higher so that’s useless.
How would i get spacing between the strings.

Example function
drawMessages(Color.YELLOW, “Game Over!\nI win ^^”, 35, g);

  private void drawMessages(Color color, String msg, int FontPointSize, Graphics2D g) {
    String[] msges;
    int startRow;
    int mesLen;

    // Put Message into Array
    msges = msg.split("\n");
    mesLen = msges.length;

    // Create Font information Object
    FontMetrics fm = g.getFontMetrics();

    // Get the Height of the Message
    int msgHeight = fm.getHeight();

    // Calculate the Row where we are going to start in PIXELS
    startRow = PANEL_HEIGHT/2 - mesLen/2*msgHeight;

    for(int i=0;i<mesLen;i++) {
      drawCenterMessage(color, msges[i], startRow+i*msgHeight, FontPointSize, g);
    }
  }
  
 /**
   * Draw a Message in the Horizontal middle of the Panel
   * @param color The Text Color
   * @param msg The Message to be displayed
   * @param g graphical context
   */
  private void drawCenterMessage(Color color, String msg, int row, int FontPointSize, Graphics2D g) {
    final int VERTICAL_PADDING = 10;   //Unused
    double xP;      /* X pixel where we will be drawing the message */
    double yP;      /* Y pixel where we will be drawing the message */
    int msgWidth;  /* The Width of the message in the given Font in pixels */

    // Create Font and add it to g.
    Font font = new Font("Dialog", Font.BOLD, FontPointSize);
    g.setFont(font);

    // Create Font information Object
    FontMetrics fm = g.getFontMetrics();

    // Get the With and Height of the Message
    msgWidth  = fm.stringWidth(msg);

    // Set Color
    if (color == null)
      g.setColor(Color.WHITE);
    else
      g.setColor(color);

    // Col Calculation
    xP = (GAME_PANEL_WEIGHT/2) - (msgWidth/2);

    // Row Calculation
    if( row<=0 || row > PANEL_HEIGHT)
      throw new IllegalArgumentException("row: " + row + " is not within panel bounds!");
    else
      yP = row;

    g.drawString(msg, (float) xP, (float) yP);
  }

Functions are both in the same class. :wink:

    startRow = PANEL_HEIGHT/2 - mesLen/2*msgHeight;

these quotients must be on higher precision. change for 2f and (float) or (double) for better results. Usually it makes positoning more accurate. :wink:

I posted too fast, i’ve solved it :o

// Get the Height of the Message and add additional spacing.
int msgHeight = fm.getHeight()+VERTICAL_PADDING;

is what i changed, working perfectly now :slight_smile:

and indeed i changed everything to floats since the draw string wants to have floats anyway…