-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMotionWithKeyListener.java
More file actions
133 lines (108 loc) · 3.16 KB
/
MotionWithKeyListener.java
File metadata and controls
133 lines (108 loc) · 3.16 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MotionWithKeyListener extends KeyAdapter
{
private Component component;
private int deltaX;
private int deltaY;
public MotionWithKeyListener(Component component, int deltaX, int deltaY)
{
this.component = component;
this.deltaX = deltaX;
this.deltaY = deltaY;
}
// 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);
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_LEFT)
move(-deltaX, 0);
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
move(deltaX, 0);
else if (e.getKeyCode() == KeyEvent.VK_UP)
move(0, -deltaY);
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
move(0, deltaY);
}
private static JButton addMotionSupport(Component component)
{
KeyListener keyListener = new MotionWithKeyListener(component, 3, 3);
component.addKeyListener(keyListener);
component.setFocusable(true);
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 Listener");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( content );
frame.add(left, BorderLayout.SOUTH);
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);
}
}
}