Skip to content

Instantly share code, notes, and snippets.

@CalebWhiting
Last active November 24, 2016 22:49
Show Gist options
  • Save CalebWhiting/20e9f46d81ee26916116 to your computer and use it in GitHub Desktop.
Save CalebWhiting/20e9f46d81ee26916116 to your computer and use it in GitHub Desktop.
import java.awt.*;
/**
* @author Caleb Date: 27/04/12 Time: 22:38
*/
public class PaintUtil {
public static class ColorString {
private final Color color;
private final String string;
public ColorString(final Color color, final String string) {
this.color = color;
this.string = string;
}
public Color getColor() {
return color;
}
public String getString() {
return string;
}
}
public static class StringGroup {
private ColorString[] strings;
public StringGroup(final ColorString... strings) {
this.strings = strings;
}
public ColorString[] getStrings() {
return strings;
}
}
public static int[] drawString(final Graphics g, final int x, final int y, final StringGroup... groups) {
int cy = y;
int width = 0;
int height = 0;
final FontMetrics fm = g.getFontMetrics();
for (final StringGroup group : groups) {
int cx = x;
for (final ColorString string : group.getStrings()) {
g.setColor(string.getColor());
g.drawString(string.getString(), cx, cy);
cx += fm.stringWidth(string.getString());
}
if (cx > width) {
width = cx;
}
cy += 10;
}
height = cy;
return new int[]{ width - x, height - y };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment