Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ColorScale.java

00001 /*
00002  * ColorScale.java
00003  *
00004  * Created on 26 June 2003, 14:34
00005  */
00006 
00007 package DisplayGUI;
00008 import java.awt.Color;
00009 import java.util.*;
00010 import java.awt.Graphics2D.*;
00011 import java.awt.geom.*;
00012 import java.awt.GradientPaint;
00013 import java.awt.FontMetrics;
00018 public class ColorScale implements displayParams {
00019 //    Vector scalePoints;
00020     List scalePoints;
00021     boolean isGradientPaint;                               
00022     boolean logScale;                                      
00023     String title=null;
00024     SortedMap sortedMap;
00025 
00027     public ColorScale(double xlow, Color loColor, String loLabel, double xHigh, Color hiColor, String hiLabel) {
00028 //        scalePoints = new Vector();
00029 //        scalePoints.addElement(new ColorScalePoint(new Double(xlow),loColor,loLabel));
00030 //        scalePoints.addElement(new ColorScalePoint(new Double(xHigh),hiColor,hiLabel));
00031         scalePoints = new ArrayList();
00032         scalePoints.add(new ColorScalePoint(new Double(xlow),loColor,loLabel));
00033         scalePoints.add(new ColorScalePoint(new Double(xHigh),hiColor,hiLabel));
00034         isGradientPaint=true;
00035         logScale=false;
00036     }
00037     public ColorScale(String title, boolean isLog, boolean isGrad) {
00038          this.title=title;
00039          logScale=isLog;
00040          isGradientPaint=isGrad;
00041          scalePoints = new ArrayList();
00042 //         scalePoints=new Vector();
00043          }
00044 
00045 
00047     public ColorScale(double xlow, Color loColor, String loLabel) {
00048         scalePoints = new ArrayList();
00049         scalePoints.add(new ColorScalePoint(new Double(xlow),loColor,loLabel));
00050 //        scalePoints = new Vector();
00051 //        scalePoints.addElement(new ColorScalePoint(xlow,loColor,loLabel));
00052         isGradientPaint=false;
00053         logScale=false;
00054         }
00055 
00056     public ColorScale(String title, List scalePoints, boolean isGradientPaint, boolean isLogScale) {
00057         this.scalePoints = scalePoints;
00058         this.title=title;
00059         this.isGradientPaint=isGradientPaint;
00060         this.logScale=isLogScale;
00061         }
00062     public ColorScale(ColorScale newScale) {
00063         title=newScale.getTitle();
00064         isGradientPaint=newScale.isGradientPaint();
00065         logScale=newScale.isLogScale();
00066         scalePoints = new ArrayList(newScale.getScalePoints());
00067         }
00068 
00069     public String setTitle(String title) {
00070          this.title=title;
00071          return title;
00072          }
00073     public String getTitle() {
00074          if(title==null) return "No title";
00075          return title;
00076          }
00077     public void setLogScale(boolean log) {
00078         logScale=log;
00079         }
00080     public boolean isLogScale() {
00081         return logScale;
00082         }
00083     public void setGradientPaint(boolean isGradientPaint) {
00084         this.isGradientPaint=isGradientPaint;
00085         }
00086     public boolean isGradientPaint() {
00087         return isGradientPaint;
00088         }
00089 
00091     public void add(double value, Color color, String label) {
00092         scalePoints.add(new ColorScalePoint(new Double(value),color,label));
00093         orderList();
00094         }
00095 //************************************************* reorder list by value
00096     private void orderList() {
00097 
00098         sortedMap = new TreeMap();              // sort the points by value
00099         for(int i=0;i<scalePoints.size();i++) {
00100            ColorScalePoint csp = (ColorScalePoint)scalePoints.get(i);
00101            sortedMap.put(csp.getValue(),csp);
00102            }
00103         scalePoints = new ArrayList();
00104         for (Iterator i=sortedMap.values().iterator(); i.hasNext(); ) {       
00105           scalePoints.add((ColorScalePoint)i.next());
00106           }        
00107     }
00108 
00110     public void drawColorScale(java.awt.Graphics2D g2, int x, int y) {
00111 
00112             double firstDataPoint = ((ColorScalePoint)scalePoints.get(0)).getValue().doubleValue();
00113             double lastDataPoint = ((ColorScalePoint)scalePoints.get(scalePoints.size()-1)).getValue().doubleValue();
00114             if(!isGradientPaint) {
00115                  drawSolidColorScale(g2,x,y);
00116                  return;
00117                  }
00118             if(logScale) {
00119                 lastDataPoint = Math.log(lastDataPoint)/Math.log(10.0);
00120                 firstDataPoint = Math.log(firstDataPoint)/Math.log(10.0);
00121                 }
00122 
00123             Rectangle2D.Double drawRect;
00124             ColorScalePoint firstPoint = (ColorScalePoint)scalePoints.get(0);
00125             double lastValue = firstPoint.getValue().doubleValue();
00126             if(logScale) lastValue = Math.log(lastValue)/Math.log(10.0); // get log to base 10
00127             Color lastColor = firstPoint.getColor();
00128             String thisLabel = firstPoint.getLabel();
00129             
00130             drawAxisLabel(g2,x,y, thisLabel);
00131             
00132             int xBox = x;
00133                       
00134             for(int i=0;i<scalePoints.size();i++) {
00135                 ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00136                 double nextValue = thisPoint.getValue().doubleValue();
00137                 if(logScale) nextValue=Math.log(nextValue)/Math.log(10.0);
00138                 Color nextColor = thisPoint.getColor();
00139                 thisLabel = thisPoint.getLabel();
00140  
00141                 double thisFraction = (nextValue-lastValue)/(lastDataPoint-firstDataPoint);
00142                 int xValue = (int)(thisFraction*(double)colorBoxWidth);
00143 //                System.out.println("Drawing for value "+Double.toString(nextValue));
00144 //                System.out.println("Drawing for color "+nextColor.toString());
00145 //                System.out.println("Drawing for label "+thisLabel);
00146 //                System.out.println("Fraction="+Double.toString(thisFraction));
00147 //                System.out.println("xValue "+Integer.toString(xValue));
00148     
00149                 drawAxisLabel(g2,xBox+xValue,y, thisLabel); 
00150                 
00151                 Color fillColor= lastColor;
00152 
00153 
00154                 if(!nextColor.equals(lastColor)) {
00155                     GradientPaint colorGradient = new GradientPaint(xBox, y, lastColor,xValue+xBox,y,nextColor);
00156                     g2.setPaint(colorGradient);
00157                 }
00158                 else {
00159                     g2.setPaint(lastColor);
00160                    }                    
00161 
00162                 int xWidth = (i==scalePoints.size()-1) ? x+colorBoxWidth-xBox : xValue;
00163                 drawRect = new Rectangle2D.Double(xBox, y, xWidth, colorBoxHeight);
00164                 g2.fill(drawRect);
00165                 // now the tickmarks and labels
00166                                 
00167                 lastColor=nextColor;
00168                 lastValue=nextValue;
00169                 xBox+=xValue;
00170             }
00171             
00172             // now draw the outline
00173              drawRect = new Rectangle2D.Double(x, y, colorBoxWidth, colorBoxHeight);
00174 //             System.out.println("Bad2: "+Double.toString(drawRect.getX())+","+Double.toString(drawRect.getY())+","+Double.toString(drawRect.getWidth())+","+Double.toString(drawRect.getHeight()));
00175              g2.setPaint(Color.black);
00176              g2.draw(drawRect);
00177             
00178             
00179  
00180             
00181     }
00182 
00183     public void drawSolidColorScale(java.awt.Graphics2D g2, int x, int y) {
00184 
00185             Rectangle2D.Double drawRect;            
00186             int nboxes = scalePoints.size();
00187             int boxWidth = (int)((double)colorBoxWidth/(double)nboxes);        
00188             
00189             int xBox = 0;
00190             
00191             for(int i=0;i<scalePoints.size();i++) {
00192                 ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00193                 double nextValue = thisPoint.getValue().doubleValue();
00194                 if(logScale) nextValue=Math.log(nextValue)/Math.log(10.0);
00195                 Color nextColor = thisPoint.getColor();
00196                 String thisLabel = thisPoint.getLabel();
00197                 
00198                 drawAxisLabel(g2,x+xBox+boxWidth/2,y, thisLabel);                 
00199                 g2.setPaint(nextColor);
00200 
00201                 drawRect = new Rectangle2D.Double(x+xBox, y, boxWidth, colorBoxHeight);
00202                 g2.fill(drawRect);
00203                 // now the tickmarks and labels
00204                                 
00205                 xBox+=boxWidth;
00206             }
00207             
00208             // now draw the outline
00209              drawRect = new Rectangle2D.Double(x, y, nboxes*boxWidth, colorBoxHeight);
00210 //             System.out.println("Bad2: "+Double.toString(drawRect.getX())+","+Double.toString(drawRect.getY())+","+Double.toString(drawRect.getWidth())+","+Double.toString(drawRect.getHeight()));
00211              g2.setPaint(Color.black);
00212              g2.draw(drawRect);
00213                         
00214     }
00215 
00217     public void drawAxisLabel(java.awt.Graphics2D g2, int x, int y, String label) {
00218         g2.setPaint(Color.black);
00219 
00220         g2.draw(new Line2D.Double(x, y, x, y-tickHeight));
00221         g2.setFont(displayScaleFont);
00222         g2.setPaint(darkBlueColor);
00223         FontMetrics metrics = g2.getFontMetrics();
00224         int stringWidth = metrics.stringWidth(label);
00225         int stringHeight = metrics.getHeight();  
00226         g2.drawString(label,x-stringWidth/2,y-tickHeight-tickLabelOffSet);
00227  
00228     }
00229 
00231     public Color getColor(Double dValue) {
00232 //      System.out.println("requested color for "+value);
00233 //      printData();
00234       if(dValue==null) return Color.white;
00235       double value =dValue.doubleValue();
00236 
00237       if(!isGradientPaint) {
00238  
00239           for(int i=0;i<scalePoints.size();i++) {
00240                ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00241                 double thisValue = thisPoint.getValue().doubleValue();
00242                 if(value<=thisValue) return thisPoint.getColor();
00243                 }
00244            return ((ColorScalePoint)scalePoints.get(scalePoints.size()-1)).getColor();
00245          }
00246        else {
00247            if(logScale) value = Math.log(value)/Math.log(10.0);
00248            Color loColor = ((ColorScalePoint)scalePoints.get(0)).getColor();
00249            double loValue = ((ColorScalePoint)scalePoints.get(0)).getValue().doubleValue();
00250            if(logScale) loValue = Math.log(loValue)/Math.log(10.0);
00251            if(value<=loValue) return loColor;
00252 
00253            for(int i=1;i<scalePoints.size();i++) {
00254              ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00255 //             System.out.println("Point "+i+":"+thisPoint.getValue().toString()+","+thisPoint.getLabel()+","+thisPoint.getColor().toString());
00256 
00257              double hiValue = (logScale) ? Math.log(thisPoint.getValue().doubleValue())/Math.log(10.0) : thisPoint.getValue().doubleValue();
00258              Color hiColor = thisPoint.getColor();
00259 //             System.out.println("Data between "+Double.toString(loValue)+" and "+Double.toString(hiValue));
00260              if(value<=hiValue) {
00261                     int loRed = loColor.getRed();
00262                     int hiRed = hiColor.getRed();
00263                     int loBlue = loColor.getBlue();
00264                     int hiBlue = hiColor.getBlue();
00265                     int loGreen = loColor.getGreen();
00266                     int hiGreen = hiColor.getGreen();
00267                     double fraction = (value-loValue)/(hiValue-loValue);
00268 //                    System.out.println("fraction="+Double.toString(fraction));
00269 //                    System.out.println("scalecolor: "+loRed+","+hiRed+":"+loGreen+","+hiGreen+":"+loBlue+","+hiBlue);
00270                     if(fraction<0.0) fraction=0.0;
00271                     if(fraction>1.0) fraction=1.0;
00272                     double redValue = (double)(hiRed-loRed)*fraction;
00273                     double blueValue = (double)(hiBlue-loBlue)*fraction;
00274                     double greenValue = (double)(hiGreen-loGreen)*fraction;
00275 //                    int newRed=loRed+(int)redValue;
00276 //                    int newGreen=loGreen+(int)greenValue;
00277 //                    int newBlue=loBlue+(int)blueValue;
00278 //                    System.out.println("newcolor: "+newRed+","+newGreen+","+newBlue);
00279                     return new Color(loRed+(int)redValue,loGreen+(int)greenValue,loBlue+(int)blueValue);
00280                     }
00281                loColor=hiColor;
00282                loValue=hiValue;
00283               }
00284             return loColor;  // ie, return the hiColor because value must be > greatesthiValue
00285            }
00286 
00287     }
00288 
00289    public String getLabel(Double value) {
00290          if(value==null) return new String("No Data");
00291          try {
00292            if(sortedMap.containsKey(value)) {
00293              String theLabel = ((ColorScalePoint)sortedMap.get(value)).getLabel(); 
00294              if(theLabel.indexOf("<")==-1 && theLabel.indexOf(">")==-1) return theLabel;
00295              }
00296           }catch(Exception e){};  // in case value cannot be represented as a number
00297           return value.toString();
00298          }
00299 
00300   public List getScalePoints() {
00301       return scalePoints;
00302      }
00303   public int getNoPoints() {
00304       return scalePoints.size();
00305      }
00306   public ColorScalePoint getColorScalePoint(int index) {
00307       if(index<0) return null;
00308       return (ColorScalePoint) scalePoints.get(index);
00309       }
00310   public void setColorScalePoint(int index, ColorScalePoint csp) {
00311       scalePoints.set(index,csp);
00312       }
00313   public void setLastPoint(double lastValue) {
00314       ColorScalePoint scalePoint = (ColorScalePoint)scalePoints.get(scalePoints.size()-1);
00315       scalePoint.setValue(new Double(lastValue));
00316       scalePoint.setLabel(Double.toString(lastValue));
00317       }
00318   public void setLastPoint(int lastValue) {
00319       ColorScalePoint scalePoint = (ColorScalePoint)scalePoints.get(scalePoints.size()-1);
00320       scalePoint.setValue(new Double(lastValue));
00321       scalePoint.setLabel(Integer.toString(lastValue));
00322       }
00323   public void removePoint(int index) {
00324       if(index<0) return;
00325       scalePoints.remove(index);
00326       }
00327   public void add(ColorScalePoint newPoint) {
00328       scalePoints.add(newPoint);
00329       orderList();
00330       }
00331 
00332   public void printData() {
00333       System.out.println("ColorScale");
00334       System.out.println("----------");
00335       System.out.println(title);
00336       System.out.println("log="+logScale+" gradient="+isGradientPaint);
00337       System.out.println("Points:");
00338       for(int i=0;i<scalePoints.size();i++) {
00339        ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00340        System.out.println("value,label,color="+thisPoint.getValue().doubleValue()+","+thisPoint.getLabel()+","+thisPoint.getColor().toString());
00341        }
00342       }
00343 
00344   public boolean equals(ColorScale otherScale) {
00345       if(logScale != otherScale.isLogScale()) return false;
00346       if(isGradientPaint != otherScale.isGradientPaint()) return false;
00347       if(!title.equals(otherScale.getTitle())) return false;
00348       if(scalePoints.size()!=otherScale.getNoPoints()) return false;
00349       List otherScalePoints = otherScale.getScalePoints();
00350       for(int i=0;i<scalePoints.size();i++) {
00351          ColorScalePoint thisPoint = (ColorScalePoint)scalePoints.get(i);
00352          ColorScalePoint otherPoint = (ColorScalePoint)otherScalePoints.get(i);
00353          if(!thisPoint.equals(otherPoint)) return false;
00354          }
00355        return true;
00356        }
00357     
00358 
00359     
00360 }

Generated on Thu Jul 15 09:55:40 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5