/Users/lyon/j4p/src/gui/layouts/BoundableComponentPlacement.java
|
1 package gui.layouts;
2
3 import java.awt.*;
4
5 // original code by Maynard Marquis
6 // hacked by DL.
7
8 public class BoundableComponentPlacement
9 implements BoundableInterface {
10
11 public int position = Alignable.NORTHWEST;
12
13 public void setBounds(Component c, int x, int y, int w, int h) {
14 Dimension preferredSize = c.getPreferredSize();
15 Dimension cellSize = new Dimension(w, h);
16 Dimension newSize = min(cellSize, preferredSize);
17 if (newSize.equals(cellSize)) {
18 c.setBounds(x, y, cellSize.width, cellSize.height);
19 return;
20 }
21 if (c instanceof Alignable) {
22 Alignable a = (Alignable) c;
23 position = a.getAlignment();
24 }
25 Point dp = getPosition(new Point(x, y), newSize, cellSize);
26 c.setBounds(dp.x, dp.y, newSize.width, newSize.height);
27 }
28
29 public Dimension min(Dimension d1, Dimension d2) {
30 if (d1.width < d2.width) return d1;
31 if (d1.height < d2.height) return d1;
32 return d2;
33 }
34
35 public Point getPosition(Point startPoint,
36 Dimension newSize,
37 Dimension cellSize) {
38
39 int west = startPoint.x;
40 int east = startPoint.x + cellSize.width - newSize.width;
41 int centerX = startPoint.x + cellSize.width / 2 - newSize.width / 2;
42
43 int north = startPoint.y;
44 int south = startPoint.y + cellSize.height - newSize.height;
45 int centerY = startPoint.y + cellSize.height / 2 - newSize.height / 2;
46
47
48 switch (position) {
49 case Alignable.NORTHWEST:
50 return new Point(west, north);
51 case Alignable.NORTH:
52 return new Point(centerX, north);
53 case Alignable.NORTHEAST:
54 return new Point(east, north);
55 case Alignable.EAST:
56 return new Point(east, centerY);
57 case Alignable.SOUTHEAST:
58 return new Point(east, south);
59 case Alignable.SOUTH:
60 return new Point(centerX, south);
61 case Alignable.SOUTHWEST:
62 return new Point(west, south);
63 case Alignable.WEST:
64 return new Point(west, centerY);
65 case Alignable.CENTER:
66 return new Point(centerX, centerY);
67 }
68 return new Point(centerX, centerY);
69 }
70 }
71