SWT
provides Color class to define colors. There are three ways to get Color
object.
a.
Using
getSystemColor method
b.
Using
RGB
c.
Using
RGBA
Using getSystemColor
method
Display
class provides 'getSystemColor' method to get the color instance.
public
Color getSystemColor (int id)
Ex:
Color
blue = display.getSystemColor(SWT.COLOR_BLUE);
Using RGB
Color
class provides following constructor to define color instance using RGB
notation.
public
Color (Device device, RGB rgb)
Ex:
Color
green = new Color(display, new RGB(0, 255, 0));
Using RGBA
Color
class provide following constructor to define color instance using RGBA
notation.
public
Color(Device device, RGBA rgba)
Ex:
Color
color = new Color(display, new RGBA(24, 20, 123, 5));
If
you create Color instances using any approach other than getSystemColor method,
you need to dispose them manually.
Ex:
green.dispose();
color.dispose();
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.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)); 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(); } 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