svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / DefaultColorTablePainter.java @ 43476
History | View | Annotate | Download (3.16 KB)
1 | 40561 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.gui; |
25 | |||
26 | import java.awt.Color; |
||
27 | import java.awt.Graphics2D; |
||
28 | import java.awt.Rectangle; |
||
29 | import java.util.ArrayList; |
||
30 | import java.util.List; |
||
31 | |||
32 | public class DefaultColorTablePainter implements ColorTablePainter { |
||
33 | private class ColorItem { |
||
34 | Color color = null; |
||
35 | double value =0; |
||
36 | |||
37 | ColorItem(double value, Color color) { |
||
38 | this.color = color;
|
||
39 | this.value = value;
|
||
40 | } |
||
41 | |||
42 | Color getColor() {
|
||
43 | return color;
|
||
44 | } |
||
45 | |||
46 | double getValue() {
|
||
47 | return value;
|
||
48 | } |
||
49 | } |
||
50 | |||
51 | List<ColorItem> colorItems = null; |
||
52 | 43476 | jjdelcerro | String name;
|
53 | 40435 | jjdelcerro | |
54 | 43476 | jjdelcerro | public DefaultColorTablePainter() {
|
55 | this.colorItems = new ArrayList<>(); |
||
56 | 40435 | jjdelcerro | int n = 256; |
57 | for( int i=0; i<n; i++) { |
||
58 | Color color = Color.getHSBColor((float) i / (float) n, 0.85f, 1.0f); |
||
59 | this.colorItems.add( new ColorItem(i,color)); |
||
60 | } |
||
61 | 43476 | jjdelcerro | name = "Default colot table";
|
62 | } |
||
63 | 40435 | jjdelcerro | |
64 | 43476 | jjdelcerro | public DefaultColorTablePainter(Color[] colors, String name) { |
65 | this.colorItems = new ArrayList<>(); |
||
66 | for( int i=0; i<colors.length; i++) { |
||
67 | this.colorItems.add( new ColorItem(i, colors[i])); |
||
68 | } |
||
69 | this.name = name;
|
||
70 | } |
||
71 | 40435 | jjdelcerro | |
72 | 43476 | jjdelcerro | @Override
|
73 | 40435 | jjdelcerro | public Color[] getColors() { |
74 | Color[] colors = new Color[colorItems.size()]; |
||
75 | for (int i = 0; i < colors.length; i++) { |
||
76 | colors[i] = ((ColorItem) colorItems.get(i)).getColor(); |
||
77 | } |
||
78 | return colors;
|
||
79 | } |
||
80 | |||
81 | 43476 | jjdelcerro | @Override
|
82 | 40435 | jjdelcerro | public String getTableName() { |
83 | 43476 | jjdelcerro | return name;
|
84 | 40435 | jjdelcerro | } |
85 | |||
86 | 43476 | jjdelcerro | @Override
|
87 | 40435 | jjdelcerro | public void paint(Graphics2D g, boolean isSelected) { |
88 | Rectangle area = g.getClipBounds();
|
||
89 | area.y=0;
|
||
90 | area.x=0;
|
||
91 | int x1 = area.x;
|
||
92 | int x2 = area.x + area.width - 1; |
||
93 | |||
94 | if( this.colorItems.size() > 0 ) { |
||
95 | double min = colorItems.get(0).getValue(); |
||
96 | double max = colorItems.get(colorItems.size() - 1).getValue(); |
||
97 | for (int i = area.x; i < (area.x + area.width - 1); i++) { |
||
98 | double pos = min + (((max - min) * (i - area.x)) / (area.width - 2)); |
||
99 | g.setColor(this.colorItems.get((int) pos).getColor()); |
||
100 | g.drawLine(i, area.y, i, area.y + area.height); |
||
101 | } |
||
102 | } else {
|
||
103 | g.setColor(new Color(224, 224, 224)); |
||
104 | g.fillRect(x1, area.y, x2 - x1, area.height - 1);
|
||
105 | } |
||
106 | if (isSelected) {
|
||
107 | g.setColor(Color.black);
|
||
108 | } else {
|
||
109 | g.setColor(new Color(96, 96, 96)); |
||
110 | } |
||
111 | g.drawRect(x1, area.y, x2 - x1, area.height - 1);
|
||
112 | } |
||
113 | |||
114 | } |