Package es.iiia.sgi.controls

Source Code of es.iiia.sgi.controls.ShapeIconControl

package es.iiia.sgi.controls;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

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.Rectangle;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

import es.iiia.shapegrammar.shape.LineModel;
import es.iiia.shapegrammar.shape.ShapeModel;

public class ShapeIconControl extends Canvas {
  private static final int iconSize = 64;
  private ShapeModel shape;
 
  public ShapeIconControl(Composite cmp, ShapeModel model) {
    super(cmp, SWT.BORDER_SOLID);

    this.shape = model.clone();
    this.setLayoutData(new RowData(iconSize, iconSize));
   
    this.addPaintListener(new PaintListener() {
      public void paintControl(PaintEvent e) {
        //ctrl.setSize(iconSize, iconSize);
        Rectangle clientArea = getClientArea();
        e.gc.setBackground(getDisplay().getSystemColor(
            SWT.COLOR_WHITE));
        e.gc.fillRectangle(0, 0, clientArea.width - 1,
            clientArea.height - 1);
        e.gc.setBackground(getDisplay().getSystemColor(
            SWT.COLOR_BLACK));
        e.gc.drawRectangle(0, 0, clientArea.width - 1,
            clientArea.height - 1);

        // now draw this control
        Rectangle2D bounds = shape.getBounds();
        // get the resize ratio
        double resize = (iconSize - 8)
            / (bounds.getWidth() > bounds.getHeight() ? bounds
                .getWidth() : bounds.getHeight());
        // resize graphics
        ShapeModel newShape = shape.clone();
        newShape.moveToCenter();
        newShape.scale(resize, resize);
        // now move it to centre of the icon
        newShape.translate(iconSize / 2, iconSize  / 2);
               
       
        for (LineModel line : newShape.getLines()) {
          for (int i=0; i<line.getPoints().size()-1; i++) {
            drawLine(e.gc, line.getPoints().get(i), line.getPoints().get(i+1));
          }
          if (line.isClosed()) {
            drawLine(e.gc, line.getPoints().get(0), line.getPoints().get(line.getPoints().size()-1));
          }
        }
      }
    });
  }
 
  private static void drawLine(GC g, Point2D p1, Point2D p2) {
    g.drawLine(
        (int) Math.round(p1.getX()),
        (int) Math.round(p1.getY()),
        (int) Math.round(p2.getX()),
        (int) Math.round(p2.getY()));
  }

}
TOP

Related Classes of es.iiia.sgi.controls.ShapeIconControl

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.