Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / DefaultColorTablePainter.java @ 38371

History | View | Annotate | Download (1.89 KB)

1
package org.gvsig.gui;
2

    
3
import java.awt.Color;
4
import java.awt.Graphics2D;
5
import java.awt.Rectangle;
6
import java.util.ArrayList;
7
import java.util.List;
8

    
9
public class DefaultColorTablePainter implements ColorTablePainter {
10
        private class ColorItem {
11
                Color color = null;
12
                double value =0;
13
                
14
                ColorItem(double value, Color color) {
15
                        this.color = color;
16
                        this.value = value;
17
                }
18
                
19
                Color getColor() {
20
                        return color;
21
                }
22
                
23
                double getValue() {
24
                        return value;
25
                }
26
        }
27
        
28
        List<ColorItem> colorItems = null;
29
        
30
        DefaultColorTablePainter() {
31
                this.colorItems = new ArrayList<ColorItem>();
32
                int n = 256;
33
                for( int i=0; i<n; i++) {
34
                Color color = Color.getHSBColor((float) i / (float) n, 0.85f, 1.0f);
35
                this.colorItems.add( new ColorItem(i,color));
36
        }
37

    
38
        }
39
        
40
        public Color[] getColors() {
41
                Color[] colors = new Color[colorItems.size()];
42
                for (int i = 0; i < colors.length; i++) {
43
                        colors[i] = ((ColorItem) colorItems.get(i)).getColor();
44
                }
45
                return colors;        
46
        }
47

    
48
        public String getTableName() {
49
                return "Default colot table";
50
        }
51

    
52
        public void paint(Graphics2D g, boolean isSelected) {
53
                Rectangle area = g.getClipBounds();
54
                area.y=0;
55
                area.x=0;
56
                int x1 = area.x;
57
                int x2 = area.x + area.width - 1;
58

    
59
                if( this.colorItems.size() > 0 ) {
60
                        double min = colorItems.get(0).getValue();
61
            double max = colorItems.get(colorItems.size() - 1).getValue();
62
            for (int i = area.x; i < (area.x + area.width - 1); i++) {
63
                                double pos = min + (((max - min) * (i - area.x)) / (area.width - 2));
64
                                g.setColor(this.colorItems.get((int) pos).getColor());
65
                                g.drawLine(i, area.y, i, area.y + area.height);
66
                        }
67
                } else {
68
                        g.setColor(new Color(224, 224, 224));
69
                        g.fillRect(x1, area.y, x2 - x1, area.height - 1);
70
                }
71
                if (isSelected) {
72
                        g.setColor(Color.black);
73
                } else {
74
                        g.setColor(new Color(96, 96, 96));
75
                }
76
                g.drawRect(x1, area.y, x2 - x1, area.height - 1);
77
        }
78

    
79
}