001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Nov 28, 2002
005     * Time: 10:30:02 AM
006     */
007    
008    package EVolve.util.painters;
009    
010    import EVolve.Scene;
011    import EVolve.util.painters.shapes.Box;
012    import EVolve.util.ColorCache;
013    import EVolve.visualization.AutoImage;
014    import EVolve.visualization.AxesPanel;
015    
016    import java.util.*;
017    
018    public class BarChartPainter extends Painter{
019        private HashMap bars; // value of the bars
020        private long xMax;
021        private int barHeight;
022        private boolean useRandomColor;
023        private ColorCache colorCache;
024        private AxesPanel canvas;
025    
026    
027        public BarChartPainter(AxesPanel canvas, int barHeight, boolean useRandomColor) {
028            bars = new HashMap();
029            xMax = 0;
030            this.barHeight = barHeight;
031            this.useRandomColor = useRandomColor;
032            colorCache = new ColorCache();
033            this.canvas = canvas;
034        }
035    
036        public String getName() {
037            return "Bar Chart Painter";
038        }
039    
040        public void paint(AutoImage image, long x, long y, long z) {
041            Box bar = null;
042            if (bars.containsKey(new Long(y))) {
043                bar = (Box)bars.get(new Long(y));
044            } else {
045                bar = new Box(0L,(int)y,0,barHeight,false,canvas);
046                if (useRandomColor)
047                    bar.setColor(colorCache.getNextColor(y));
048                else {
049                    bar.setColor(Scene.getColor());
050                }
051                bars.put(new Long(y), bar);
052                image.setColor(0,(int)y,bar);
053            }
054    
055            bar.setSize(bar.getRealWidth()+(int)x,barHeight);
056            bar.move(0,(int)y*barHeight);
057    
058            if (xMax < bar.getRealWidth())
059                xMax = bar.getRealWidth();
060    
061            image.setImageSize((int)(xMax > Box.maxLength ? Box.maxLength : xMax),barHeight*bars.size());
062        }
063    
064        public long getxMax() {
065            return xMax;
066        }
067    
068        public int[] getValue() {
069            int[] value = new int[bars.size()];
070            Iterator it = bars.keySet().iterator();
071            while (it.hasNext()) {
072                Long key = (Long)it.next();
073                value[key.intValue()] = ((Box)bars.get(key)).getRealWidth();
074            }
075            return value;
076        }
077    
078        public Object clone() {
079            BarChartPainter o = null;
080            o = (BarChartPainter)super.clone();
081            return o;
082        }
083    }