001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.graph.old;
006    
007    import graphlab.graph.graph.EdgeModel;
008    import graphlab.graph.graph.GraphModel;
009    import graphlab.graph.graph.GraphPoint;
010    import graphlab.graph.graph.VertexModel;
011    import graphlab.platform.core.BlackBoard;
012    
013    import java.awt.*;
014    import java.awt.image.VolatileImage;
015    import java.util.Iterator;
016    
017    /**
018     * @author azin azadi
019     */
020    public class LayeredRenderer extends AcceleratedRenderer {
021        public LayeredRenderer(GraphModel g, BlackBoard blackboard) {
022            super(g, blackboard);
023        }
024    
025        VolatileImage vLayer, eLayer;
026    
027        private VolatileImage createTransparentBuffer() {
028            GraphicsConfiguration gc = getGraphicsConfiguration();
029            VolatileImage c = gc.createCompatibleVolatileImage(getWidth(), getHeight(), VolatileImage.TRANSLUCENT);
030            // This uses the same code as from Code Example 5, but replaces the try block.
031            Graphics2D gImg = (Graphics2D) c.getGraphics();
032            gImg.setComposite(AlphaComposite.Src);
033            gImg.setColor(new Color(0, 0, 0, 0));
034            gImg.fillRect(0, 0, 100, 100);
035            gImg.dispose();
036    //        c.createGraphics().cl
037            return c;
038        }
039    
040        public void paint(Graphics2D g) {
041            if (vLayer == null)
042                vLayer = createTransparentBuffer();
043            if (eLayer == null)
044                eLayer = createTransparentBuffer();
045            g.setComposite(AlphaComposite.Src);
046            g.drawImage(eLayer, 0, 0, this);
047            g.drawImage(vLayer, 0, 0, this);
048        }
049    
050        public void repaintVLayer() {
051            vLayer = createTransparentBuffer();
052            Graphics2D graphics = vLayer.createGraphics();
053            for (VertexModel v : getGraph()) {
054                repaint(v, graphics);
055            }
056            graphics.dispose();
057        }
058    
059        public void repaintELayer() {
060            eLayer = createTransparentBuffer();
061            Graphics2D graphics = eLayer.createGraphics();
062            Iterator<EdgeModel> ie = getGraph().edgeIterator();
063            while (ie.hasNext()) {
064                repaint(ie.next(), graphics);
065            }
066            graphics.dispose();
067        }
068    
069        public void repaint(VertexModel src) {
070            super.repaint(src, vLayer.createGraphics());
071    //        repaint();
072        }
073    
074        public void repaint(EdgeModel src) {
075            super.repaint(src, eLayer.createGraphics());
076    //        repaint();
077        }
078    
079        public void vertexRemoved(VertexModel v) {
080            repaintVLayer();
081            repaint();
082        }
083    
084        public void edgeRemoved(EdgeModel e) {
085            repaintELayer();
086            repaint();
087        }
088    
089        public void graphCleared() {
090            repaintVLayer();
091            repaintELayer();
092            repaint();
093        }
094    
095        public void updateSize(VertexModel src, GraphPoint newSize) {
096            repaintVLayer();
097            repaint();
098        }
099    
100        public void updateLocation(VertexModel src, GraphPoint newLocation) {
101            repaintVLayer();
102            repaint();
103        }
104    
105        public void updateBounds(Rectangle r, EdgeModel src) {
106            repaintELayer();
107    //        repaint();
108        }
109    }