Package ch.fusun.baron.coatofarms.ui

Source Code of ch.fusun.baron.coatofarms.ui.CoatOfArmsViewer

/**
*
*/
package ch.fusun.baron.coatofarms.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

/**
* Displays a {@link CoatOfArmsData} in a {@link Composite}
*/
public class CoatOfArmsViewer extends Composite {

  /**
   * Data to be displayed
   */
  private CoatOfArmsData data;

  /**
   * @param parent
   *            The parent
   * @param style
   *            The SWT style
   */
  public CoatOfArmsViewer(Composite parent, int style) {
    super(parent, style);
    this.addPaintListener(new PaintListener() {

      @Override
      public void paintControl(PaintEvent event) {
        Image image = new Image(getDisplay(), getClientArea());
        drawOnGC(data, image, image.getBounds().width / 2,
            image.getBounds().height / 2, image.getBounds().width,
            image.getBounds().height);
        event.gc.drawImage(image, 0, 0);
        image.dispose();
      }
    });
  }

  /**
   * Draws the data onto the graphics context
   *
   * @param data
   *            The coat of arms data
   * @param image
   *            The image to draw on
   * @param width
   *            The width of the flag
   * @param height
   *            The height of the flag
   * @param x
   *            x position
   * @param y
   *            y position
   */
  public static void drawOnGC(CoatOfArmsData data, Image image, int x, int y,
      int width, int height) {
    for (int i = x - width / 2; i < x + width / 2; i++) {
      for (int j = y - height / 2; j < y + height / 2; j++) {
        image.getImageData().setAlpha(i, j, 255);
      }
    }
    GC gc = new GC(image);
    gc.setAntialias(SWT.ON);
    data.getBackground().drawBackground(gc, x, y, width, height);
    if (data.getForeground() != null) {
      data.getForeground().drawForeground(gc);
    }
    gc.dispose();
  }

  /**
   * Returns the coat of arms
   *
   * @return the data
   */
  public CoatOfArmsData getCoatOfArms() {
    return data;
  }

  /**
   * Sets the new coat of arms and redraws the widget
   *
   * @param data
   */
  public void setCoatOfArms(CoatOfArmsData data) {
    this.data = data;
    redraw();
  }
}
TOP

Related Classes of ch.fusun.baron.coatofarms.ui.CoatOfArmsViewer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.