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.*;
008    import graphlab.platform.core.BlackBoard;
009    
010    import java.awt.*;
011    import java.awt.image.VolatileImage;
012    
013    /**
014     * a super renderer, not working completely yet.
015     */
016    public class AcceleratedRenderer extends FastRenderer {
017        public AcceleratedRenderer(GraphModel g, BlackBoard blackboard) {
018            super(g, blackboard);
019        }
020    
021        private Image offscreenImage;
022        private Graphics offscreenGraphics;
023        private Dimension offscreenDimension;
024        private VolatileImage volatileImg;
025    //    Thread paintThread = new Thread(this);
026    
027    //    public void repaint() {
028    //        wait += 100;
029    //    }
030    
031        long wait = 0;
032    
033        public void run() {
034            while (true) {
035                while (wait <= 0)
036                    try {
037                        Thread.sleep(100);
038                    } catch (InterruptedException e) {
039                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
040                    }
041                while (wait > 0) {
042                    try {
043                        Thread.sleep(100);
044                    } catch (InterruptedException e) {
045                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
046                    }
047                    wait -= 100;
048                }
049                paint(getGraphics());
050            }
051        }
052    
053        public void paint(Graphics2D g, boolean drawExtras) {
054            System.out.println("paint");
055    //        new RuntimeException().printStackTrace();
056            // create the hardware accelerated image.
057            createBackBuffer();
058    
059            // Main rendering loop. Volatile images may lose their contents.
060            // This loop will continually render to (and produce if neccessary) volatile images
061            // until the rendering was completed successfully.
062            do {
063    
064                // Validate the volatile image for the graphics configuration of this
065                // component. If the volatile image doesn't apply for this graphics configuration
066                // (in other words, the hardware acceleration doesn't apply for the new device)
067                // then we need to re-create it.
068                GraphicsConfiguration gc = this.getGraphicsConfiguration();
069                int valCode = volatileImg.validate(gc);
070    
071                // This means the device doesn't match up to this hardware accelerated image.
072                if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
073                    createBackBuffer(); // recreate the hardware accelerated image.
074                }
075    
076                Graphics offscreenGraphics = volatileImg.getGraphics();
077    //   offscreenGraphics.setColor(Color.WHITE);
078    //   offscreenGraphics.fillRect(0, 0, getWidth(), getHeight());
079    //   offscreenGraphics.setColor(Color.BLACK);
080    //   offscreenGraphics.drawLine(0, 0, 10, 10); // arbitrary rendering logic
081                paintGraph(offscreenGraphics, drawExtras);
082                // paint back buffer to main graphics
083                g.drawImage(volatileImg, 0, 0, this);
084                // Test if content is lost
085            } while (volatileImg.contentsLost());
086        }
087    
088        // This method produces a new volatile image.
089        private void createBackBuffer() {
090            GraphicsConfiguration gc = getGraphicsConfiguration();
091            volatileImg = gc.createCompatibleVolatileImage(getWidth(), getHeight());
092        }
093    
094    //    public void update(Graphics g) {
095    //        wait += 100;
096    ////        paint(g);
097    //    }
098    
099        public void repaint(VertexModel src) {
100            Graphics gg = getGraphics();
101            repaint(src, gg);
102        }
103    
104        public void repaint(VertexModel src, Graphics gg) {
105            GraphPoint l = src.getLocation();
106            gg.setColor(GraphModel.getColor(src.getColor()));
107            if (src.isSelected())
108                gg.setColor(Color.black);
109            else
110                gg.setColor(GraphModel.getColor(src.getColor()));
111            gg.fillOval((int) l.x - 5, (int) l.y - 5, 10, 10);
112        }
113    
114        public void repaint(EdgeModel src) {
115            Graphics gg = getGraphics();
116            repaint(src, gg);
117        }
118    
119        public void repaint(EdgeModel src, Graphics gg) {
120            GraphPoint l, r;
121            l = src.source.getLocation();
122            r = src.target.getLocation();
123    
124    //        if (src.isSelected())
125    //            gg.setColor(Color.black);
126    //        else
127    //            gg.setColor(src.getRGBColor());
128            gg.drawLine((int) l.x, (int) l.y, (int) r.x, (int) r.y);
129        }
130    
131        public void vertexAdded(VertexModel v) {
132            v.setLabel(v.getId() + "");
133            v.setVertexListener(this);
134            repaint(v);
135        }
136    
137        public void edgeAdded(EdgeModel e) {
138            e.setLabel(e.getId());
139            e.setEdgeModelListener(this);
140    //        e.updateBounds();
141            repaint(e);
142        }
143    }