001    /*--------------------------------------------------------------------------+
002    $Id: FlatTreeMapRenderer.java 26931 2010-03-17 14:53:13Z besenreu $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.treemap;
019    
020    import java.awt.Color;
021    import java.awt.Graphics2D;
022    import java.awt.Rectangle;
023    import java.awt.geom.Rectangle2D;
024    
025    
026    /**
027     * A very simple tree map renderer just drawing "flat" rectangles.
028     * 
029     * @author Benjamin Hummel
030     * @author $Author: besenreu $
031     * @version $Rev: 26931 $
032     * @levd.rating GREEN Hash: B52F73980DF98FACCD4B858A5872C409
033     */
034    public class FlatTreeMapRenderer implements ITreeMapRenderer {
035    
036            /** {@inheritDoc} */
037            public <T> void renderTreeMap(ITreeMapNode<T> node, Graphics2D graphics) {
038                    if (node.getChildren().isEmpty()) {
039                            Rectangle2D rect = node.getLayoutRectangle();
040                            graphics.setColor(node.getColor());
041                            graphics.fill(rect);
042    
043                            if (node.getDrawingPattern() != null) {
044                                    drawPattern(graphics, rect, node);
045                            }
046    
047                            graphics.setColor(Color.BLACK);
048                            graphics.draw(rect);
049                    } else {
050                            for (ITreeMapNode<T> child : node.getChildren()) {
051                                    renderTreeMap(child, graphics);
052                            }
053                    }
054            }
055    
056            /** Draws the pattern. */
057            private <T> void drawPattern(Graphics2D graphics, Rectangle2D rect,
058                            ITreeMapNode<T> node) {
059                    IDrawingPattern drawingPattern = node.getDrawingPattern();
060                    graphics.setColor(node.getPatternColor());
061                    for (int x = (int) rect.getMinX(); x <= rect.getMaxX(); ++x) {
062                            for (int y = (int) rect.getMinY(); y <= rect.getMaxY(); ++y) {
063                                    if (drawingPattern.isForeground(x, y)) {
064                                            graphics.fill(new Rectangle(x, y, 1, 1));
065                                    }
066                            }
067                    }
068            }
069    
070    }