View Javadoc

1   package ca.spaz.gui;
2   
3   import java.awt.Rectangle;
4   
5   /*** 
6    */
7   
8   public class SpazPosition {
9   	public int leftA, rightA, topA, bottomA;
10  	public double leftR, rightR, topR, bottomR;
11  
12  	public SpazPosition(){}
13  	
14  	public SpazPosition(SpazPosition copy) {
15  		this.leftA = copy.leftA;
16  		this.leftR = copy.leftR;
17  		this.rightA = copy.rightA;
18  		this.rightR = copy.rightR;
19  		this.topA = copy.topA;
20  		this.topR = copy.topR;
21  		this.bottomA = copy.bottomA;
22  		this.bottomR = copy.bottomR;				
23  	}
24  	
25  	public void setHPos(double lR, int lA, double rR, int rA) {
26  		this.leftR = lR;
27  		this.leftA = lA;
28  		this.rightR = rR;
29  		this.rightA = rA;
30  	}
31  	
32  	public void setVPos(double tR, int tA, double bR, int bA) {
33  		this.topR = tR;
34  		this.topA = tA;
35  		this.bottomR = bR;
36  		this.bottomA = bA;
37  	}
38  
39  	public Rectangle getRectangle(int width, int height) {
40  		int top = (int)(topR*height) + topA;
41  		int bottom = (int)(bottomR*height) + bottomA;
42  		int left = (int)(leftR*width) + leftA;
43  		int right = (int)(rightR*width) + rightA;
44  		return ( new Rectangle(left, top, (right - left), (bottom - top)) );
45  	}	
46  	
47  	public Rectangle getMinRectangle() {
48  		return ( new Rectangle(leftA, topA, (rightA - leftA), (bottomA - topA)) );
49  	}	
50  	
51  	public String toString() {
52  		return (leftA + ", " + rightA + ", " + topA + ", " + bottomA);
53  	}
54  	
55  	public void adjustLeft(int val, int width) {
56  		int cur = (int)(width * leftR);
57  		this.leftA = (val - cur);
58  	}
59  
60  	public void adjustRight(int val, int width) {
61  		int cur = (int)(width * rightR);
62  		this.rightA = (val - cur);
63  	}
64  
65  	public void adjustTop(int val, int height) {
66  		int cur = (int)(height * topR);
67  		this.topA = (val - cur);
68  	}
69  
70  	public void adjustBottom(int val, int height) {
71  		int cur = (int)(height * bottomR);
72  		this.bottomA = (val - cur);
73  	}
74  
75  	public boolean valid() {
76  		if ((topA + (topR*500)) > (bottomA + (bottomR*500))) return false;
77  		if ((leftA + (leftR*500)) > (rightA + (rightR*500))) return false;
78  		if (leftR < 0 || leftR > 1) return false;
79  		if (rightR < 0 || rightR > 1) return false;
80  		if (topR < 0 || topR > 1) return false;
81  		if (bottomR < 0 || bottomR > 1) return false;
82  		return true;
83  	}
84  
85  
86  	public String toXMLString() {
87  		StringBuffer sb = new StringBuffer();
88  		sb.append("<position ");
89  		sb.append(" left_rel=\""+ leftR + '"');
90  		sb.append(" left_abs=\""+ leftA + '"');
91  		sb.append(" right_rel=\""+ rightR + '"');
92  		sb.append(" right_abs=\""+ rightA + '"');
93  		sb.append(" top_rel=\""+ topR + '"');
94  		sb.append(" top_abs=\""+ topA + '"');
95  		sb.append(" bottom_rel=\""+ bottomR + '"');
96  		sb.append(" bottom_abs=\""+ bottomA + '"');
97  		sb.append("/>\n");
98  		return sb.toString();		
99  	}
100 
101 	
102 }