public class ColorDialog extends JDialog implements ActionListener
class ColorPanel extends JPanel
1:
2: package scribble3;
3:
4: import java.awt.*;
5: import java.awt.event.*;
6: import javax.swing.*;
7:
8: public class ColorDialog extends JDialog implements ActionListener {
9:
10: public ColorDialog(JFrame owner, String title) {
11: this(owner, title, Color.black);
12: }
13:
14: public ColorDialog(JFrame owner, String title, Color color) {
15: super(owner, title, true);
16: this.color = color;
17:
18: getContentPane().setLayout(new BorderLayout());
19:
20: JPanel topPanel = new JPanel();
21: topPanel.setLayout(new BorderLayout());
22: colorPanel = new ColorPanel(20, 20, 8, 8);
23: topPanel.add(colorPanel, BorderLayout.CENTER);
24: moreColorButton = new JButton("More colors");
25: moreColorButton.addActionListener(this);
26: topPanel.add(moreColorButton, BorderLayout.SOUTH);
27: getContentPane().add(topPanel, BorderLayout.CENTER);
28:
29: JPanel bottomPanel = new JPanel();
30: bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
31: okButton = new JButton("Ok");
32: okButton.addActionListener(this);
33: bottomPanel.add(okButton);
34: cancelButton = new JButton("Cancel");
35: cancelButton.addActionListener(this);
36: bottomPanel.add(cancelButton);
37: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
38:
39: pack();
40: }
41:
42: public Color showDialog() {
43: result = null;
44: colorPanel.setColor(color);
45: Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
46: Dimension dialogSize = getSize();
47: setLocation(screenSize.width / 2 - dialogSize.width / 2,
48: screenSize.height / 2 - dialogSize.height / 2);
49: show();
50: if (result != null) {
51: color = result;
52: }
53: return result;
54: }
55:
56: public void actionPerformed(ActionEvent e) {
57: Object source = e.getSource();
58: if (source == okButton) {
59: result = colorPanel.getColor();
60: } else if (source == moreColorButton) {
61: Color selectedColor = chooser.showDialog(ColorDialog.this,
62: "Choose color",
63: color);
64: if (selectedColor != null) {
65: colorPanel.setColor(selectedColor);
66: colorPanel.repaint();
67: }
68: return;
69: }
70: setVisible(false);
71: }
72:
73: protected JButton okButton;
74: protected JButton cancelButton;
75: protected JButton moreColorButton;
76: protected ColorPanel colorPanel;
77: protected JColorChooser chooser = new JColorChooser();
78: protected Color color = null;
79:
80: protected Color result = null;
81:
82: class ColorPanel extends JPanel {
83:
84: ColorPanel(int cellWidth, int cellHeight, int xpad, int ypad) {
85: if (cellWidth < 5) {
86: cellWidth = 5;
87: }
88: if (cellHeight < 5) {
89: cellHeight = 5;
90: }
91: if (xpad < 2) {
92: xpad = 2;
93: }
94: if (ypad < 2) {
95: ypad = 2;
96: }
97: this.cellWidth = cellWidth;
98: this.cellHeight = cellHeight;
99: this.xpad = xpad;
100: this.ypad = ypad;
101: rowCount = colorGrid.length;
102: columnCount = colorGrid[0].length;
103: dimension = new Dimension((cellWidth + xpad) * columnCount + xpad,
104: (cellHeight + ypad) * (rowCount + 1) + ypad);
105: addMouseListener(new MouseAdapter() {
106: public void mousePressed(MouseEvent event) {
107: Point p = event.getPoint();
108: int i = (p.y / (ColorPanel.this.cellHeight + ColorPanel.this.ypad));
109: int j = (p.x / (ColorPanel.this.cellWidth + ColorPanel.this.xpad));
110: if (i < rowCount &&
111: j < columnCount) {
112: color = colorGrid[i][j];
113: repaint();
114: }
115: }
116: });
117: }
118:
119: public void setColor(Color color) {
120: this.color = color;
121: }
122:
123: public Color getColor() {
124: return color;
125: }
126:
127: public Dimension getMinimumSize() {
128: return dimension;
129: }
130:
131: public Dimension getPreferredSize() {
132: return dimension;
133: }
134:
135: public void paint(Graphics g) {
136: Dimension dim = getSize();
137: g.setColor(Color.lightGray);
138: g.fillRect(0, 0, dim.width, dim.height);
139: int x, y;
140: for (int i = 0; i < rowCount; i++) {
141: for (int j = 0; j < columnCount; j++) {
142: x = (cellWidth + xpad) * j + xpad;
143: y = (cellHeight + ypad) * i + ypad;
144: g.setColor(colorGrid[i][j]);
145: g.fillRect(x, y, cellWidth, cellHeight);
146: g.setColor(Color.black);
147: g.drawRect(x, y, cellWidth, cellHeight);
148: }
149: }
150: x = xpad;
151: y = (cellHeight + ypad) * rowCount + ypad;
152: int width = (cellWidth + xpad) * columnCount - xpad;
153: g.setColor(color);
154: g.fillRect(x, y, width, cellHeight);
155: g.setColor(Color.black);
156: g.drawRect(x, y, width, cellHeight);
157: }
158:
159: protected Color color;
160:
161: protected int cellWidth;
162: protected int cellHeight;
163: protected int rowCount;
164: protected int columnCount;
165: protected int xpad;
166: protected int ypad;
167: protected Dimension dimension;
168:
169: protected Color[][] colorGrid = {
170: { Color.white, Color.lightGray, Color.darkGray, Color.black},
171: { Color.gray, Color.blue, Color.cyan, Color.green },
172: { Color.yellow, Color.orange, Color.pink, Color.red },
173: { Color.magenta, new Color(230, 230, 250), new Color(0, 0, 128), new Color(64, 224, 208) } };
174:
175: }
176:
177: }