-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMotionWithKeyBindings.java
More file actions
158 lines (124 loc) · 3.7 KB
/
MotionWithKeyBindings.java
File metadata and controls
158 lines (124 loc) · 3.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MotionWithKeyBindings
{
private JComponent component;
public MotionWithKeyBindings(JComponent component)
{
this.component = component;
}
// Move the component to its new location. The component will stop
// moving when it reaches the bounds of its container
public void move(int deltaX, int deltaY)
{
int componentWidth = component.getSize().width;
int componentHeight = component.getSize().height;
Dimension parentSize = component.getParent().getSize();
int parentWidth = parentSize.width;
int parentHeight = parentSize.height;
// Determine next X position
int nextX = Math.max(component.getLocation().x + deltaX, 0);
if ( nextX + componentWidth > parentWidth)
{
nextX = parentWidth - componentWidth;
}
// Determine next Y position
int nextY = Math.max(component.getLocation().y + deltaY, 0);
if ( nextY + componentHeight > parentHeight)
{
nextY = parentHeight - componentHeight;
}
// Move the component
component.setLocation(nextX, nextY);
}
public MotionAction addAction(String name, int deltaX, int deltaY)
{
MotionAction action = new MotionAction(name, deltaX, deltaY);
KeyStroke pressedKeyStroke = KeyStroke.getKeyStroke(name);
InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(pressedKeyStroke, name);
component.getActionMap().put(name, action);
return action;
}
private class MotionAction extends AbstractAction
{
private int deltaX;
private int deltaY;
public MotionAction(String name, int deltaX, int deltaY)
{
super(name);
putValue(Action.ACTION_COMMAND_KEY, name);
this.deltaX = deltaX;
this.deltaY = deltaY;
}
public void actionPerformed(ActionEvent e)
{
move(deltaX, deltaY);
}
}
private static JButton addMotionSupport(JComponent component)
{
int delta = 3;
MotionWithKeyBindings motion = new MotionWithKeyBindings(component);
motion.addAction("LEFT", -delta, 0);
motion.addAction("RIGHT", delta, 0);
motion.addAction("UP", 0, -delta);
motion.addAction("DOWN", 0, delta);
Action left = component.getActionMap().get("LEFT");
return new JButton(left);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
// Create a panel with a component to be moved
JPanel content = new JPanel();
content.setLayout(null);
JLabel component = new JLabel( new ColorIcon(Color.BLUE, 40, 40) );
component.setSize( component.getPreferredSize() );
component.setLocation(100, 100);
content.add(component);
JButton left = addMotionSupport( component );
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Motion With Key Bindings");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( content );
frame.add(left, BorderLayout.PAGE_END);
frame.setSize(600, 600);
frame.setLocationByPlatform( true );
frame.setVisible(true);
}
});
}
static class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
}