Get the extent of a string : Draw String « SWT 2D Graphics « Java Tutorial






import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ControlEditor;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FontSizeString {
  private static final String STRING = "www.java2s.com";

  private static final String[] SIZES = { "8", "10", "12", "14", "16", "18" };

  private static Font font;

  public static void main(String[] a) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Extents");

    final Canvas canvas = new Canvas(shell, SWT.NONE);

    shell.addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent event) {
        canvas.setBounds(shell.getClientArea());
      }
    });
    canvas.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent event) {
        event.gc.setFont(font);
        Point pt = event.gc.stringExtent(STRING);

        System.out.println(pt);
      }
    });

    // Create an editor to house the dropdown
    ControlEditor editor = new ControlEditor(canvas);

    // Create the combo and fill it
    final Combo combo = new Combo(canvas, SWT.READ_ONLY);
    for (int i = 0, n = SIZES.length; i < n; i++) {
      combo.add(SIZES[i]);
    }

    // Set up the editor
    editor.horizontalAlignment = SWT.CENTER;
    editor.verticalAlignment = SWT.TOP;
    Point size = combo.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    editor.minimumWidth = size.x;
    editor.minimumHeight = size.y;
    editor.setEditor(combo);

    combo.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        if (font != null)
          font.dispose();
        font = new Font(shell.getDisplay(), "Helvetica", new Integer(combo.getText()).intValue(),
            SWT.BOLD);
        canvas.redraw();
      }
    });

    // Select the first item in the combo
    combo.select(0);

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    if (font != null)
      font.dispose();
    display.dispose();
  }
}








18.14.Draw String
18.14.1.Draw string and display newlines and tabs as nonprintable characters.Draw string and display newlines and tabs as nonprintable characters.
18.14.2.String with new line and tabString with new line and tab
18.14.3.Draw string and display newlines and tabs as nonprintable characters: isTransparentDraw string and display newlines and tabs as nonprintable characters: isTransparent
18.14.4.Draw string and process newlines and expanding tabs: drawText(String text, int x, int y)Draw string and process newlines and expanding tabs: drawText(String text, int x, int y)
18.14.5.Draw string and process newlines and expanding tabs: drawText(String text, int x, int y, boolean isTransparent)Draw string and process newlines and expanding tabs: drawText(String text, int x, int y, boolean isTransparent)
18.14.6.Draw string and processing newlines and expanding tabsDraw string and processing newlines and expanding tabs
18.14.7.Get the extent of a string
18.14.8.Measure a String