Java Tutorial/SWT 2D Graphics

Материал из Java эксперт
Версия от 18:18, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Changing Fonts

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FontChangeSWT {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Canvas Example");
   shell.setLayout(new FillLayout());
   Canvas canvas = new Canvas(shell, SWT.NONE);
   canvas.addPaintListener(new PaintListener() {
     public void paintControl(PaintEvent e) {
       
       Font font = new Font(shell.getDisplay(), "Helvetica", 18, SWT.NORMAL);
       e.gc.setFont(font);
       e.gc.drawText("My Text", 0, 0);
       font.dispose();
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Creating Fonts

ConstructorDescriptionpublic Font(Device device, FontData fd)Creates a font using the specified device and font data.public Font(Device device, FontData[] fds)Creates a font using the specified device and array of font data.public Font(Device device, String name, int height, int style)Creates a font using the specified device, name, height (in points), and style. Style constants are SWT.NORMAL, SWT.BOLD, and SWT.ITALIC.

Font Styles

StyleDescriptionSWT.NORMALCreates a normal fontSWT.BOLDCreates a bold fontSWT.ITALICCreates an italic font


Font Terminology

TermMeaningbaselineThe imaginary line the font sits onascentThe number of pixels that characters reach above the baseline to the top of typical lowercase charactersdescentThe number of pixels that characters reach below the baselineheightThe total height of characters in pixels, equal to the ascent plus the descent plus the leading arealeading areaThe number of pixels above the top of typical lowercase characters



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FontMetricsPaintSWT {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setText("Canvas Example");
   shell.setLayout(new FillLayout());
   Canvas canvas = new Canvas(shell, SWT.NONE);
   canvas.addPaintListener(new PaintListener() {
     public void paintControl(PaintEvent e) {
       
       Font font = new Font(shell.getDisplay(), new FontData("Helvetica", 18, SWT.NORMAL));
       e.gc.setFont(font);
       e.gc.drawText("My Text", 0, 0);
       
       FontMetrics fm = e.gc.getFontMetrics();
       int bHeight = fm.getLeading() + fm.getAscent();
       int oHeight = fm.getAscent();
       int yHeight = fm.getAscent() + fm.getDescent();
       int totalHeight = fm.getHeight(); // Equals fm.getLeading() + fm.getAscent()
                                         // + fm.getDescent();
       
       e.gc.drawLine(10,bHeight,100,bHeight);
       e.gc.drawLine(10,oHeight,100,oHeight);
       e.gc.drawLine(10,yHeight,100,yHeight);
       e.gc.drawLine(10,totalHeight,100,totalHeight);
       
       
       font.dispose();
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Get system default font

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FontSystemGettingSWT {

 public static void main(String[] args) {
   final Display display = new Display();
   Shell shell = new Shell(display);
   shell.setText("Canvas Example");
   shell.setLayout(new FillLayout());
   Canvas canvas = new Canvas(shell, SWT.NONE);
   canvas.addPaintListener(new PaintListener() {
     public void paintControl(PaintEvent e) {
       
       e.gc.setFont(display.getSystemFont());
       
       e.gc.drawText(display.getSystemFont().getFontData()[0].getName(), 5, 5);
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Using FontMetrics to get char width

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class FontMetricsCharWidth {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   Text text = new Text(shell, SWT.NONE);
   GC gc = new GC(text);
   FontMetrics fm = gc.getFontMetrics();
   int charWidth = fm.getAverageCharWidth();
   int width = text.ruputeSize(charWidth * 8, SWT.DEFAULT).x;
   gc.dispose();
   
   System.out.println(width);
   shell.isDisposed();
   display.dispose();
 }

}</source>