KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > meta > arranger > GridArranger


1 /*
2  * GridArranger.java
3  *
4  * Created on 7 July 2003, 15:40
5  */

6
7 package com.thoughtriver.open.vectorvisuals.meta.arranger;
8
9 import java.awt.geom.*;
10 import java.util.*;
11
12 import com.thoughtriver.open.vectorvisuals.*;
13
14 /**
15  * This <CODE>Arranger</CODE> implementation arranges <CODE>VisualObject</CODE>s
16  * in a gridlike pattern. The amount of space between the origins of the objects
17  * can be set, as can the number of objects that will be placed on a single row
18  * of the grid before the next row is started. The general approach to
19  * populating the grid is left-to-right, then top-to-bottom.
20  *
21  * @author Brandon Franklin
22  * @version $Date: 2006/11/25 08:58:56 $
23  */

24 public class GridArranger extends Arranger {
25
26     /**
27      * The number of pixels between object origins, both horizontally and
28      * vertically
29      */

30     private double spacing;
31
32     /**
33      * The number of objects that will be placed on a row before starting a new
34      * one
35      */

36     private int objectsPerRow;
37
38     /**
39      * Creates a new instance of <CODE>GridArranger</CODE>. The spacing is
40      * set to a default value of 10.0, and the number of objects per row is set
41      * to a default value of 10.
42      */

43     public GridArranger() {
44         setSpacing(10.0);
45         setObjectsPerRow(10);
46     }
47
48     /**
49      * Returns the amount of space that will be allotted between <CODE>VisualObject</CODE>
50      * origins, both horizontally and vertically.
51      *
52      * @return the amount of space that will be allotted between <CODE>VisualObject</CODE>
53      * origins
54      */

55     public double getSpacing() {
56         return spacing;
57     }
58
59     /**
60      * Sets the amount of space that will be allotted between <CODE>VisualObject</CODE>
61      * origins, both horizontally and vertically.
62      *
63      * @param spacing the amount of space that will be allotted between <CODE>VisualObject</CODE>
64      * origins
65      */

66     public void setSpacing(final double spacing) {
67         this.spacing = spacing;
68     }
69
70     /**
71      * Returns the number of <CODE>VisualObject</CODE>s that will be arranged
72      * on a single row before the next row is started.
73      *
74      * @return the number of <CODE>VisualObject</CODE>s that will be arranged
75      * on a single row before the next row is started
76      */

77     public int getObjectsPerRow() {
78         return objectsPerRow;
79     }
80
81     /**
82      * Sets the number of <CODE>VisualObject</CODE>s that will be arranged on
83      * a single row before the next row is started.
84      *
85      * @param objectsPerRow the number of <CODE>VisualObject</CODE>s that
86      * will be arranged on a single row before the next row is started
87      */

88     public void setObjectsPerRow(final int objectsPerRow) {
89         this.objectsPerRow = objectsPerRow;
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     @Override JavaDoc
96     public Map<VisualObject, AffineTransform> getArrangedPositions() {
97
98         // Calculate all the desired positions
99
Map<VisualObject, AffineTransform> positions = new HashMap<VisualObject, AffineTransform>();
100         int curRow = 0;
101         int curCol = 0;
102         for (VisualObject obj : getObjects()) {
103             positions.put(obj, AffineTransform.getTranslateInstance(getSpacing()
104                     * curCol, getSpacing() * curRow));
105
106             curCol++;
107             if (curCol == getObjectsPerRow()) {
108                 curCol = 0;
109                 curRow++;
110             }
111         }
112
113         return positions;
114     }
115
116 }
117
Popular Tags