Source code: jplot/SwatchPanel.java
1 /*
2 * JPLOT -- Java Plotting Interface for any programme or
3 * as an independent GUI
4 *
5 * Copyright (C) 1999 Jan van der Lee
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 *
22 * Send bugs, suggestions or queries to <jplot@cig.ensmp.fr>
23 * The latest releases are found at
24 * http://www.cig.ensmp.fr/~vanderlee/jplot.html
25 *
26 * Initally developed for use by the Centre d'Informatique Geologique
27 * Ecole des Mines de Paris, Fontainebleau, France.
28 */
29
30 package jplot;
31
32 import java.awt.*;
33 import javax.swing.*;
34
35 /**
36 * SwatchPanel builds a chooser panel from a series of rectangular
37 * chooseable 'buttons'... Note that this class must be derived in
38 * order to be used properly.
39 */
40 class SwatchPanel extends JPanel {
41
42 protected Dimension swatchSize;
43 protected Dimension numSwatches;
44 protected Dimension gap;
45 protected int Ns;
46 Color[] lowerLeftLineColor;
47 Color[] upperRightLineColor;
48 protected int width, height;
49
50 /**
51 * Constructs the swatch panel
52 * @param selectedIndex index of the current selected index
53 */
54 public SwatchPanel(int selectedIndex) {
55 initValues();
56 Ns = numSwatches.width*numSwatches.height;
57 setToolTipText("");
58 setOpaque(true);
59 gap = new Dimension(2,2);
60 lowerLeftLineColor = new Color [Ns];
61 upperRightLineColor = new Color [Ns];
62 setRequestFocusEnabled(false);
63 resetBackgrounds();
64 if (selectedIndex >= 0) {
65 lowerLeftLineColor[selectedIndex] = Color.white;
66 upperRightLineColor[selectedIndex] = Color.black;
67 }
68 width = numSwatches.width*(swatchSize.width + gap.width) -1;
69 height = numSwatches.height*(swatchSize.height + gap.height) -1;
70 }
71
72 /**
73 * Constructs the swatch panel
74 */
75 public SwatchPanel() {
76 this(0);
77 }
78
79 protected void initValues() {
80 swatchSize = new Dimension(1,1);
81 numSwatches = new Dimension(1,1);
82 }
83
84 protected void resetBackgrounds() {
85 for (int i=0; i<Ns; i++) {
86 lowerLeftLineColor[i] = Color.black;
87 upperRightLineColor[i] = Color.white;
88 }
89 }
90
91 /**
92 * Presses the swatch with the specified index, which correspond
93 * to the actually selected swatch.
94 * @param index index of the selected swatch.
95 */
96 protected void setSelectedSwatch(int index) {
97 for (int i=0; i<Ns; i++) {
98 lowerLeftLineColor[i] = Color.black;
99 upperRightLineColor[i] = Color.white;
100 }
101 if (index >= 0) {
102 lowerLeftLineColor[index] = Color.white;
103 upperRightLineColor[index] = Color.black;
104 }
105 repaint();
106 }
107
108 protected void paintIt(Graphics2D g2, int column, int row, int x, int y) {}
109
110 public void paintComponent(Graphics g) {
111 if (Ns == 0) return;
112 Graphics2D g2 = (Graphics2D)g;
113 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
114 RenderingHints.VALUE_ANTIALIAS_ON);
115 g2.setColor(getBackground());
116 g2.fillRect(0,0,getWidth(), getHeight());
117 for (int row=0; row<numSwatches.height; row++) {
118 for (int column=0; column<numSwatches.width; column++) {
119 int x = column*(swatchSize.width+gap.width);
120 int y = row*(swatchSize.height+gap.height);
121 int index = row*numSwatches.width+column;
122 paintIt(g2,column,row,x,y);
123 g2.setColor(lowerLeftLineColor[index]);
124 g2.drawLine(x+swatchSize.width-1,y,x+swatchSize.width-1,y+swatchSize.height-1);
125 g2.drawLine(x,y+swatchSize.height-1,x+swatchSize.width-1,y+swatchSize.height-1);
126 g2.setColor(upperRightLineColor[index]);
127 g2.drawLine(x,y,x+swatchSize.width-1,y);
128 g2.drawLine(x,y,x,y+swatchSize.height-1);
129 }
130 }
131 }
132
133 public Dimension getPreferredSize() {
134 return new Dimension(width,height);
135 }
136
137 public Dimension getMaximumSize() {
138 return getPreferredSize();
139 }
140
141 public Dimension getMinimumSize() {
142 return getPreferredSize();
143 }
144
145 public int getIndex(int x, int y) {
146 return (getRow(y)*numSwatches.width)+getColumn(x);
147 }
148
149 public int getColumn(int x) {
150 return x/(swatchSize.width+gap.width);
151 }
152
153 public int getRow(int y) {
154 return y/(swatchSize.height+gap.height);
155 }
156 }