]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/java/net/tinyos/mviz/DShapeModel.java
Updated files.
[tinyos-2.x.git] / support / sdk / java / net / tinyos / mviz / DShapeModel.java
diff --git a/support/sdk/java/net/tinyos/mviz/DShapeModel.java b/support/sdk/java/net/tinyos/mviz/DShapeModel.java
new file mode 100644 (file)
index 0000000..37e548f
--- /dev/null
@@ -0,0 +1,218 @@
+/*\r
+ * Copyright (c) 2006 Stanford University.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * - Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the\r
+ *   distribution.\r
+ * - Neither the name of the Stanford University nor the names of\r
+ *   its contributors may be used to endorse or promote products derived\r
+ *   from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL STANFORD\r
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r
+ * OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+\r
+package net.tinyos.mviz;\r
+\r
+\r
+// DShapeModel.java\r
+/*\r
+ Store the data state for a single shape:\r
+  type, two points, color\r
+ Supports DShapeModelListeners.\r
+*/\r
+import java.awt.*;\r
+\r
+import javax.swing.*;\r
+import java.util.*;\r
+import java.awt.event.*;\r
+import java.io.*;\r
+\r
+\r
+class DShapeModel extends Object implements Serializable {\r
+\r
+       // The 5 standard things for a DShapeModel to store\r
+    protected char type;\r
+    protected int x1, y1, x;\r
+    protected int x2, y2, y;\r
+    protected Color fill; \r
+    \r
+    protected float value;\r
+    \r
+    protected int HALF_WIDTH = 20;\r
+    protected int HALF_HEIGHT = 20;\r
+\r
+    // NOTE: "transient" -- not serialized\r
+    transient private ArrayList listeners;\r
+      \r
+    public DShapeModel(char type, int x, int y, float value) {\r
+        this.type = type;\r
+        this.x = x;\r
+        this.y = y;\r
+        this.value = value;\r
+        int color = (int)(value)%230;\r
+        this.fill = new Color(color+15, color, color+25);\r
+        \r
+        listeners = null;\r
+        \r
+        this.x1 = x - this.HALF_WIDTH;\r
+        this.x2 = x + this.HALF_WIDTH;\r
+        this.y1 = y - this.HALF_HEIGHT;\r
+        this.y2 = y + this.HALF_HEIGHT;\r
+    }\r
+    \r
+    // Construct a DShapeModel with default size.\r
+       public DShapeModel(char type, Color color) {\r
+               this(type, 50, 50, 89, 89, color);\r
+       }\r
+    \r
+    public DShapeModel(){\r
+        this('m', new Color(12,24,48));\r
+    }\r
+\r
+       public DShapeModel(char type, int x1, int y1, int x2, int y2, Color fill) {\r
+               this.type = type;\r
+               this.x1 = x1;\r
+               this.y1 = y1;\r
+               this.x2 = x2;\r
+               this.y2 = y2;\r
+               this.fill = fill;\r
+               \r
+               listeners = null;\r
+       }\r
+       \r
+       public DShapeModel(DShapeModel other) {\r
+               this.type = other.type;\r
+               this.x1 = other.x1;\r
+               this.y1 = other.y1;\r
+               this.x2 = other.x2;\r
+               this.y2 = other.y2;\r
+               this.fill = other.fill;\r
+               \r
+               listeners = null;\r
+       }\r
+       \r
+       public char getType() { return(type); }\r
+       \r
+       public int getX1() { return(x1); }\r
+       public int getY1() { return(y1); }\r
+       public int getX2() { return(x2); }\r
+       public int getY2() { return(y2); }\r
+       \r
+       \r
+       // Below here, code not done\r
+       \r
+       \r
+       public void applyDeltas(int dx1, int dy1, int dx2, int dy2) {\r
+           x1 += dx1;\r
+           x2 += dx2;\r
+           y1 += dy1;\r
+           y2 += dy2;\r
+        \r
+        x += dx1;\r
+        y += dy1;\r
+           fireChanges();\r
+       }\r
+       \r
+       public int getWidth() {\r
+           return Math.abs(x2-x1)+1;\r
+       }\r
+       \r
+       public int getHeight() {\r
+           return Math.abs(y2-y1)+1;\r
+       }\r
+       \r
+       public int getLocX() {\r
+           return Math.min(x1, x2);\r
+       }\r
+       \r
+       public int getLocY() {\r
+           return Math.min(y1, y2);\r
+       }\r
+                       \r
+       \r
+       \r
+       public Color getColor() { return(fill); }\r
+       public void setColor(Color color) {\r
+           if (fill.equals(color)) return;\r
+           fill = color;\r
+           fireChanges();\r
+       }\r
+       \r
+       \r
+       public void addListener(DShapeModelListener listener) {\r
+           if (listeners == null) listeners = new ArrayList();\r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) {\r
+                   if (it.next() == listener)\r
+                       return;\r
+               }\r
+               listeners.add(listener);            \r
+       }\r
+\r
+       public void removeListener(DShapeModelListener listener) {\r
+           if (listeners == null) return;          \r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) {\r
+                   if (it.next() == listener){\r
+                       it.remove();\r
+                       return;\r
+                   }           \r
+               }                       \r
+       }\r
+       //=========================================================================/\r
+       protected void fireChanges(){\r
+           if (listeners==null) return;\r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) \r
+                  ((DShapeModelListener)(it.next())).shapeChanged(this);\r
+       }\r
+       //=========================================================================/\r
+       public void rotate(){\r
+           // Get old height/width and locations.\r
+           int x = getLocX();  int y = getLocY();\r
+           int w = getWidth(); int h = getHeight();\r
+           int dL = (h/2-w/2);\r
+           // Get the locations right.\r
+           x-=dL;      y+= dL;\r
+           x1=x;       x2=x+h-1;\r
+           y1=y;       y2=y+w-1;       \r
+               fireChanges();\r
+       }\r
+       //=========================================================================/\r
+       // Rotation with respect to center.\r
+       public void scale(int magnitude){\r
+           if (x1<x2){ x1-=magnitude;  x2+=magnitude;\r
+           }else{              x1+=magnitude;  x2-=magnitude;  }\r
+           if (y1<y2){ y1-=magnitude;  y2+=magnitude;\r
+           }else{              y1+=magnitude;  y2-=magnitude;  }    \r
+           fireChanges();\r
+       }\r
+       //=========================================================================/\r
+       public void move(int x, int y){\r
+           int h = getHeight();\r
+           int w = getWidth();\r
+           x1=x; x2=x+w-1;\r
+           y1=y; y2=y+h-1;\r
+           fireChanges();          \r
+       }\r
+       \r
+}\r
+\r