-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputWH.java
More file actions
117 lines (101 loc) · 3.18 KB
/
Copy pathInputWH.java
File metadata and controls
117 lines (101 loc) · 3.18 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
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
public class InputWH {
public int width;
public int height;
Draw draw = new Draw();
InputWH() {
showInput();
}
private void showInput() {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
try {
UIManager.setLookAndFeel(info.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
break;
}
}
JPanel p = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0,1,2,2));
JPanel labels1 = new JPanel(new FlowLayout());
labels.add(new JLabel("Width", SwingConstants.RIGHT));
labels.add(new JLabel("Height", SwingConstants.RIGHT));
labels1.add(new JLabel("Minimum Width:900, Height: 800"));
p.add(labels, BorderLayout.WEST);
p.add(labels1, BorderLayout.SOUTH);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField widthField = new JTextField();
controls.add(widthField);
JTextField heightField = new JTextField();
heightField.addAncestorListener(new RequestFocusListener(false));
controls.add(heightField);
p.add(controls, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, p, "Enter Canvas Width and Height",
JOptionPane.QUESTION_MESSAGE);
try {
width = Integer.parseInt(widthField.getText());
height = Integer.parseInt(heightField.getText());
if(width<900 || height <800){
JOptionPane.showMessageDialog(null, p,
"W:900,H:800 Minimum required", JOptionPane.ERROR_MESSAGE);
}
draw.setWH(width, height);
draw.openPaint();
} catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null, p,
"Please enter valid number!", JOptionPane.ERROR_MESSAGE);
}
}
/**
* @param args
* none
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new InputWH();
}
});
}
}
class RequestFocusListener implements AncestorListener {
private boolean removeListener;
public RequestFocusListener() {
this(true);
}
public RequestFocusListener(boolean removeListener) {
this.removeListener = removeListener;
}
@Override
public void ancestorAdded(AncestorEvent e) {
JComponent component = e.getComponent();
component.requestFocusInWindow();
if (removeListener)
component.removeAncestorListener(this);
}
@Override
public void ancestorMoved(AncestorEvent e) {
}
@Override
public void ancestorRemoved(AncestorEvent e) {
}
}