1 20 21 package JFlex.gui; 22 23 import java.awt.*; 24 import java.util.*; 25 26 33 public class GridPanel extends Panel implements Handles { 34 35 private int cols; 36 private int rows; 37 38 private int hgap; 39 private int vgap; 40 41 private Vector constraints = new Vector(); 42 private Insets insets = new Insets(0,0,0,0); 43 44 public GridPanel(int cols, int rows) { 45 this(cols, rows, 0, 0); 46 } 47 48 public GridPanel(int cols, int rows, int hgap, int vgap) { 49 this.cols = cols; 50 this.rows = rows; 51 this.hgap = hgap; 52 this.vgap = vgap; 53 } 54 55 public void doLayout() { 56 Dimension size = getSize(); 57 size.height -= insets.top+insets.bottom; 58 size.width -= insets.left+insets.right; 59 60 float cellWidth = size.width/cols; 61 float cellHeight = size.height/rows; 62 63 for (int i = 0; i < constraints.size(); i++) { 64 GridPanelConstraint c = (GridPanelConstraint) constraints.elementAt(i); 65 66 float x = cellWidth * c.x + insets.left + hgap/2; 67 float y = cellHeight * c.y + insets.right + vgap/2; 68 69 float width, height; 70 71 if (c.handle == FILL) { 72 width = (cellWidth-hgap) * c.width; 73 height = (cellHeight-vgap) * c.height; 74 } 75 else { 76 Dimension d = c.component.getPreferredSize(); 77 width = d.width; 78 height = d.height; 79 } 80 81 switch (c.handle) { 82 case TOP_CENTER: 83 x+= (cellWidth+width)/2; 84 break; 85 case TOP_RIGHT: 86 x+= cellWidth-width; 87 break; 88 case CENTER_LEFT: 89 y+= (cellHeight+height)/2; 90 break; 91 case CENTER: 92 x+= (cellWidth+width)/2; 93 y+= (cellHeight+height)/2; 94 break; 95 case CENTER_RIGHT: 96 y+= (cellHeight+height)/2; 97 x+= cellWidth-width; 98 break; 99 case BOTTOM: 100 y+= cellHeight-height; 101 break; 102 case BOTTOM_CENTER: 103 x+= (cellWidth+width)/2; 104 y+= cellHeight-height; 105 break; 106 case BOTTOM_RIGHT: 107 y+= cellHeight-height; 108 x+= cellWidth-width; 109 break; 110 } 111 112 c.component.setBounds(new Rectangle((int)x, (int)y, (int)width, (int)height)); 113 } 114 } 115 116 public Dimension getPreferredSize() { 117 float dy = 0; 118 float dx = 0; 119 120 for (int i = 0; i < constraints.size(); i++) { 121 GridPanelConstraint c = (GridPanelConstraint) constraints.elementAt(i); 122 123 Dimension d = c.component.getPreferredSize(); 124 125 dx = Math.max(dx, d.width/c.width); 126 dy = Math.max(dy, d.height/c.height); 127 } 128 129 dx+= hgap; 130 dy+= vgap; 131 132 dx*= cols; 133 dy*= rows; 134 135 dx+= insets.left+insets.right; 136 dy+= insets.top+insets.bottom; 137 138 return new Dimension((int)dx,(int)dy); 139 } 140 141 public void setInsets(Insets insets) { 142 this.insets = insets; 143 } 144 145 public void add(int x, int y, Component c) { 146 add(x,y,1,1,FILL,c); 147 } 148 149 public void add(int x, int y, int handle, Component c) { 150 add(x,y,1,1,handle,c); 151 } 152 153 public void add(int x, int y, int dx, int dy, Component c) { 154 add(x,y,dx,dy,FILL,c); 155 } 156 157 public void add(int x, int y, int dx, int dy, int handle, Component c) { 158 super.add(c); 159 constraints.addElement(new GridPanelConstraint(x,y,dx,dy,handle,c)); 160 } 161 } 162 | Popular Tags |