-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorFrame.java
More file actions
104 lines (91 loc) · 2.7 KB
/
ColorFrame.java
File metadata and controls
104 lines (91 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** @author Gergely Kota
ColorFrame is a popup menu that prompts for a color to select.
It shows a JColorChooser in a modal JDialog, and senses when
a color has been selected. At this point, it minimizes the window
thereby allowing the program flow to continue. The selected color
is then returned.
*/
public class ColorFrame extends JDialog
{
private JColorChooser jcc;
private Color color;
private static ColorFrame instance;
private boolean cancel;
// initializer creates the single instance the first time
// the class is accessed
static
{
instance = new ColorFrame();
}
// force single instance of ColorFrame via static initializer
// this is because the same task is done each time - the window
// pops up and returns a selected color.
private ColorFrame()
{
// setSize(400, 300);
setTitle("Color Selection");
setModal(true);
setLocation(300, 300);
setResizable(false);
getContentPane().add(jcc = new JColorChooser(), BorderLayout.CENTER);
JButton jb = new JButton("Select Color");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
color = jcc.getColor();
}
});
JButton jbc = new JButton("Cancel");
jbc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cancel = true;
}
});
JPanel bottom = new JPanel(new GridLayout(1,2));
bottom.add(jb);
bottom.add(jbc);
getContentPane().add(bottom, BorderLayout.SOUTH);
pack();
}
/** pauses the flow of the program until a Color is selected.
It then returns that Color and allows the calling program to continue.
@return the selected Color
*/
public static synchronized Color getColor()
{
// set the color to return to null
// start a timer that checks if the color to return has been set
// when it has, the window is minimized so that the control can continue
instance.color = null;
instance.cancel = false;
Timer check = new Timer(2, null);
check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(instance.color != null)
instance.hide();
if(instance.cancel)
instance.hide();
}
});
check.start();
instance.show();
check.stop();
return instance.color;
}
/* ------------------------------------------------------- */
/* ------------------------------------------------------- */
/* ------------------------------------------------------- */
public static void main(String[] args)
{
Debug.println("Started");
Color c = ColorFrame.getColor();
Debug.println("Ended");
Debug.println(c);
Debug.println("Started");
c = ColorFrame.getColor();
Debug.println("Ended");
Debug.println(c);
}
}