private void measure() { FontMetrics fontmetrics = getFontMetrics(getFont()); if (fontmetrics == null) return; line_height = fontmetrics.getHeight(); line_ascent = fontmetrics.getAscent(); max_width = 0; for (int i = 0; i < num_lines; i++) { line_widths[i] = fontmetrics.stringWidth(lines[i]); if (line_widths[i] > max_width) max_width = line_widths[i]; } max_width += 2 * btnMarginWidth; max_height = num_lines * line_height + 2 * btnMarginHeight; }
public void centerText(String s1, String s2, Graphics g, Color c, int x, int y, int w, int h) { // locs[0].centerText(s1, s2, this.getGraphics(), // Color.white, 400,0,200,50); // g.setXORMode(unselected_color); // centerText("pic" + im, null, g, Color.black, x, y, w, h); Font f = g.getFont(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f); int ascent = fm.getAscent(); int height = fm.getHeight(); int width1 = 0, width2 = 0, x0 = 0, x1 = 0, y0 = 0, y1 = 0; width1 = fm.stringWidth(s1); if (s2 != null) width2 = fm.stringWidth(s2); x0 = x + (w - width1) / 2; x0 = x + (w - width2) / 2; if (s2 == null) y0 = y + (h - height) / 2 + ascent; else { y0 = y + (h - (int) (height * 2.2)) / 2 + ascent; y1 = y0 + (int) (height * 1.2); } g.setColor(c); g.drawString(s1, x0, y0); if (s2 != null) g.drawString(s2, x1, y1); }