KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > Shape


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.swt.graphics.Point;
14
15 public class Shape {
16     int[] data;
17     int used = 0;
18     
19     public Shape(int[] data) {
20         this.data = data;
21         this.used = data.length;
22     }
23     
24     public Shape(int size) {
25         data = new int[size * 2];
26     }
27     
28     public Shape() {
29         this(8);
30     }
31     
32     public void add(IntAffineMatrix transform, Shape toAppend) {
33         int idx = 0;
34         while(idx < toAppend.used) {
35             int x = toAppend.data[idx++];
36             int y = toAppend.data[idx++];
37             
38             add(transform.getx(x,y), transform.gety(x, y));
39         }
40     }
41     
42     public void add(Shape toAppend) {
43         int idx = 0;
44         while(idx < toAppend.used) {
45             int x = toAppend.data[idx++];
46             int y = toAppend.data[idx++];
47             
48             add(x, y);
49         }
50     }
51     
52     public Shape reverse() {
53         int[] result = new int[used];
54         
55         int idx = 0;
56         int srcIdx = used - 2;
57         while (idx < used) {
58             result[idx++] = data[srcIdx];
59             result[idx++] = data[srcIdx + 1];
60             srcIdx -= 2;
61         }
62         
63         return new Shape(result);
64     }
65     
66     public void add(int x, int y) {
67         if (used >= data.length - 1) {
68             resizeArray(Math.max(data.length * 2, 8));
69         }
70         
71         data[used++] = x;
72         data[used++] = y;
73     }
74     
75     public void add(Point toAdd) {
76         add(toAdd.x, toAdd.y);
77     }
78     
79     public Point[] asPointArray() {
80         Point[] result = new Point[used / 2];
81         int idx = 0;
82         for (int i = 0; i < result.length; i++) {
83             int x = data[idx++];
84             int y = data[idx++];
85             
86             result[i] = new Point(x, y);
87         }
88         
89         return result;
90     }
91     
92     private void resizeArray(int newSize) {
93         int[] newData = new int[newSize];
94         System.arraycopy(data, 0, newData, 0, used);
95         data = newData;
96     }
97     
98     public int[] getData() {
99         if (used < data.length) {
100             resizeArray(used);
101         }
102         
103         return data;
104     }
105 }
106
Popular Tags