SWT
provides Font class to define font specific to your application. Font class
provides following constructors to define Font instances.
public Font(Device device, FontData fd)
public Font(Device device, FontData[]
fds)
public Font(Device device, String name,
int height, int style)
Ex:
Font
font = new Font(display, "Arial", 10, SWT.BOLD | SWT.ITALIC);
Following
is the complete working application.
package swt_app; import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.RGBA; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class Test { private static int shellWidth = 1000; private static int shellHeight = 700; private static int canvasHeight = 300; private static int canvasWidth = 400; private static void drawGraphcis(Display display, GC gc) { Color blue = display.getSystemColor(SWT.COLOR_BLUE); Color red = display.getSystemColor(SWT.COLOR_RED); Color green = new Color(display, new RGB(0, 255, 0)); Color color = new Color(display, new RGBA(24, 20, 123, 5)); Font font = new Font(display, "Arial", 10, SWT.BOLD | SWT.ITALIC); gc.setFont(font); gc.setBackground(red); gc.drawRectangle(150, 130, 200, 75); gc.fillRectangle(30, 30, 200, 75); gc.setForeground(blue); gc.drawLine(0, 0, 400, 300); gc.drawString("Hello World Graphics Application", 75, 0); gc.drawOval(0, 0, 400, 300); green.dispose(); color.dispose(); font.dispose(); } private static void addGraphicsToShell(Display display, Shell shell) { Canvas canvas = new Canvas(shell, SWT.BORDER); canvas.setSize(canvasWidth, canvasHeight); canvas.setLocation(20, 20); /* Open shell window */ shell.open(); GC gc = new GC(canvas); drawGraphcis(display, gc); canvas.addPaintListener(new PaintListener() { @Override public void paintControl(PaintEvent e) { GC gc = e.gc; drawGraphcis(display, gc); } }); gc.dispose(); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell to Display */ Shell shell = new Shell(display); shell.setSize(shellWidth, shellHeight); shell.setText("SWT Tutorial"); addGraphicsToShell(display, shell); /* * Run the event dispatching loop until an exit condition occurs, which is typically when the main shell window * is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment