2009. 11. 15. 19:53

package layout;

import java.awt.*;
import java.util.*;

public class PositionLayout implements LayoutManager {
    Vector  positions = new Vector(10);
    Vector  components = new Vector(10);
    private  boolean   leftRatio;
    private  boolean   topRatio;
    private  boolean   rightRatio;
    private  boolean   bottomRatio;

    public void addLayoutComponent(String p, Component c) {
        positions.addElement(p);
        components.addElement(c);
    }


    public void removeLayoutComponent(Component c) {

        int i = components.indexOf(c);
        if(i != -1) {
            positions.removeElementAt(i);
            components.removeElementAt(i);
        }
    }


    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0,0);
        int ncomponents = parent.getComponentCount();
        Insets  insets = parent.getInsets();
        dim.width += insets.left + insets.right;
        dim.height += insets.top + insets.bottom;

        int maxW = dim.width;
        int maxH = dim.height;
        for(int i = 0; i < ncomponents; i++) {
            Component  com = parent.getComponent(i);
            if(com.isVisible() == false)
                continue;
            int  w = com.getPreferredSize().width + dim.width;
            int  h = com.getPreferredSize().height + dim.height;
            if( w > maxW )
                maxW = w;
            if( h > maxH )
                maxH = h;
        }
        //System.out.println("prefer Size: width="+maxW+"\t\theight="+maxH);
        return  new Dimension(maxW, maxH);
    }


    public Dimension minimumLayoutSize(Container target) {
        return  target.getSize();
    }


    public void layoutContainer(Container target) {
        Insets insets = target.getInsets();
        int    ncomponents = target.getComponentCount();
        Dimension d = target.getSize();
        d.width  -= insets.left + insets.right;
        d.height  -= insets.top + insets.bottom;
        int  startX =0, startY =0, endX =0, endY =0;
        int  left = 0, top =0, right = 0, bottom =0;
        for(int i=0; i< ncomponents; i++) {
            Component comp = target.getComponent(i);
         StringTokenizer token = new StringTokenizer((String)positions.elementAt(i), ", \t");
            String  leftSt = token.nextToken();
            if(leftSt.endsWith("%")) {
                leftRatio = true;
                left  = Integer.parseInt(leftSt.substring(0, leftSt.length()-1));
            } else {
                left  = Integer.parseInt(leftSt);
            }

            String  topSt = token.nextToken();
            if(topSt.endsWith("%")) {
                topRatio = true;
                top  = Integer.parseInt(topSt.substring(0, topSt.length()-1));
            } else {
                top  = Integer.parseInt(topSt);
            }

            String  rightSt = token.nextToken();
            if(rightSt.endsWith("%")) {
                rightRatio = true;
                right  = Integer.parseInt(rightSt.substring(0, rightSt.length()-1));
            } else {
                right  = Integer.parseInt(rightSt);
            }

            String  bottomSt = token.nextToken();
            if(bottomSt.endsWith("%")) {
                bottomRatio = true;
                bottom  = Integer.parseInt(bottomSt.substring(0, bottomSt.length()-1));
            } else {
                bottom  = Integer.parseInt(bottomSt);
            }

            if(leftRatio)
                startX = (d.width * left)/100;
            else
                startX = left;

            if(topRatio)
                startY = (d.height * top)/100;
            else
                startY = top;

            if(rightRatio)
                endX = (d.width * right)/100;
            else
                endX = right;

            if(bottomRatio)
                endY = (d.height * bottom)/100;
            else
                endY = bottom;

            if(startX > endX) {
                int temp = startX;
                startX = endX;
                endX = temp;
            }
            if(startY > endY) {
                int temp = startY;
                startY = endY;
                endY = temp;
            }
            int  w = endX - startX;
            int  h = endY - startY;
            comp.setBounds(startX+insets.left, startY+insets.top, w, h);
            topRatio = false;
            leftRatio = false;
            rightRatio = false;
            bottomRatio = false;
        }
    }

    public String toString() {
        return getClass().getName();
    }
}

'JAVA 6.0 > GUI (AWT, SWING)' 카테고리의 다른 글

JFreeChart Package Documentation  (0) 2009.11.15
JFreeChart Package File  (0) 2009.11.15
JFreeChart 사용하기 (HistogramChart)  (0) 2009.11.15
JFreeChart 사용하기 (ScatterPlot)  (0) 2009.11.15
JFreeChart 사용하기 (LineChart)  (0) 2009.11.15
Posted by TIMEC