Sat Feb 09, 2013 12:20 am
import java.awt.*;
import java.util.Vector;
public class DiagonalLayout implements LayoutManager {
private int verticalDistance;
private int minWidth = 0, minHeight = 0;
private int preWidth = 0, preHeight = 0;
private boolean isSize = true;
public DiagonalLayout() {
this(5);
}
public DiagonalLayout(int v) {
verticalDistance = v;
}
/* override from LayoutManager. */
public void addLayoutComponent(String name, Component comp) {
}
/* override from LayoutManager. */
public void removeLayoutComponent(Component comp) {
}
private void setSizes(Container container) {
int countComp = container.countComponents();
Dimension newDim = null;
//Reset preferred/minimum width and height.
preWidth = 0;
preHeight = 0;
minWidth = 0;
minHeight = 0;
for (int i = 0; i < countComp; i++) {
Component c = container.getComponent(i);
if (c.isVisible()) {
newDim = c.preferredSize();
if (i > 0) {
preWidth += newDim.width/2;
preHeight += verticalDistance;
} else {
preWidth = newDim.width;
}
preHeight += newDim.height;
minWidth = Math.max(c.minimumSize().width, minWidth);
minHeight = preHeight;
}
}
}
/* override from LayoutManager. */
public Dimension preferredLayoutSize(Container container) {
Dimension dim = new Dimension(0, 0);
int countComp = container.countComponents();
setSizes(container);
//Always add the container's insets!
Insets insets = container.insets();
dim.width = preWidth + insets.left + insets.right;
dim.height = preHeight + insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* override from LayoutManager. */
public Dimension minLayoutSize(Container container) {
Dimension dim = new Dimension(0, 0);
int countComp = container.countComponents();
//Always add the container's insets!
Insets insets = container.insets();
dim.width = minWidth + insets.left + insets.right;
dim.height = minHeight + insets.top + insets.bottom;
sizeUnknown = false;
return dim;
}
/* This is called when the panel is first displayed,
* and every time its size changes.
*/
public void layoutContainer(Container container) {
Insets insets = container.insets();
int maxWidth = container.size().width
- (insets.left + insets.right);
int maxHeight = container.size().height
- (insets.top + insets.bottom);
int countComp = container.countComponents();
int previousWidth = 0, previousHeight = 0;
int x = 0, y = insets.top;
int rowh = 0, start = 0;
int xFudge = 0, yFudge = 0;
boolean oneColumn = false;
// Go through the components' sizes, if neither preferredLayoutSize()
// nor minLayoutSize() has been called.
if (sizeUnknown) {
setSizes(container);
}
if (maxWidth <= minWidth) {
oneColumn = true;
}
if (maxWidth != preWidth) {
xFudge = (maxWidth - preWidth)/(countComp - 1);
}
if (maxHeight > preHeight) {
yFudge = (maxHeight - preHeight)/(countComp - 1);
}
for (int i = 0 ; i < countComp ; i++) {
Component c = container.getComponent(i);
if (c.isVisible()) {
Dimension d = c.preferredSize();
if (i > 0) {
if (!oneColumn) {
//x += previousWidth - d.width/2 + xFudge;
x += previousWidth/2 + xFudge;
}
y += previousHeight + verticalDistance + yFudge;
}
// If x is too big, ...
if ((!oneColumn) &&
(x + d.width) > (container.size().width - insets.right)) {
// ... reduce x to a reasonable number.
x = container.size().width - insets.bottom - d.width;
}
// If y is too big, ...
if ((y + d.height) > (container.size().height - insets.bottom)) {
// ... do nothing.
// Another choice would be to do what we do to x.
}
// Set the component's size and position.
c.reshape(x, y, d.width, d.height);
previousWidth = d.width;
previousHeight = d.height;
}
}
}
public String toString() {
String str = "";
return getClass().getName() + "[verticalDistance=" + verticalDistance + str + "]";
}
}
|
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.