342 *
343 * public static void main(String args[]) {
344 * Frame f = new Frame("GridBag Layout Example");
345 * GridBagEx1 ex1 = new GridBagEx1();
346 *
347 * ex1.init();
348 *
349 * f.add("Center", ex1);
350 * f.pack();
351 * f.setSize(f.getPreferredSize());
352 * f.show();
353 * }
354 * }
355 * </pre></blockquote><hr>
356 * <p>
357 * @author Doug Stein
358 * @author Bill Spitzak (orignial NeWS & OLIT implementation)
359 * @see java.awt.GridBagConstraints
360 * @see java.awt.GridBagLayoutInfo
361 * @see java.awt.ComponentOrientation
362 * @since JDK1.0
363 */
364 public class GridBagLayout implements LayoutManager2,
365 java.io.Serializable {
366
367 static final int EMPIRICMULTIPLIER = 2;
368 /**
369 * This field is no longer used to reserve arrays and kept for backward
370 * compatibility. Previously, this was
371 * the maximum number of grid positions (both horizontal and
372 * vertical) that could be laid out by the grid bag layout.
373 * Current implementation doesn't impose any limits
374 * on the size of a grid.
375 */
376 protected static final int MAXGRIDSIZE = 512;
377
378 /**
379 * The smallest grid that can be laid out by the grid bag layout.
380 */
381 protected static final int MINSIZE = 1;
382 /**
545 }
546
547 /**
548 * Removes the constraints for the specified component in this layout
549 * @param comp the component to be modified
550 */
551 private void removeConstraints(Component comp) {
552 comptable.remove(comp);
553 }
554
555 /**
556 * Determines the origin of the layout area, in the graphics coordinate
557 * space of the target container. This value represents the pixel
558 * coordinates of the top-left corner of the layout area regardless of
559 * the <code>ComponentOrientation</code> value of the container. This
560 * is distinct from the grid origin given by the cell coordinates (0,0).
561 * Most applications do not call this method directly.
562 * @return the graphics origin of the cell in the top-left
563 * corner of the layout grid
564 * @see java.awt.ComponentOrientation
565 * @since JDK1.1
566 */
567 public Point getLayoutOrigin () {
568 Point origin = new Point(0,0);
569 if (layoutInfo != null) {
570 origin.x = layoutInfo.startx;
571 origin.y = layoutInfo.starty;
572 }
573 return origin;
574 }
575
576 /**
577 * Determines column widths and row heights for the layout grid.
578 * <p>
579 * Most applications do not call this method directly.
580 * @return an array of two arrays, containing the widths
581 * of the layout columns and
582 * the heights of the layout rows
583 * @since JDK1.1
584 */
585 public int [][] getLayoutDimensions () {
586 if (layoutInfo == null)
587 return new int[2][0];
588
589 int dim[][] = new int [2][];
590 dim[0] = new int[layoutInfo.width];
591 dim[1] = new int[layoutInfo.height];
592
593 System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
594 System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
595
596 return dim;
597 }
598
599 /**
600 * Determines the weights of the layout grid's columns and rows.
601 * Weights are used to calculate how much a given column or row
602 * stretches beyond its preferred size, if the layout has extra
603 * room to fill.
604 * <p>
605 * Most applications do not call this method directly.
606 * @return an array of two arrays, representing the
607 * horizontal weights of the layout columns
608 * and the vertical weights of the layout rows
609 * @since JDK1.1
610 */
611 public double [][] getLayoutWeights () {
612 if (layoutInfo == null)
613 return new double[2][0];
614
615 double weights[][] = new double [2][];
616 weights[0] = new double[layoutInfo.width];
617 weights[1] = new double[layoutInfo.height];
618
619 System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
620 System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
621
622 return weights;
623 }
624
625 /**
626 * Determines which cell in the layout grid contains the point
627 * specified by <code>(x, y)</code>. Each cell is identified
628 * by its column index (ranging from 0 to the number of columns
629 * minus 1) and its row index (ranging from 0 to the number of
630 * rows minus 1).
631 * <p>
632 * If the <code>(x, y)</code> point lies
633 * outside the grid, the following rules are used.
634 * The column index is returned as zero if <code>x</code> lies to the
635 * left of the layout for a left-to-right container or to the right of
636 * the layout for a right-to-left container. The column index is returned
637 * as the number of columns if <code>x</code> lies
638 * to the right of the layout in a left-to-right container or to the left
639 * in a right-to-left container.
640 * The row index is returned as zero if <code>y</code> lies above the
641 * layout, and as the number of rows if <code>y</code> lies
642 * below the layout. The orientation of a container is determined by its
643 * <code>ComponentOrientation</code> property.
644 * @param x the <i>x</i> coordinate of a point
645 * @param y the <i>y</i> coordinate of a point
646 * @return an ordered pair of indexes that indicate which cell
647 * in the layout grid contains the point
648 * (<i>x</i>, <i>y</i>).
649 * @see java.awt.ComponentOrientation
650 * @since JDK1.1
651 */
652 public Point location(int x, int y) {
653 Point loc = new Point(0,0);
654 int i, d;
655
656 if (layoutInfo == null)
657 return loc;
658
659 d = layoutInfo.startx;
660 if (!rightToLeft) {
661 for (i=0; i<layoutInfo.width; i++) {
662 d += layoutInfo.minWidth[i];
663 if (d > x)
664 break;
665 }
666 } else {
667 for (i=layoutInfo.width-1; i>=0; i--) {
668 if (d > x)
669 break;
670 d += layoutInfo.minWidth[i];
|
342 *
343 * public static void main(String args[]) {
344 * Frame f = new Frame("GridBag Layout Example");
345 * GridBagEx1 ex1 = new GridBagEx1();
346 *
347 * ex1.init();
348 *
349 * f.add("Center", ex1);
350 * f.pack();
351 * f.setSize(f.getPreferredSize());
352 * f.show();
353 * }
354 * }
355 * </pre></blockquote><hr>
356 * <p>
357 * @author Doug Stein
358 * @author Bill Spitzak (orignial NeWS & OLIT implementation)
359 * @see java.awt.GridBagConstraints
360 * @see java.awt.GridBagLayoutInfo
361 * @see java.awt.ComponentOrientation
362 * @since 1.0
363 */
364 public class GridBagLayout implements LayoutManager2,
365 java.io.Serializable {
366
367 static final int EMPIRICMULTIPLIER = 2;
368 /**
369 * This field is no longer used to reserve arrays and kept for backward
370 * compatibility. Previously, this was
371 * the maximum number of grid positions (both horizontal and
372 * vertical) that could be laid out by the grid bag layout.
373 * Current implementation doesn't impose any limits
374 * on the size of a grid.
375 */
376 protected static final int MAXGRIDSIZE = 512;
377
378 /**
379 * The smallest grid that can be laid out by the grid bag layout.
380 */
381 protected static final int MINSIZE = 1;
382 /**
545 }
546
547 /**
548 * Removes the constraints for the specified component in this layout
549 * @param comp the component to be modified
550 */
551 private void removeConstraints(Component comp) {
552 comptable.remove(comp);
553 }
554
555 /**
556 * Determines the origin of the layout area, in the graphics coordinate
557 * space of the target container. This value represents the pixel
558 * coordinates of the top-left corner of the layout area regardless of
559 * the <code>ComponentOrientation</code> value of the container. This
560 * is distinct from the grid origin given by the cell coordinates (0,0).
561 * Most applications do not call this method directly.
562 * @return the graphics origin of the cell in the top-left
563 * corner of the layout grid
564 * @see java.awt.ComponentOrientation
565 * @since 1.1
566 */
567 public Point getLayoutOrigin () {
568 Point origin = new Point(0,0);
569 if (layoutInfo != null) {
570 origin.x = layoutInfo.startx;
571 origin.y = layoutInfo.starty;
572 }
573 return origin;
574 }
575
576 /**
577 * Determines column widths and row heights for the layout grid.
578 * <p>
579 * Most applications do not call this method directly.
580 * @return an array of two arrays, containing the widths
581 * of the layout columns and
582 * the heights of the layout rows
583 * @since 1.1
584 */
585 public int [][] getLayoutDimensions () {
586 if (layoutInfo == null)
587 return new int[2][0];
588
589 int dim[][] = new int [2][];
590 dim[0] = new int[layoutInfo.width];
591 dim[1] = new int[layoutInfo.height];
592
593 System.arraycopy(layoutInfo.minWidth, 0, dim[0], 0, layoutInfo.width);
594 System.arraycopy(layoutInfo.minHeight, 0, dim[1], 0, layoutInfo.height);
595
596 return dim;
597 }
598
599 /**
600 * Determines the weights of the layout grid's columns and rows.
601 * Weights are used to calculate how much a given column or row
602 * stretches beyond its preferred size, if the layout has extra
603 * room to fill.
604 * <p>
605 * Most applications do not call this method directly.
606 * @return an array of two arrays, representing the
607 * horizontal weights of the layout columns
608 * and the vertical weights of the layout rows
609 * @since 1.1
610 */
611 public double [][] getLayoutWeights () {
612 if (layoutInfo == null)
613 return new double[2][0];
614
615 double weights[][] = new double [2][];
616 weights[0] = new double[layoutInfo.width];
617 weights[1] = new double[layoutInfo.height];
618
619 System.arraycopy(layoutInfo.weightX, 0, weights[0], 0, layoutInfo.width);
620 System.arraycopy(layoutInfo.weightY, 0, weights[1], 0, layoutInfo.height);
621
622 return weights;
623 }
624
625 /**
626 * Determines which cell in the layout grid contains the point
627 * specified by <code>(x, y)</code>. Each cell is identified
628 * by its column index (ranging from 0 to the number of columns
629 * minus 1) and its row index (ranging from 0 to the number of
630 * rows minus 1).
631 * <p>
632 * If the <code>(x, y)</code> point lies
633 * outside the grid, the following rules are used.
634 * The column index is returned as zero if <code>x</code> lies to the
635 * left of the layout for a left-to-right container or to the right of
636 * the layout for a right-to-left container. The column index is returned
637 * as the number of columns if <code>x</code> lies
638 * to the right of the layout in a left-to-right container or to the left
639 * in a right-to-left container.
640 * The row index is returned as zero if <code>y</code> lies above the
641 * layout, and as the number of rows if <code>y</code> lies
642 * below the layout. The orientation of a container is determined by its
643 * <code>ComponentOrientation</code> property.
644 * @param x the <i>x</i> coordinate of a point
645 * @param y the <i>y</i> coordinate of a point
646 * @return an ordered pair of indexes that indicate which cell
647 * in the layout grid contains the point
648 * (<i>x</i>, <i>y</i>).
649 * @see java.awt.ComponentOrientation
650 * @since 1.1
651 */
652 public Point location(int x, int y) {
653 Point loc = new Point(0,0);
654 int i, d;
655
656 if (layoutInfo == null)
657 return loc;
658
659 d = layoutInfo.startx;
660 if (!rightToLeft) {
661 for (i=0; i<layoutInfo.width; i++) {
662 d += layoutInfo.minWidth[i];
663 if (d > x)
664 break;
665 }
666 } else {
667 for (i=layoutInfo.width-1; i>=0; i--) {
668 if (d > x)
669 break;
670 d += layoutInfo.minWidth[i];
|