Tuesday, 3 January 2017

SWT: Graphics

SWT provides Graphics context ‘GC’ class, to work with graphics. By using GC, you can perform following tasks.
a.   Set the foreground and background colors
b.   Draw shapes like rectangle, circle etc.,
c.    Display images
d.   Change the font etc.,

Following step-by-step procedure explains simple drawing application using Canvas and GC.

Step 1: Define Canvas object. You can draw graphics on the surface of Canvas object.

Canvas canvas = new Canvas(shell, SWT.BORDER);
canvas.setSize(canvasWidth, canvasHeight);
canvas.setLocation(20, 20);

Step 2: Open the shell. Note that shee should be opened after defining Canvas object.
shell.open();

Step 3: Get the GC instance to draw graphics on canvas surface.

GC gc = new GC(canvas);

By using above gc instance, we can apply graphics on the canvas surface, If you try to draw any graphic outside the canvas surface area, it is not visible.

Step 4: Now you can use different methods provided by GC class, to draw graphics.

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);

Step 5: Dispose the resources taken by SWT application.
gc.dispose();

Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
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 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();

  Color blue = display.getSystemColor(SWT.COLOR_BLUE);
  Color red = display.getSystemColor(SWT.COLOR_RED);

  GC gc = new GC(canvas);

  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);

  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();

 }
}



There is one major problem with above program, just minimize the window, maximize it back, now you can observe whatever the graphics you painted on Canvas are cleared. It is because, after maximizing, the graphics are not repainted on the canvas.

To resolve this issue, we should know when the graphics are repainted. We can do this by using PaintListener interface. The paintListener will detect whenever canvas is repainted, and execute the paintControl method.

First I moved all the graphics to drawGraphics method.

private static void drawGraphcis(Display display, GC gc)
{
         Color blue = display.getSystemColor(SWT.COLOR_BLUE);
         Color red = display.getSystemColor(SWT.COLOR_RED);

         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);

}

Next, I added paintListener to canvas object like below.

canvas.addPaintListener(new PaintListener()
{
         @Override
         public void paintControl(PaintEvent e)
         {
                 GC gc = e.gc;
                 drawGraphcis(display, gc);
         }

});

Whenever repaint needed, I called drawGraphics method.


Following is the updated 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.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);

  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);

 }

 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();

 }
}


Note

You can get GC object on any SWT widget, and apply graphics.



Previous                                                 Next                                                 Home

No comments:

Post a Comment