text class - centered shadowed text with multiple colors and fonts per line

Here’s a class I made for adding text in my games with shadowing to improve visibility and different fonts and colors for highlighting. Does multiple lines of left/right/centered text with multiple fonts and colors.

Example usage:


Font p30 = new Font(Font.SERIF, Font.PLAIN, 30),  
     b20 = new Font(Font.SERIF, Font.BOLD, 20),
     i25 = new Font(Font.SERIF, Font.ITALIC, 25);

// drawing to Graphics object g
Text.draw(g, p30, Color.white, Text.NOSHADOW,
         Text.LEFT, Text.xy(20, 40), "left", 30, "aligned", 30, "text",
         Text.RIGHT, Text.xy(580, 40), "right", 30, "aligned", 30, "text",
         Text.CENTER, Text.xy(300, 40), "center", 30, "aligned", 30, "text",
         40, Text.shadow(Color.black, 1, 2), "centered shadowed text", 
         30, Color.pink, "with multiple ", i25, Color.red, "colors", 
             Color.orange, " and ", Color.cyan, b20, "fonts per line.");

produces:

The text class:


public class Text{
  static int x0 = 0, y = 0, xOffSet = 0, yOffSet = 0, alignment = 0;
  static Color shadowColor = null;
  
  // for use as parameters in draw method
  public static Position xy(int x, int y){
    return new Position(x, y);
  }
  
  public static Shadow shadow(Color c, int x, int y){ 
    return new Shadow(c, x, y); 
  }
  
  public static final Shadow NOSHADOW = new Shadow(null, 0, 0);
  
  public static final Alignment LEFT = new Alignment(0),   
                                CENTER = new Alignment(1),
                                RIGHT = new Alignment(2);
  
  // args: String, Color, Integer(new line spacing), Font, 
  //       Position( xy(x, y) ), Shadow( shadow(c, x, y) or NOSHADOW ),
  //       or Alignment( LEFT, CENTER or RIGHT )
  public static void draw(Graphics g, Object...args){
    int i = 0;
    while (i < args.length){
      int w = 0, j = i; 
      // get number of arguments in current line
      if (alignment == 0){
        while ((j < args.length) && 
               ((args[j] instanceof String) || (args[j] instanceof Color) || 
                (args[j] instanceof Font) || (args[j] instanceof Shadow))) 
          j++;
      }else{
        // if center or right alignment calc line width
        Font f = g.getFont();
        while ((j < args.length) && 
               ((args[j] instanceof String) || (args[j] instanceof Color) || 
                (args[j] instanceof Font) || (args[j] instanceof Shadow))){
          if (args[j] instanceof String) 
            w+= g.getFontMetrics().stringWidth((String)args[j]);
          else if (args[j] instanceof Font)
            g.setFont((Font)args[j]);
          j++;
        }
        g.setFont(f);
      }
      // arguments starting a new line
      if (j == i){
        if (args[j] instanceof Integer){
          y+= (Integer)args[j];
        }else if (args[j] instanceof Position){
          x0 = ((Position)args[j]).x;
          y = ((Position)args[j]).y;
        }else if (args[j] instanceof Alignment){
          alignment = ((Alignment)args[j]).alignment;
        }
        i++;
      // draw line of text
      }else{
        int x = (alignment==1) ? x0 - w/2 : (alignment==2) ? x0 - w : x0;
        for (int k = i; k < j; k++){
          if (args[k] instanceof String){
            if (shadowColor == null){
              g.drawString((String)args[k], x, y);
            }else{
              Color c = g.getColor();
              g.setColor(shadowColor); 
              g.drawString((String)args[k], x+xOffSet, y+yOffSet);
              g.setColor(c); 
              g.drawString((String)args[k], x, y);
            }
            x+= g.getFontMetrics().stringWidth((String)args[k]);
          }else if (args[k] instanceof Color){ 
            g.setColor((Color)args[k]);
          }else if (args[k] instanceof Font){
            g.setFont((Font)args[k]);
          }else if (args[k] instanceof Shadow){
            shadowColor = ((Shadow)args[k]).color; 
            xOffSet = ((Shadow)args[k]).xOffset; 
            yOffSet = ((Shadow)args[k]).yOffset;
          }
        } 
        i = j;
      }
    }
  }
  
  public static class Shadow{
    Shadow(Color c, int x, int y){ color = c; xOffset = x; yOffset = y; }
    Color color;
    int xOffset, yOffset;
  }
  
  public static class Alignment{
    Alignment(int a){ alignment = a; }
    int alignment;  // 0 = left, 1 = center, 2 = right
  }
  
  public static class Position{
    Position(int X, int Y){ x = X; y = Y; }
    int x, y;
  }
}