Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Panels / ColorPanel.java @ 312

History | View | Annotate | Download (3.65 KB)

1

    
2
/*
3
 * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
4
 * for visualizing and manipulating spatial features with geometry and attributes.
5
 *
6
 * Copyright (C) 2003 Vivid Solutions
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
 *
22
 * For more information, contact:
23
 *
24
 * Vivid Solutions
25
 * Suite #1A
26
 * 2328 Government Street
27
 * Victoria BC  V8T 5G5
28
 * Canada
29
 *
30
 * (250)385-6040
31
 * www.vividsolutions.com
32
 */
33

    
34
package com.iver.cit.gvsig.gui.Panels;
35

    
36
import java.awt.BasicStroke;
37
import java.awt.Color;
38
import java.awt.Graphics;
39
import java.awt.Graphics2D;
40

    
41
import javax.swing.JPanel;
42

    
43

    
44
/**
45
 * Displays a colour.
46
 */
47
public class ColorPanel extends JPanel {
48
    private Color fillColor = Color.red;
49
    private Color lineColor = Color.green;
50
    private int margin = 0;
51

    
52
    public ColorPanel() {
53
        setBackground(Color.white);
54
    }
55

    
56
    protected void paintComponent(Graphics g) {
57
        super.paintComponent(g);
58

    
59
        //Before I simply set the ColorPanel's background colour. But I found this
60
        //caused weird paint effects e.g. a second copy of the panel appearing
61
        //at the top left corner of the rendered image. [Jon Aquino].
62
        //<<TODO:DESIGN>> Use the GraphicsState class [Jon Aquino]
63
        Color originalColor = g.getColor();
64
        g.setColor(getBackground());
65
        ((Graphics2D)g).setStroke(fillStroke);
66
        g.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
67
        g.setColor(fillColor);
68
        g.fillRect(margin, margin, getWidth() - 1 - margin - margin,
69
            getHeight() - 1 - margin - margin);
70

    
71
        if (lineColor != null) {
72
            g.setColor(lineColor);
73
            ((Graphics2D)g).setStroke(lineStroke);
74

    
75
            //-1 to ensure the rectangle doesn't extend past the panel [Jon Aquino]
76
            g.drawRect(margin, margin, getWidth() - 1 - margin - margin,
77
                getHeight() - 1 - margin - margin);
78
        }
79

    
80
        //<<TODO:DESIGN>> Put the next line in a finally block [Jon Aquino]
81
        g.setColor(originalColor);
82
    }
83

    
84
    /** Workaround for bug 4238829 in the Java bug database */
85
    public void setBounds(int x, int y, int w, int h) {
86
        super.setBounds(x, y, w, h);
87
        validate();
88
    }
89

    
90
    public void setFillColor(Color fillColor) {
91
        this.fillColor = fillColor;
92
    }
93

    
94
    /**
95
     * @param lineColor the new line colour, or null to not draw the line
96
     */
97
    public void setLineColor(Color lineColor) {
98
        this.lineColor = lineColor;
99
    }
100

    
101
    public void setMargin(int margin) {
102
        this.margin = margin;
103
    }
104
    public Color getFillColor() {
105
        return fillColor;
106
    }
107

    
108
    public Color getLineColor() {
109
        return lineColor;
110
    }
111
    
112
    private BasicStroke fillStroke = new BasicStroke(1);
113
    private int lineWidth = 1;
114
    private BasicStroke lineStroke = new BasicStroke(lineWidth);
115
    
116
    public void setLineWidth(int lineWidth) {
117
        lineStroke = new BasicStroke(lineWidth);
118
        this.lineWidth = lineWidth;
119
    }
120
    
121
    public int getLineWidth() {
122
        return lineWidth;
123
    }
124

    
125
}