diff dwtx/draw2d/Ellipse.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/draw2d/Ellipse.d	Sun Aug 03 00:52:14 2008 +0200
@@ -0,0 +1,72 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *     Alex Selkov - Fix for Bug# 22701
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.draw2d.Ellipse;
+
+import dwt.dwthelper.utils;
+
+import dwtx.draw2d.geometry.Rectangle;
+import dwtx.draw2d.Shape;
+import dwtx.draw2d.Graphics;
+
+/**
+ * An figure that draws an ellipse filling its bounds.
+ */
+public class Ellipse
+    : Shape
+{
+
+/**
+ * Constructs a new Ellipse with the default values of a Shape.
+ * @since 2.0
+ */
+public this() { }
+
+/**
+ * Returns <code>true</code> if the given point (x,y) is contained within this ellipse.
+ * @param x the x coordinate
+ * @param y the y coordinate
+ * @return <code>true</code>if the given point is contained
+ */
+public bool containsPoint(int x, int y) {
+    if (!super.containsPoint(x, y))
+        return false;
+    Rectangle r = getBounds();
+    long ux = x - r.x - r.width / 2;
+    long uy = y - r.y - r.height / 2;
+    return ((ux * ux) << 10) / (r.width * r.width)
+         + ((uy * uy) << 10) / (r.height * r.height) <= 256;
+}
+
+/**
+ * Fills the ellipse.
+ * @see dwtx.draw2d.Shape#fillShape(dwtx.draw2d.Graphics)
+ */
+protected void fillShape(Graphics graphics) {
+    graphics.fillOval(getBounds());
+}
+
+/**
+ * Outlines the ellipse.
+ * @see dwtx.draw2d.Shape#outlineShape(dwtx.draw2d.Graphics)
+ */
+protected void outlineShape(Graphics graphics) {
+    Rectangle r = Rectangle.SINGLETON;
+    r.setBounds(getBounds());
+    r.width--;
+    r.height--;
+    r.shrink((lineWidth - 1) / 2, (lineWidth - 1) / 2);
+    graphics.drawOval(r);
+}
+
+}