myMouseKeyboardPanel.java 5/7/2012 import javax.swing.*; import ...

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 8 months ago)

81 views

myMouseKeyboardPanel.java 5/7/2012
import
javax
.
swing
.
*
;
import
java
.
awt
.
*
;
import
java
.
awt
.
event
.
*
;
import
java
.
util
.
*
;
public
class
myMouseKeyboardPanel
extends
JPanel
implements
ActionListener
,
MouseListener
{
private
final
int
WIDTH
=
300
,
HEIGHT
=
200
;
private
int
rx
=
100
,
ry
=
100
;
private
int
x
,
y
;
private
boolean
insider
=
false
;
private
String
keyString
=
"none"
;
private
JButton
b1
;
public
myMouseKeyboardPanel
()
{
addKeyListener
(
new
DotsListener
())
;
addMouseListener
(
this
)
;
setBackground
(
Color
.
black
)
;
setPreferredSize
(
new
Dimension
(
WIDTH
,
HEIGHT
))
;
b1
=
new
JButton
(
"Start"
)
;
b1
.
addActionListener
(
this
)
;
add
(
b1
)
;
}
public
void
paintComponent
(
Graphics
page
)
{
super
.
paintComponent
(
page
)
;
page
.
setColor
(
Color
.
red
)
;
page
.
drawRect
(
rx
,
ry
,
30
,
30
)
;
repaint
()
;
}
public
void
actionPerformed
(
ActionEvent
event
)
{
requestFocus
()
;
}
public
void
mousePressed
(
MouseEvent
e
)
{
/*
NOTHING
*/
}
public
void
mouseClicked
(
MouseEvent
e
)
{
x
=
e
.
getX
()
;
y
=
e
.
getY
()
;
if
((
x
>
=
rx
&
&
x
<
=
rx
+
30
)
&
&
(
y
>
=
ry
&
&
y
<
=
ry
+
30
))
insider
=
true
;
else
insider
=
false
;
1
myMouseKeyboardPanel.java 5/7/2012
}
public
void
mouseEntered
(
MouseEvent
e
)
{
/*
NOTHING
*/
}
public
void
mouseExited
(
MouseEvent
e
)
{
/*
NOTHING
*/
}
public
void
mouseReleased
(
MouseEvent
e
)
{
/*
NOTHING
*/
}
private
class
DotsListener
implements
KeyListener
{
public
void
keyPressed
(
KeyEvent
event
)
{
int
keyCode
=
event
.
getKeyCode
()
;
if
(
insider
)
{
if
(
keyCode
=
=
KeyEvent
.
VK_DOWN
)
ry
=
ry
+
1
;
else
if
(
keyCode
=
=
KeyEvent
.
VK_UP
)
ry
=
ry
-
1
;
}
}
public
void
keyTyped
(
KeyEvent
event
)
{
int
id
=
event
.
getID
()
;
char
c
=
event
.
getKeyChar
()
;
if
(
c
=
=
'd'
)
ry
=
ry
+
1
;
else
if
(
c
=
=
'u'
)
ry
=
ry
-
1
;
}
public
void
keyReleased
(
KeyEvent
event
)
{}
}
}
2