source: trunk/src/org/expeditee/items/MagneticConstraint/Utilities/Line.java@ 960

Last change on this file since 960 was 960, checked in by bln4, 9 years ago
File size: 2.8 KB
Line 
1package org.expeditee.items.MagneticConstraint.Utilities;
2
3import java.awt.Rectangle;
4import java.util.*;
5
6import org.expeditee.items.Item;
7
8@SuppressWarnings("serial")
9public class Line extends LinkedList<Item> {
10
11 private Line() {
12 super();
13 }
14
15 private Line(final Line toCopy) {
16 super(toCopy);
17 }
18
19 public static Line getLineFromToken(final Item token) {
20 if(token == null) return new Line();
21 else return getLineFromToken(token, new Line());
22 }
23
24 public static Line getLineContainingToken(final Item token) {
25 if(token == null) return new Line();
26 Item current = token;
27 while(current.getMagnetizedItemLeft() != -1)
28 current = current.getParent().getItemWithID(current.getMagnetizedItemLeft());
29 return getLineFromToken(current, new Line());
30 }
31
32 public static int getLineHeight(final Line line) {
33 return (int) (line.getBoundingBox().getHeight() * 1.2);
34 }
35
36 public Line setX(final int newX) {
37 int delta = newX - this.getFirst().getX();
38 deltaX(delta);
39 return this;
40 }
41
42 public Line setY(final int newY) {
43 int delta = newY - this.getFirst().getY();
44 deltaY(delta);
45 return this;
46 }
47
48 public Line deltaX(final int delta) {
49 for(final Item token : this) token.setPosition(token.getX() + delta, token.getY());
50 return this;
51 }
52
53 public Line deltaY(final int delta) {
54 for(final Item token : this) token.setPosition(token.getX(), token.getY() + delta);
55 return this;
56 }
57
58 public Rectangle getBoundingBox() {
59// final Rectangle rect = new Rectangle(this.getFirst().getX(), this.getFirst().getY(), this.getFirst().getBoundsWidth(), this.getFirst().getBoundsHeight());
60// for(final Item token : this)
61// rect.add(new Rectangle(token.getX(), token.getY(), token.getBoundsWidth(), token.getBoundsHeight()));
62// return rect;
63 final Rectangle rect = this.getFirst().getArea().getBounds();
64 for(final Item token : this)
65 rect.add(token.getArea().getBounds());
66 return rect;
67 }
68
69 public Line next() {
70 final Item lastInThisLine = this.get(this.size() - 1);
71 if(lastInThisLine.getMagnetizedItemBottom() == -1) return null;
72 else return getLineFromToken(this.get(0).getParent().getItemWithID(lastInThisLine.getMagnetizedItemBottom()));
73 }
74
75 public Line last() {
76 final Item firstInThisLine = this.get(0);
77 if(firstInThisLine.getMagnetizedItemTop() == -1) return null;
78 else return getLineContainingToken(this.get(0).getParent().getItemWithID(firstInThisLine.getMagnetizedItemTop()));
79 }
80
81 private static Line getLineFromToken(final Item token, final Line acc) {
82 if(token.getMagnetizedItemRight() == -1) {
83 acc.add(token);
84 return acc;
85 } else {
86 final Line lineSoFar = new Line(acc);
87 lineSoFar.add(token);
88 return getLineFromToken(token.getParent().getItemWithID(token.getMagnetizedItemRight()), lineSoFar);
89 }
90 }
91}
Note: See TracBrowser for help on using the repository browser.